我有2个窗口,可以不同大小/不同位置。 最终目标是能够在特定位置点击窗口A ,并在相对的同一位置点击窗口B 。
这是一个屏幕,解释我要实现的目标:
现在我要检索:
我听说过ScreenToClient / ClientToScreen,并且我了解在单击窗口A 时需要找到鼠标的相对位置,并将点击相对发送到窗口B >
此外,要发送我的按钮,请点击我使用:
SendMessage(character.MainWindowHandle, 0x201, IntPtr.Zero, CreateParams(?,?);
SendMessage(character.MainWindowHandle, 0x202, IntPtr.Zero, CreateParams(?,?);
我需要找到两个问号
我现在的代码:
var windowAHandle = Win32Api.GetForegroundWindow();
var windowARect = Win32Api.GetWindowRectangle(windowAHandle);
var windowB = PersosEnLigne.First(w => w.Nom == "Character 2");
var windowBRect = Win32Api.GetWindowRectangle(windowB.Process.MainWindowHandle);
var ptTopLeft = new Win32Api.POINT();
var ptBottomRight = new Win32Api.POINT();
ptTopLeft.x = windowARect.Left;
ptTopLeft.y = windowARect.Top;
ptBottomRight.x = windowARect.Right;
ptBottomRight.y = windowARect.Bottom;
Win32Api.ScreenToClient(windowB.MainWindowHandle, ref ptTopLeft);
Win32Api.ScreenToClient(windowB.MainWindowHandle, ref ptBottomRight);
//I'm not even sure if I need to use ScreenToClient or ClientToScreen
答案 0 :(得分:1)
有了WindowA矩形后,找到当前光标位置相对于WindowA矩形左上角的偏移位置:
Point offsetA = new Point(Cursor.Position.X - windowARect.Left, Cursor.Position.Y - windowARect.Top);
现在计算此偏移相对于WindowA的大小(宽度/高度)的“百分比”:
double xPct = (double)offsetA.X / (double)(windowARect.Right - windowARect.Left + 1);
double yPct = (double)offsetA.Y / (double)(windowARect.Bottom - windowARect.Top + 1));
现在,您可以找到WindowB中的“相同”位置,方法是找到宽度/高度,乘以“百分比”,然后将该数字添加到WindowB的左上方:
int xOffsetB = (int)((double)(windowBRect.Right - windowBRect.Left + 1) * xPct);
int yOffsetB = (int)((double)(windowBRect.Bottom - windowBRect.Top + 1) * yPct);
Point offsetB = new Point(windowBRect.Left + xOffsetB, windowBRect.Top + yOffsetB);
现在,您可以使用offsetB
中的X,Y值来了解在第二个窗口中单击的位置。