如何使用 user32.dll 捕获鼠标离开事件

How can I catch mouse leave event using user32.dll

我正在使用 user32.dll 获取光标位置和模拟鼠标点击等。我在 WPF 中使用 MouseLeave 事件 app.But 我想捕捉鼠标离开(或鼠标悬停)来自所有 windows 的事件(不仅在我的 WPF 应用程序中)。是否可以使用 user32.dll 或其他方式捕获此事件?

有一个 Windows API 调用允许获取鼠标事件,即使它们不在 window:

SetCapture(hWnd)

您可以将此与调用结合起来:

WindowFromPoint()

获取哪个 Window 鼠标悬停

我是这样做的

[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);
            }
        }));
    }