📜  c# set cursor pos - C# (1)

📅  最后修改于: 2023-12-03 14:59:40.836000             🧑  作者: Mango

C# Set Cursor Position

在某些情况下,你可能需要在C#应用程序中控制光标的位置。这时候,你可以使用SetCursorPos API来实现。

使用方法

要使用SetCursorPos API,你需要使用System.Runtime.InteropServices命名空间,并引用完整的Win32 DLL。

using System.Runtime.InteropServices;

[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
示例代码

以下是一个示例代码,它可以将光标移动到屏幕上的指定X和Y坐标。

int x = 100;
int y = 200;
SetCursorPos(x, y);
注意事项

需要注意的是,SetCursorPos API只会在真正的物理屏幕上移动光标,而不是在虚拟屏幕(如果有)上移动。因此,如果需要在多个显示器上移动光标,需要进行额外的处理。