Search code examples
c#mouse-positionwindows-api-code-pack

How to obtain mouse cursor coordinates from lParam from a low level mouse callback method?


The question heading itself pretty much describes my overall problem. Following is what I have done so far.

    // the event is registered as following 
     mouseProc = new CallWndRetProc(MouseProc); // get keys
     MouseProcHandle = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, IntPtr.Zero, 0);

     // The callback method
     public static IntPtr MouseProc(int nCode, int wParam, IntPtr lParam)
    {            
        if (wParam == WM_LBUTTONUP && MouseProcHandle != IntPtr.Zero )                
        {

        }

        if (wParam == WM_MOUSEMOVE)
        {
          // Want to get mouse position here 
        }

        return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);
    }

Is there a reliable way to get the mouse position ?

Code examples will be appreciated Thanks


Solution

  • According to codeguru forum and especially pinvoke.net you are looking probably for (pinvoke.net again):

    [StructLayout(LayoutKind.Sequential)]
     public struct MSLLHOOKSTRUCT
     {
         public POINT pt;
         public int mouseData; // be careful, this must be ints, not uints (was wrong before I changed it...). regards, cmew.
         public int flags;
         public int time;
         public UIntPtr dwExtraInfo;
     }
    

    Then of course, you could always get current coordinates. Lots of this here on Stackoverflow.