在最小化的窗口中获取鼠标单击坐标

时间:2019-12-01 23:50:11

标签: vb.net process automation window

我正在尝试在最小化的窗口中自动进行鼠标单击。

由于我的屏幕/桌面坐标与进程/窗口坐标不同,因此出现问题。

这是我正在测试的代码:

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))

该代码正在运行,它将鼠标单击发送到所需窗口,但未发送到正确的坐标。

因此,我需要找到要单击的窗口区域的相对坐标,而不是桌面/屏幕坐标。

有什么方法可以检测发送到进程/窗口的事件以获得相对坐标?

例如,在Visual Studio中,有一个名为spy ++的工具可以工作,但是现在我没有将点击发送到自己的应用程序中。

1 个答案:

答案 0 :(得分:0)

ScreenToClient函数解决了此问题:

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);