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

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

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

  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可以模拟

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

  1. mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP | MOUSEEVENTF_MOVE, -450, 0, 1, 0);

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

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

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

  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

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

  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函数主要用于模拟鼠标点击的过程,上面我提到“先左键按下 + 后左键抬起”

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

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

  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两部分

  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函数故名思意,它主要用于把屏幕上的坐标转换到窗口客户上对应坐标

  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; // 绝对坐标
  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);

鼠标右键单击(静默):

  1. mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 1, 0);

鼠标左键双击(静默):

  1. mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 2, 0);

鼠标移动(相对位置):

  1. mouse_event(MOUSEEVENTF_MOVE, 100, 50, 0, 0);

鼠标移动(绝对位置):

  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. C# 模拟鼠标移动与点击

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

  3. C#__ 模拟鼠标单击事件

    首先要用到的引用有 [DllImport("User32")] public extern static void mouse_event(int dwFlags, int dx, ...

  4. delphi 在桌面屏幕上模拟鼠标单击

    delphi 在桌面屏幕上模拟鼠标单击 procedure TFrmUnicom.Button1Click(Sender: TObject); var oldPoint, newPoint: TPoi ...

  5. 零基础逆向工程40_Win32_14_枚举窗口_模拟鼠标键盘

    1 查找窗口 1.1 代码案例 //查找指定窗口 TCHAR szTitle[MAX_PATH] = {0}; HWND hwnd = ::FindWindow(TEXT("#32770&q ...

  6. 使用powershell/vbs自动化模拟鼠标点击操作

    今天想做windows上的自动化,所以才有了模拟鼠标点击的需求,先考虑用powershell实现: 首先先安装一个名为“WASP”免费可用的Powershell扩展程序,下载地址:http://was ...

  7. python + selenium webdriver 通过python来模拟鼠标、键盘操作,来解决SWFFileUpload调用系统底层弹出框无法定位问题

    Webdriver是基于浏览器操作的,当页面上传文件使用的是flash的控件SWFFileUpload调用的时候,调用的是系统底层的文件选择弹出框 这种情况,Webdriver暂时是不支持除页面外的其 ...

  8. C# 模拟鼠标移动和点击

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

  9. C#模拟鼠标、键盘操作

    C语言 在程序中打开网页,模拟鼠标点击.键盘输入 一.简述         记--使用C语言 打开指定网页,并模拟鼠标点击.键盘输入.实现半自动填写账号密码,并登录网站(当然现在的大部分网站都有验证码 ...

随机推荐

  1. 关于loadrunner录制不跳转到IE

    我是一个新手,对于这个问题,我已经愁了两周左右,因为是自学,一直没人教,靠自己百度也一直解决不了. 今天,我总算解决了这个问题. 我之前是ie8,根据网上说的启动IE----工具---Internet ...

  2. Spring的定时任务配置(转)

    spring的定时任务配置分为三个步骤: 1.定义任务 2.任务执行策略配置 3.启动任务 1.定义任务 <!--要定时执行的方法--> <bean id="testTas ...

  3. log4j2自定义输出线程环境信息

    在配置日志的输出格式的时候,我们可以按照内置的规则输出日志,但是有时候需要及时输出我们自定义的信息,这时需要借助ThreadContext类. ThreadContext类类似于MDC和NDC,它是一 ...

  4. 解决cocos2dx在Xcode中运行时报:convert: iCCP: known incorrect sRGB profile 的问题

    解决cocos2dx在Xcode中运行时报:convert: iCCP: known incorrect sRGB profile 的问题 本文的实践来源是参照了两个帖子完成的: http://dis ...

  5. [转]Java中的回车换行符/n /r /t

    '\r'是回车,'\n'是换行,前者使光标到行首,后者使光标下移一格.通常用的Enter是两个加起来.下面转一篇文章. 回车和换行 今天,我总算搞清楚"回车"(carriage r ...

  6. Deis logo 开源PaaS系统 Deis

    Deis 是一个 Django/Celery API 服务器.Python CLI 和一组 Chef cookbooks 合并起来提供一个类似 Heroku 的应用平台,用于公有云和私有云.Deis ...

  7. linux基本命令--学习记录

    1.mkdir -p 递归创建目录(-p参数代表递归创建): 2.touch 创建file: 3.cat 查看 5.vi 编辑器 6.echo >(直接覆盖) 或者<<(后面追加)单 ...

  8. 【Leetcode】【Medium】Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  9. 想抛就抛:Application_Error中统一处理ajax请求执行中抛出的异常

    女朋友不是想抛就抛,但异常却可以,不信请往下看. 今天在MVC Controller中写代码时,纠结了一下: public async Task<ActionResult> Save(in ...

  10. 基于QT的webkit与ExtJs开发CB/S结构的企业应用管理系统

      一:源起       1.何为CB/S的应用程序       C/S结构的应用程序,是客户端/服务端形式的应用程序,这种应用程序要在客户电脑上安装一个程序,客户使用这个程序与服务端通信,完成一定的 ...