Search code examples
c#delphiwinapivb6

get current mouse cursor type


How do I get the current GLOBAL mouse cursor type (hourglass/arrow/..)? In Windows.

Global - I need it even if the mouse is ouside of my application or even if my program is windlowless.

In C#, Delphi or pure winapi, nevermind...

Thank you very much in advance!!


Solution

  • After thee years its time to answer my own question. Here's how you check if the current global cursor is hourglass in C# (extend the code for you own needs if you need):

    private static bool IsWaitCursor()
    {
        var h = Cursors.WaitCursor.Handle;
    
        CURSORINFO pci;
        pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO));
        if(!GetCursorInfo(out pci))
            throw new Win32Exception(Marshal.GetLastWin32Error());
    
        return pci.hCursor == h;
    }
    
    [StructLayout(LayoutKind.Sequential)]
    struct POINT
    {
        public Int32 x;
        public Int32 y;
    }
    
    [StructLayout(LayoutKind.Sequential)]
    struct CURSORINFO
    {
        public Int32 cbSize;        // Specifies the size, in bytes, of the structure. 
        // The caller must set this to Marshal.SizeOf(typeof(CURSORINFO)).
        public Int32 flags;         // Specifies the cursor state. This parameter can be one of the following values:
        //    0             The cursor is hidden.
        //    CURSOR_SHOWING    The cursor is showing.
        public IntPtr hCursor;          // Handle to the cursor. 
        public POINT ptScreenPos;       // A POINT structure that receives the screen coordinates of the cursor. 
    }
    
    [DllImport("user32.dll", SetLastError = true)]
    static extern bool GetCursorInfo(out CURSORINFO pci);