Search code examples
vb.netprocessautomationwindow

Get mouse click coordinates in a minimized window


I'm trying to automate mouse clicks in a minimized window.

As my screen/desktop coordinates are not the same as a process/window coordinates I'm having issues.

This is the code I'm testing with:

Function MakeDWord(LoWord As Integer, HiWord As Integer) As Long
    Return New IntPtr((HiWord << 16) Or (LoWord And &HFFFF))
End Function

SendMessage(_targetProcess.MainWindowHandle, WM_LBUTTONDOWN, 0&, MakeDWord(x, y))
SendMessage(_targetProcess.MainWindowHandle, WM_LBUTTONUP, 0&, MakeDWord(x, y))

The code is working, it's sending a mouse click to the desired window, but not to the right coordinates.

So, I need to find the relative coordinates of the window area I want to click, instead of the desktop/screen coordinates.

There's any way to detect the events sent to a process/window in order to get the relative coordinates?

For example, in Visual Studio there's a tool called spy++ that works, but now I'm not sending the click to my own application.


Solution

  • The ScreenToClient function solved this:

    https://pinvoke.net/default.aspx/user32/ScreenToClient.html

    RECT rct;
    POINT topLeft;
    POINT bottomRight;
    
    /** Getting a windows position **/
    GetWindowRect(hWnd, out rct);
    
    /** assign RECT coods to POINT **/
    topLeft.X = rct.Left;
    topLeft.Y = rct.Top;
    bottomRight.X = rct.Right;
    bottomRight.Y = rct.Bottom;
    
    /** this takes the POINT, which is using screen coords (0,0 in top left screen) and converts them into coords inside specified window (0,0 from  top left of hWnd) **/
    ScreenToClient(hWnd, ref topLeft);
    ScreenToClient(hWnd, ref bottomRight);
    
    int width = bottomRight.X - topLeft.X;
    int height = bottomRight.Y - topLeft.Y;
    
    Rectangle R = new Rectangle(topLeft.X, topLeft.Y, width, height);