我正在尝试从控制台.NET Core应用程序中设置鼠标光标位置,该应用程序将在Windows和Linux上运行。是否有任何API可以让我在两个操作系统上都设置位置?
使用.NET Core获取Windows鼠标光标位置是可能的,可以使用user32.dll
中的GetCursorPos
函数。
请看下面的示例:
class Program
{
[DllImport("user32.dll")]
private static extern bool GetCursorPos(out Point lpPoint);
private static void Main()
{
Point mouse = default;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
GetCursorPos(out mouse);
}
else
{
// How to do on Linux and OSX?
}
Console.WriteLine($"Mouse X:{mouse.X} Y:{mouse.Y}");
}
}
如果您想下载此示例项目以进行测试,请随意点击链接:https://github.com/sergiocabral/Sample.CSharpNETCore.GetCursorPosition