首页 > 代码库 > C#控制鼠标代码实例
C#控制鼠标代码实例
1获得当前屏幕中鼠标的位置
int i = MousePosition.X; int j = MousePosition.Y;这是control类中的方法。
2移动鼠标
首先引入dll
[System.Runtime.InteropServices.DllImport("user32")]
private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
const int MOUSEEVENTF_MOVE = 0x0001;
const int MOUSEEVENTF_LEFTDOWN = 0x0002;
const int MOUSEEVENTF_LEFTUP = 0x0004;
const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
const int MOUSEEVENTF_RIGHTUP = 0x0010;
const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
const int MOUSEEVENTF_MIDDLEUP = 0x0040;
const int MOUSEEVENTF_ABSOLUTE = 0x8000;
然后调用
mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, 38843, 53702, 0, 0);
说明,有 | MOUSEEVENTF_ABSOLUTE表示绝对位置,好用
绝对位置计算,x= 屏幕鼠标坐标mouseposition.x*65535/1920(这是显示器分辨率宽)
绝对位置y=mouseposition.y*65535/1080屏幕高
3鼠标点击模拟
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);