在弄清楚如何使用低级 Hook 获取沿着显示器边界的任意位置的鼠标单击位置后,我收到一个 X Y 坐标,该坐标通常包含 x: -1680 到 +1920
和 y: 0 to 1200
在我的电脑案例中。够简单的!
现在的问题是,我现在想要计算相对于给定窗口的鼠标位置,因此我使用 GetForegroundWindow()
和 GetWindowRect(HandleRef hWnd, out RECT lpRect)
获取我的事件窗口坐标。
我遇到的问题是我需要当前事件桌面(事件桌面是指单击发生在哪个显示器上)来计算鼠标单击相对于窗口的坐标。
不幸的是,我无法找到像 GetActiveMonitor()
或类似的 API 调用,所以希望有人能给我指出正确的方向?
最佳答案
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
Call it as:
RECT rct = new RECT();
GetWindowRect(hWnd, ref rct);
像这样获取鼠标位置后
int mouserelativepositionX = mousePosition.X - rct.Left;
int mouserelativepositionY = mousePosition.Y - rct.Top;
关于c# - 获取相对于事件窗口的鼠标点击位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12122801/