想必有很多人在项目开发中可能遇见需要做模拟鼠标点击的小功能,很多人会在

百度过后采用mouse_event这个函数,不过我并不想讨论如何去使用mouse_event

函数怎么去使用,因为那没有多大意义。

[csharp] view plaincopy
  1. static void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo)
  2. {
  3. int x = dx, y = dy;
  4. edit_position(dwFlags, dx, dy, ref x, ref y);
  5. IntPtr hWndFromPoint = WindowFromPoint(x, y);
  6. screen_to_client(hWndFromPoint, ref x, ref y);
  7. send_message(hWndFromPoint, dwFlags, cButtons, x, y);
  8. }

上述代码你发现了什么?如果你发现说明你知道了本文到底在写什么东东 说不定你

会有一些兴趣看下去,不过想到我如今混那么凄惨 在工地上做干活 不过也还好。

鼠标点击目标时会向鼠标所点击目标窗口投递消息,根据鼠标的按键、状态不同会

投递不同的消息,一个完整的“鼠标左键单击”事件过程为“WM_LBUTTONDOWN +

WM_LBUTTONUP”即鼠标“先左键按下 + 后左键抬起”,由于mouse_event可以模拟

鼠标点击过程而不是直接性一次完整的鼠标单击过程,所以同样存在“按下、抬起”

[csharp] view plaincopy
  1. mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP | MOUSEEVENTF_MOVE, -450, 0, 1, 0);

mouse_event在没有提供MOUSEEVENTF_MOVE量时光标不会移动到相对位置,

“光标相对位置=光标现行位置+新光标位置”如果提供量“MOUSEEVENTF_ABSOLUTE”

绝对位置,则会以“新光标位置”为准而不会添加“光标现行位置”

[csharp] view plaincopy
  1. static void edit_position(int dwFlags, int dx, int dy, ref int x, ref int y)
  2. {
  3. Point pos = MousePosition;
  4. x = x + pos.X;
  5. y = y + pos.Y;
  6. if ((dwFlags | MOUSEEVENTF_ABSOLUTE) == dwFlags)
  7. SetCursorPos(dx, dy);
  8. if ((dwFlags | MOUSEEVENTF_MOVE) == dwFlags)
  9. SetCursorPos(x, y);
  10. }

edit_position函数主要用于对MOUSEEVENTF_MOVE于MOUSEEVENTF_ABSOLUTE

相对/绝对光标位置修改的一个支持

[csharp] view plaincopy
  1. static void send_message(IntPtr hWnd, int dwFlags, int cButtons, int x, int y)
  2. {
  3. if ((dwFlags | MOUSEEVENTF_LEFTDOWN) == dwFlags)
  4. SendMessage(hWnd, WM_LBUTTONDOWN, cButtons, MakeDWord(x, y));
  5. if ((dwFlags | MOUSEEVENTF_LEFTUP) == dwFlags)
  6. SendMessage(hWnd, WM_LBUTTONUP, cButtons, MakeDWord(x, y));
  7. if ((dwFlags | MOUSEEVENTF_RIGHTDOWN) == dwFlags)
  8. SendMessage(hWnd, WM_RBUTTONDOWN, cButtons, MakeDWord(x, y));
  9. if ((dwFlags | MOUSEEVENTF_RIGHTUP) == dwFlags)
  10. SendMessage(hWnd, WM_RBUTTONUP, cButtons, MakeDWord(x, y));
  11. if ((dwFlags | MOUSEEVENTF_MIDDLEDOWN) == dwFlags)
  12. SendMessage(hWnd, WM_MBUTTONDOWN, cButtons, MakeDWord(x, y));
  13. if ((dwFlags | MOUSEEVENTF_MIDDLEUP) == dwFlags)
  14. SendMessage(hWnd, WM_MBUTTONUP, cButtons, MakeDWord(x, y));
  15. }

send_message函数主要用于模拟鼠标点击的过程,上面我提到“先左键按下 + 后左键抬起”

在上面的代码中你会看的清楚的不得了,如果相反你可以去尝试一番会有什么后果 与其说

不如你们自己做更要来的快些。

[csharp] view plaincopy
  1. static int MakeDWord(int low, int high)
  2. {
  3. return low + (high * Abs(~ushort.MaxValue));
  4. }
  5. static int Abs(int value)
  6. {
  7. return ((value >> 31) ^ value) - (value >> 31);
  8. }

MakeDWord / 合并整数,函数主要是把两个short合并为一个int,分为low、high两部分

[csharp] view plaincopy
  1. static bool screen_to_client(IntPtr hwnd, ref int x, ref int y)
  2. {
  3. bool bRetVal = false;
  4. Point lpptPos = new Point(x, y);
  5. if ((bRetVal = ScreenToClient(hwnd, ref lpptPos)))
  6. {
  7. x = lpptPos.X;
  8. y = lpptPos.Y;
  9. }
  10. return bRetVal;
  11. }

screen_to_client函数故名思意,它主要用于把屏幕上的坐标转换到窗口客户上对应坐标

[csharp] view plaincopy
  1. public const int WM_LBUTTONDOWN = 513; // 鼠标左键按下
  2. public const int WM_LBUTTONUP = 514; // 鼠标左键抬起
  3. public const int WM_RBUTTONDOWN = 516; // 鼠标右键按下
  4. public const int WM_RBUTTONUP = 517; // 鼠标右键抬起
  5. public const int WM_MBUTTONDOWN = 519; // 鼠标中键按下
  6. public const int WM_MBUTTONUP = 520; // 鼠标中键抬起
  7. public const int MOUSEEVENTF_MOVE = 0x0001; // 移动鼠标
  8. public const int MOUSEEVENTF_LEFTDOWN = 0x0002; // 鼠标左键按下
  9. public const int MOUSEEVENTF_LEFTUP = 0x0004; // 鼠标左键抬起
  10. public const int MOUSEEVENTF_RIGHTDOWN = 0x0008; // 鼠标右键按下
  11. public const int MOUSEEVENTF_RIGHTUP = 0x0010; // 鼠标右键抬起
  12. public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; // 鼠标中键按下
  13. public const int MOUSEEVENTF_MIDDLEUP = 0x0040; // 鼠标中键抬起
  14. public const int MOUSEEVENTF_ABSOLUTE = 0x8000; // 绝对坐标
[csharp] view plaincopy
  1. [DllImport("user32.dll", SetLastError = true)]
  2. public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, int lParam);
  3. [DllImport("user32.dll", SetLastError = true)]
  4. public static extern IntPtr WindowFromPoint(int xPoint, int yPoint);
  5. [DllImport("user32.dll", SetLastError = true)]
  6. public static extern int SetCursorPos(int x, int y);
  7. [DllImport("user32.dll", SetLastError = true)]
  8. public static extern bool ScreenToClient(IntPtr hWnd, ref Point lppt);
  9. // [DllImport("user32", SetLastError = true)]
  10. // public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

鼠标右键单击(静默):

[csharp] view plaincopy
  1. mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 1, 0);

鼠标左键双击(静默):

[csharp] view plaincopy
  1. mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 2, 0);

鼠标移动(相对位置):

[csharp] view plaincopy
  1. mouse_event(MOUSEEVENTF_MOVE, 100, 50, 0, 0);

鼠标移动(绝对位置):

[csharp] view plaincopy
  1. mouse_event(MOUSEEVENTF_ABSOLUTE, 100, 50, 0, 0);

好了就这么多吧,现在浑身头晕脑涨的 话说多了反倒不好 自己从代码中领悟才是真

C# 模拟鼠标(mouse_event)相关推荐

  1. C#用mouse_event模拟鼠标点击的问题

    1.首先添加using System.Runtime.InteropServices; 2.为鼠标添加模拟点击的各种参数 //鼠标事件  因为我用的不多,所以其他参数没有写 1 2 3 4 5 6 7 ...

  2. Qt:Qt使用鼠标模拟函数mouse_event和按键模拟函数keybd_even实现网页刷新功能

    用Qt实现网页刷新功能 前言 在上一篇博客Qt:使用Qt实现网页自动刷新工具,使用了PostMassage函数 通过Windows的消息机制实现的网页刷新功能.因为消息种类太多,不方便使用和记忆,所以 ...

  3. C#使用mouse_event函数模拟鼠标事件

    C#使用mouse_event函数模拟鼠标事件 mouse_event函数 private static extern int mouse_event(int dwFlags, int dx, int ...

  4. 用Mouse_event()来模拟鼠标操作

    在自动化测试的开发中,有一些控件的ID是很难找到的,所以有些时候,我们直接设置鼠标的位置,然后是用click事件,会收到很好的效果.在Windows API中有个mouse_event函数为我们准备好 ...

  5. 模拟键盘、鼠标 mouse_event keybd_event

    MSDN 讲解:http://msdn.microsoft.com/en-us/library/ms646304(v=vs.85).aspx 中文讲解:http://dev.firnow.com/co ...

  6. C++模拟鼠标点击和键盘输入的操作 mouse_event keybd_event

    实际运用,视频讲解 python和C++实现模拟键鼠操作实测说明[300英雄] 高清视频传送门:点我 相关环境 操作系统:Win10 企业版 编译环境:MinGW 相关说明 参考资料:windows ...

  7. C# 模拟鼠标移动与点击

    我们需要用到的mouse_event函数,位于user32.dll这个库文件里面,所以我们要先声明引用. [System.Runtime.InteropServices.DllImport(" ...

  8. 如何模拟鼠标的拖拽行为.

    如何模拟鼠标的拖拽行为. Delphi / Windows SDK/API http://www.delphi2007.net/DelphiAPI/html/delphi_20061112235944 ...

  9. vc++6.0 模拟鼠标点击代码 木马程序的编写 VC 模拟键盘输入

    From: http://fengqing888.blog.163.com/blog/static/33011416201112124481/ MFC 工程 把以下代码放到你想要响应的函数里面就行 C ...

最新文章

  1. 小程序无限层级路由方案
  2. pyqt入门教程(一)
  3. Yarn ResourceManager High Availability
  4. Java中switch语句支持的类型
  5. C# dataTable 转 IList 问题
  6. 任意两点最短路floyd算法matlab,多源最短路——Floyd算法
  7. phpstorm如何进行文件或者文件夹重命名
  8. 路飞学城-python爬虫密训-第三章
  9. 前端学习(2325):angular之添加新组件
  10. STM32H743+CubeMX-移植ThreadX
  11. 实战 | Vue + Element UI 页面创建
  12. MySQL8单表记录多少_mysql学习笔记之8(单表数据记录查询)_mysql
  13. 一个关于从1到100的加法算法
  14. 路由的跳转 , 动态路由的配置
  15. 新生代的他们,正在续写“黑客”传奇
  16. eclipse jar打包 jar line too long 异常处理方法
  17. 【数据结构笔记12】平衡二叉树,AVL树,RR旋转/LL旋转/LR旋转/RL旋转,AVL树插入的代码实现
  18. druid连接池_springboot三步整合阿里数据库连接池druid
  19. python的cubes怎么使用_Python之Cubes框架使用
  20. [L4D]目录Missions中任务与地图关系

热门文章

  1. 内容管理系统测试实战
  2. 尝试用LoadRunner录制单机程序--未生成录制脚本
  3. 同样是软件测试工程师,为什么性能测试月薪更高?
  4. react 引入html文件_React最快速上手教程
  5. php启动端口修改,PHP_启动服务端口报错
  6. java中调用数组参数_java中如何调用带有数组类型参数的存储过程
  7. android支付宝余额怎么做,android实现类似于支付宝余额快速闪动的效果
  8. android怎么做版本检测,android 实现检测版本,下载apk更新(附源码)
  9. b丅151组成的充电器电路_一文读懂uc3842组成的开关电源电路
  10. html5 audio js控制进度,HTML5 audio标签使用js进行播放控制实例