如何在桌面上获取光标坐标,而不是使用 C# 的表单

How to get cursor coordinates on desktop, not at the form using C#

我找到的所有内容仅当光标位于表单内时才有效。如何可以找出光标在屏幕上任意位置的坐标?

这是获取所有屏幕边界的方法

// For each screen, add the screen properties to a list box.
    foreach (var screen in System.Windows.Forms.Screen.AllScreens)
    {
        listBox1.Items.Add("Device Name: " + screen.DeviceName);
        listBox1.Items.Add("Bounds: " + 
            screen.Bounds.ToString());
        listBox1.Items.Add("Type: " + 
            screen.GetType().ToString());
        listBox1.Items.Add("Working Area: " + 
            screen.WorkingArea.ToString());
        listBox1.Items.Add("Primary Screen: " + 
            screen.Primary.ToString());
    }

来自文档 https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.screen?view=netframework-4.8

正如其他人所说,要在您使用的屏幕中找到要点 Cursor.Positionhttps://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.cursor.position?view=netframework-4.8

基本上您想阅读有关 Screen 对象的所有文档。

获取鼠标屏幕坐标:

public static Point GetMouseScreenPosition()
{
    return Control.MousePosition;
}

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.mouseposition?view=netframework-4.8

我的问题是我使用了在表单外不起作用的 MouseMove 事件。使用计时器解决了问题。