How to get global cursor info using GetCursorInfo

Posted on 2004-11-19
Last Modified: 2008-01-09
I was not able to call GetCursorInfo method in the "user32" lib in VBCODE. Can the expert please show how this method can be implemented in a vb project
Thanks in advance
Question by:manish_tg
    LVL 14

    Expert Comment

    by:Shiju Sasidharan

    here is an example that uses GetCursorInfo
    u can Download  Source code from this link

    LVL 48

    Expert Comment

    GetCursorInfo Function


    The GetCursorInfo function retrieves information about the global cursor.


    BOOL GetCursorInfo(          PCURSORINFO pci

    [out] Pointer to a CURSORINFO structure that receives the information. Note that you must set CURSORINFO.cbSize to sizeof(CURSORINFO) before calling this function.
    Return Value

    If the function succeeds, the return value is nonzero.

    If the function fails, the return value is zero. To get extended error information, call GetLastError.

    Function Information

    Minimum DLL Version user32.dll
    Header Declared in Winuser.h, include Windows.h
    Import library User32.lib
    Minimum operating systems Windows 98, Windows  
    LVL 17

    Expert Comment

    Option Explicit

    Private Type POINTAPI
        x As Long
        y As Long
    End Type

    Private Type PCURSORINFO
        cbSize As Long
        flags As Long
        hCursor As Long
        ptScreenPos As POINTAPI
    End Type

    Private Declare Function GetCursorInfo Lib "user32.dll" (ByRef pci As PCURSORINFO) As Long
    Private Sub Form_Load()
        Dim typCI As PCURSORINFO
        typCI.cbSize = Len(typCI)
        If GetCursorInfo(typCI) <> 0 Then
            Debug.Print typCI.flags, typCI.hCursor, typCI.ptScreenPos.x & "," & typCI.ptScreenPos.y
        End If
    End Sub
    LVL 48

    Expert Comment

    GetCursor will return a handle to the current cursor which you could then compare with a standard pointer to see which it is

    GetCursorInfo returns a CURSORINFO struct which also includes if it is visible and its position.

    To test which sort of pointer is being shown, you could use CopyImage to copy the selected cursor's image and CopyImage on system cursor (which you load using LoadCursor) then compare the bits of the image...


    Author Comment

    Thanks zzzzzooc . Your code was what I was requiring and it worked perfectly thanks.

    Can you tell we what FLAGS and HCURSOR values means.

    Actually I am using this information to find whether the mouse is being handled exclusively by some directX application or it is under the control of the window OS.

    When i using any normal windows application, i get Flag=1 and HCURSOR=5254

    but when I am using a Fulscreen Game Application that uses DirectX, i get Flag=0 and HCURSOR=0

    Can i assume that whenever Flag and HCURSOR are zero, it means the mouse is being accessed exclusively by some directX application? Thanks for your code
    LVL 17

    Accepted Solution

    The below is documentation on the structure.

    "CURSORINFO Structure"

    >> it means the mouse is being accessed exclusively by some directX application
    As the link above mentions, Flag will be 0 if the cursor is hidden and hCursor will be 0 if the current cursor has no handle to a loaded cursor. Usually for DirectX games, they'll use their own cursors and hide the current one, so it's safe to use this method. You may want to test different games, though.

    Author Comment

    Thanks buddy. A very useful comments and you have solved by big problem cheers!!!. This should work on NT and XP too....right, I have tested on 98.
    LVL 17

    Expert Comment

    "Minimum operating systems Windows 98, Windows NT 4.0 SP6"

    Worked for me on Win2k Pro. Good luck. :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    Watch V7 9 49
    Extract Data and filename Macro 20 90
    Access 2016 VB code 9 69
    String manipulation in Visual Basic 7 33
    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now