我正在使用user32.dll获取光标位置和模拟鼠标点击等。我在WPF应用程序中使用MouseLeave事件。但我想从所有窗口捕获鼠标离开(或鼠标悬停)事件(不仅在我的WPF app)。是否可以使用user32.dll或其他东西捕获此事件?
我是这样做的
[DllImport("user32.dll")]
static extern bool GetCursorPos(out Point lpPoint);
void StartGettingCursorPos()
{
Task.Run(new Action(() =>
{
while (true)
{
GetCursorPos(out Point point);
Console.WriteLine($"X:{point.X}; Y:{point.Y}");
Thread.Sleep(20);
}
}));
}
有一个Windows API调用允许获取鼠标事件,即使它们不在窗口中:
SetCapture(HWND)
您可以将其与以下呼叫结合使用:
WindowFromPoint()
获取鼠标所在的窗口