東西不難。
使用的函數那么幾個。
本例是我刪除淘寶購物記錄時寫的,所以是兩個坐標點來回移動並點擊鼠標左鍵。
using System; using System.Runtime.InteropServices; using System.Threading; namespace 鼠標移動且點擊 { public enum MouseType { //移動鼠標 MOUSEEVENTF_MOVE = 0x0001, //模擬鼠標左鍵按下 MOUSEEVENTF_LEFTDOWN = 0x0002, //模擬鼠標左鍵抬起 MOUSEEVENTF_LEFTUP = 0x0004, //模擬鼠標右鍵按下 MOUSEEVENTF_RIGHTDOWN = 0x0008, //模擬鼠標右鍵抬起 MOUSEEVENTF_RIGHTUP = 0x0010, //模擬鼠標中鍵按下 MOUSEEVENTF_MIDDLEDOWN = 0x0020, //模擬鼠標中鍵抬起 MOUSEEVENTF_MIDDLEUP = 0x0040, //標示是否采用絕對坐標 MOUSEEVENTF_ABSOLUTE = 0x8000, } class Program { [System.Runtime.InteropServices.DllImport("user32")] public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); [DllImport("User32.dll")] public static extern bool SetCursorPos(int X, int Y); static void Main(string[] args) { Init(); Console.ReadKey(); } static int x1, y1, x2, y2; private static void DoClick() { SetCursorPos(x1, y1); mouse_event((int)MouseType.MOUSEEVENTF_LEFTDOWN | (int)MouseType.MOUSEEVENTF_LEFTUP, 0,0, 0, 0); //mouse_event((int)MouseType.MOUSEEVENTF_RIGHTDOWN | (int)MouseType.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0); string n = $"x1:{x1},y1:{y1}"; Console.WriteLine(n); Thread.Sleep(300); } private static void DoClickXY2() { SetCursorPos(x2, y2); // mouse_event((int)MouseType.MOUSEEVENTF_RIGHTDOWN | (int)MouseType.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0); mouse_event((int)MouseType.MOUSEEVENTF_LEFTDOWN | (int)MouseType.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); string n = $"x2:{x2},y2:{y2}"; Console.WriteLine(n); Thread.Sleep(300); } private static void InputData() { Console.WriteLine("x1坐標"); x1 = int.Parse(Console.ReadLine()); Console.WriteLine("y1坐標"); y1 = int.Parse(Console.ReadLine()); Console.WriteLine("x2坐標"); x2 = int.Parse(Console.ReadLine()); Console.WriteLine("y2坐標"); y2 = int.Parse(Console.ReadLine()); Console.WriteLine(); string n = $"x1:{x1},y1:{y1},x2:{x2},y2:{y2}"; Console.WriteLine(n); } static void Init() { while (true) { pool(); Console.WriteLine("是否繼續? Y/N"); var b = Console.ReadKey().KeyChar; if (b != 'Y' && b != 'y') break; } } static void pool() { Console.WriteLine("執行次數:"); int num = int.Parse(Console.ReadLine()); InputData(); while (num >= 0) { DoClick(); Thread.Sleep(100); DoClickXY2(); num--; } Console.WriteLine(); } } }