获取鼠标相对于活动窗口的单击位置



在研究了如何使用低级挂钩在监视器边界的任何位置获得鼠标点击位置后,我收到了一个X Y坐标,在我的电脑中,该坐标通常包含x: -1680 to +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;

我的猜测是,你可以通过使用if:来知道你的鼠标在哪里

if(mousePosition.X > -1680 && mousePosition.X < 0)
      //We are in monitor 1;
else
      //Monitor 2;

最新更新