/// <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 / 2, winHeight / 2); MoveMouseToPoint(centerP); }