c# - 获取相对于事件窗口的鼠标点击位置

标签 c# winapi multiple-monitors mouse-hook

在弄清楚如何使用低级 Hook 获取沿着显示器边界的任意位置的鼠标单击位置后,我收到一个 X Y 坐标,该坐标通常包含 x: -1680 到 +1920y: 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/

相关文章:

c# - 如何使用 Core 2.2 中的身份在浏览器关闭 15 分钟后保持 session 事件?

c# - 等待的任务能否在 IsCompleted 中返回 false

c# 获取具有特定外键的用户列表

multithreading - EndDialog 不能在线程中调用?

c - 在子菜单项上使用 EnableMenuItem

windows - 在 Windows 上的双显示器上使用 NetBeans 8

c# - 如何更改监视器设置以通过脚本复制和扩展

c# - 表单提交上 HiddenFor 的替代方案?

browser - 在第二台显示器上打开新的浏览器页面

windows - 如何在 Windows 中创建虚拟打印机?