C#自己帶的類庫中沒有關於鼠標操做的函數庫,須要引用微軟的dll,在visual studio中使用 nuget添加 mshtml 便可(Microsoft.mshtml)html
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Runtime.InteropServices; namespace workhelper { class MouseHelper { [System.Runtime.InteropServices.DllImport("user32")] public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); //移動鼠標 public const int MOUSEEVENTF_MOVE = 0x0001; //模擬鼠標左鍵按下 public const int MOUSEEVENTF_LEFTDOWN = 0x0002; //模擬鼠標左鍵擡起 public const int MOUSEEVENTF_LEFTUP = 0x0004; //模擬鼠標右鍵按下 public const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模擬鼠標右鍵擡起 public const int MOUSEEVENTF_RIGHTUP = 0x0010; //模擬鼠標中鍵按下 public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; //模擬鼠標中鍵擡起 public const int MOUSEEVENTF_MIDDLEUP = 0x0040; //標示是否採用絕對座標 public const int MOUSEEVENTF_ABSOLUTE = 0x8000; [DllImport("user32.dll")] public static extern bool SetCursorPos(int X, int Y); } }
把光標移到屏幕的指定位置。(ps:是整個屏幕的座標,相對於屏幕左上角的絕對位置)c#
參數函數
若是失敗,返回值是0spa
dwFlags 標誌位集,指定點擊按鈕和鼠標動做的多種狀況。此參數能夠是下列值的某種組合:code
VALUE | MEANING |
---|---|
MOUSEEVENTF_ABSOLUTE | dX和dY參數含有規範化的絕對座標。若是不設置,這些參數含有相對數據:相對於上次位置的改動位置。此標誌可設置,也可不設置,無論鼠標的類型或與系統相連的相似於鼠標的設備的類型如何。要獲得關於相對鼠標動做的信息,參見下面備註部分 |
MOUSEEVENTF_MOVE | 鼠標移動 |
MOUSEEVENTF_LEFTDOWN | 鼠標左鍵按下 |
MOUSEEVENTF_LEFTUP | 鼠標左鍵鬆開 |
MOUSEEVENTF_RIGHTDOWN | 鼠標右鍵按下 |
MOUSEEVENTF_RIGHTUP | 鼠標右鍵鬆開 |
MOUSEEVENTF_MIDDLEDOWN | 鼠標中鍵按下 |
MOUSEEVENTF_MIDDLEUP | 鼠標中鍵鬆開 |
MOUSEEVENTF_WHEEL | 鼠標輪被滾動,若是鼠標有一個滾輪。滾動的數量由dwData給出 |
MouseHelper.SetCursorPos(Form1.point.X, Form1.point.Y); MouseHelper.mouse_event(MouseHelper.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); MouseHelper.mouse_event(MouseHelper.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);