C#采用mouse_event函数实现模拟鼠标功能
下面我通过代码为大家分享下c#模拟鼠标,具体内容如下:
想必有很多人在项目开发中可能遇见需要做模拟鼠标点击的小功能,很多人会在百度过后采用mouse_event这个函数,不过我并不想讨论如何去使用mouse_event函数怎么去使用,因为那没有多大意义。
static void mouse_event(int dwflags, int dx, int dy, int cbuttons, int dwextrainfo) { int x = dx, y = dy; edit_position(dwflags, dx, dy, ref x, ref y); intptr hwndfrompoint = windowfrompoint(x, y); screen_to_client(hwndfrompoint, ref x, ref y); send_message(hwndfrompoint, dwflags, cbuttons, x, y); }
上述代码你发现了什么?如果你发现说明你知道了本文到底在写什么东东 说不定你会有一些兴趣看下去,不过想到我如今混那么凄惨 在工地上做干活 不过也还好。
鼠标点击目标时会向鼠标所点击目标窗口投递消息,根据鼠标的按键、状态不同会投递不同的消息,一个完整的“鼠标左键单击”事件过程为“wm_lbuttondown +
wm_lbuttonup”即鼠标“先左键按下 + 后左键抬起”,由于mouse_event可以模拟鼠标点击过程而不是直接性一次完整的鼠标单击过程,所以同样存在“按下、抬起”
mouse_event(mouseeventf_leftdown | mouseeventf_leftup | mouseeventf_move, -450, 0, 1, 0);
mouse_event在没有提供mouseeventf_move量时光标不会移动到相对位置,“光标相对位置=光标现行位置+新光标位置”如果提供量“mouseeventf_absolute”绝对位置,则会以“新光标位置”为准而不会添加“光标现行位置”
static void edit_position(int dwflags, int dx, int dy, ref int x, ref int y) { point pos = mouseposition; x = x + pos.x; y = y + pos.y; if ((dwflags | mouseeventf_absolute) == dwflags) setcursorpos(dx, dy); if ((dwflags | mouseeventf_move) == dwflags) setcursorpos(x, y); }
edit_position函数主要用于对mouseeventf_move于mouseeventf_absolute
相对/绝对光标位置修改的一个支持
static void send_message(intptr hwnd, int dwflags, int cbuttons, int x, int y) { if ((dwflags | mouseeventf_leftdown) == dwflags) sendmessage(hwnd, wm_lbuttondown, cbuttons, makedword(x, y)); if ((dwflags | mouseeventf_leftup) == dwflags) sendmessage(hwnd, wm_lbuttonup, cbuttons, makedword(x, y)); if ((dwflags | mouseeventf_rightdown) == dwflags) sendmessage(hwnd, wm_rbuttondown, cbuttons, makedword(x, y)); if ((dwflags | mouseeventf_rightup) == dwflags) sendmessage(hwnd, wm_rbuttonup, cbuttons, makedword(x, y)); if ((dwflags | mouseeventf_middledown) == dwflags) sendmessage(hwnd, wm_mbuttondown, cbuttons, makedword(x, y)); if ((dwflags | mouseeventf_middleup) == dwflags) sendmessage(hwnd, wm_mbuttonup, cbuttons, makedword(x, y)); }
send_message函数主要用于模拟鼠标点击的过程,上面我提到“先左键按下 + 后左键抬起”在上面的代码中你会看的清楚的不得了,如果相反你可以去尝试一番会有什么后果与其说
不如你们自己做更要来的快些。
static int makedword(int low, int high) { return low + (high * abs(~ushort.maxvalue)); } static int abs(int value) { return ((value >> 31) ^ value) - (value >> 31); } makedword / 合并整数,函数主要是把两个short合并为一个int,分为low、high两部分 static bool screen_to_client(intptr hwnd, ref int x, ref int y) { bool bretval = false; point lpptpos = new point(x, y); if ((bretval = screentoclient(hwnd, ref lpptpos))) { x = lpptpos.x; y = lpptpos.y; } return bretval; } screen_to_client函数故名思意,它主要用于把屏幕上的坐标转换到窗口客户上对应坐标 public const int wm_lbuttondown = 513; // 鼠标左键按下 public const int wm_lbuttonup = 514; // 鼠标左键抬起 public const int wm_rbuttondown = 516; // 鼠标右键按下 public const int wm_rbuttonup = 517; // 鼠标右键抬起 public const int wm_mbuttondown = 519; // 鼠标中键按下 public const int wm_mbuttonup = 520; // 鼠标中键抬起 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", setlasterror = true)] public static extern int sendmessage(intptr hwnd, int umsg, int wparam, int lparam); [dllimport("user32.dll", setlasterror = true)] public static extern intptr windowfrompoint(int xpoint, int ypoint); [dllimport("user32.dll", setlasterror = true)] public static extern int setcursorpos(int x, int y); [dllimport("user32.dll", setlasterror = true)] public static extern bool screentoclient(intptr hwnd, ref point lppt); // [dllimport("user32", setlasterror = true)] // public static extern int mouse_event(int dwflags, int dx, int dy, int cbuttons, int dwextrainfo);
鼠标右键单击(静默):
mouse_event(mouseeventf_rightdown | mouseeventf_rightup, 0, 0, 1, 0);
鼠标左键双击(静默):
mouse_event(mouseeventf_leftdown | mouseeventf_leftup, 0, 0, 2, 0);
鼠标移动(相对位置):
mouse_event(mouseeventf_move, 100, 50, 0, 0);
鼠标移动(绝对位置):
mouse_event(mouseeventf_absolute, 100, 50, 0, 0);
以上内容比较多请认真学习,希望能够帮助到大家。
上一篇: 丰胸方法有哪些(食疗方法推荐)
下一篇: C#中值类型和引用类型解析