C# 操作鼠标移动到指定的屏幕位置方法

时间:2022-03-27 08:47:51
 /// <summary>
/// 引用user32.dll动态链接库(windows api),
/// 使用库中定义 API:SetCursorPos
/// </summary>
[DllImport("user32.dll")]
private static extern int SetCursorPos(int x, int y);
/// <summary>
/// 移动鼠标到指定的坐标点
/// </summary>
public void MoveMouseToPoint(Point p)
{
SetCursorPos(p.X, p.Y);
}
/// <summary>
/// 设置鼠标的移动范围
/// </summary>
public void SetMouseRectangle(Rectangle rectangle)
{
System.Windows.Forms.Cursor.Clip = rectangle;
}
/// <summary>
/// 设置鼠标位于屏幕中心
/// </summary>
public void SetMouseAtCenterScreen()
{
int winHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
int winWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
Point centerP = new Point(winWidth / , winHeight / );
MoveMouseToPoint(centerP);
}