Search code examples
c#mousesendinput

Mouse position values for SendInput


I am developing a C# application to simulate mouse movement using SendInput declared in Winuser.h. My calls to SendInput work fine and I can move the mouse around the screen.

What I then tried was to record user mouse movement using:

    [DllImport("user32.dll")]
    public static extern bool GetCursorPos(ref POINT lpPoint);

and then pass the mouse points to SendInput. I noticed a discrepancy between the mouse values obtained by GetCursorPos and where SendInput actually positions the mouse. For example, a mouse x position of 1300 (far right of screen) corresponds to the mouse positioning near left of screen using SendInput.

What's the relation between the POINT data obtained via GetCursorPos and the coordinate system used by SendInput.

Thanks.

Lance


Solution

  • As discussed in the following question:

    SendInput doesn't perform click mouse button unless I move cursor

    The screen coordinates returned by GetCursorPos need to be converted to absolute and passed to SendInput:

    dx =  (x * 65536) / GetSystemMetrics(SystemMetric.SM_CXSCREEN);
    dy = (y * 65536) / GetSystemMetrics(SystemMetric.SM_CYSCREEN);
    

    Lance.