C#_模拟鼠标操作

一、class

 class MouseHelper
 {
     [DllImport("user32.dll")]
     public static extern bool SetCursorPos(int X, int Y);
     [System.Runtime.InteropServices.DllImport("user32")]
     public static extern int mouse_event(int dwFlags, int dx, int dy, int dwData, 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;
     //模拟鼠标滚轮滚动操作,必须配合dwData参数
     public const int MOUSEEVENTF_WHEEL = 0x0800;
     //调用windows的系统锁定
     [DllImport("user32.dll")]
     static extern void BlockInput(bool Block);

 }

二、调用

  前台操作,自行加延迟或锁定键盘鼠标。

 //键盘鼠标锁定
 BlockInput(true);
 //移动鼠标到坐标点
 MouseHelper.SetCursorPos(265,230);
 //鼠标左键单击
 MouseHelper.mouse_event(MouseHelper.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
 MouseHelper.mouse_event(MouseHelper.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
 //鼠标左键双击
 MouseHelper.mouse_event(MouseHelper.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
 MouseHelper.mouse_event(MouseHelper.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
 MouseHelper.mouse_event(MouseHelper.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
 MouseHelper.mouse_event(MouseHelper.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
 //键盘鼠标解锁
 BlockInput(false);

你可能感兴趣的:(C#,c#,计算机外设,开发语言)