当前位置: 首页 > news >正文

C# 模拟鼠标操作工具类

写在前面

用WinForm做RPA项目时经常需要模拟鼠标操作,通过调用Windows Api 可以实现控制鼠标的移动、点击以及滚轮滚动,做到跟人工一样的操作。

代码实现

    public static class MouseKeyController{[DllImport("user32")]private static extern int mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);[DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]private static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);[DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]private static extern int PostMessage(IntPtr hWnd, int Msg, Keys wParam, int lParam);#region 屏幕分辨率和缩放百分比[DllImport("user32.dll")]private static extern IntPtr GetDC(IntPtr ptr);[DllImport("gdi32.dll")]private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);[DllImport("user32.dll", EntryPoint = "ReleaseDC")]private static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);const int HORZRES = 8;const int VERTRES = 10;const int LOGPIXELSX = 88;const int LOGPIXELSY = 90;const int DESKTOPVERTRES = 117;const int DESKTOPHORZRES = 118;/// <summary>/// 获取真实屏幕分辨率大小/// </summary>public static Size DESKTOP{get {IntPtr hdc = GetDC(IntPtr.Zero);Size size = new Size();size.Width = GetDeviceCaps(hdc, DESKTOPHORZRES);size.Height = GetDeviceCaps(hdc, DESKTOPVERTRES);ReleaseDC(IntPtr.Zero, hdc);return size;}}#endregion//移动鼠标 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;//模拟鼠标滚轮滚动操作,必须配合dwData参数const int MOUSEEVENTF_WHEEL = 0x0800;private static int screenWidth = 0;private static int screenHeight = 0;public static void Init(){screenWidth = Screen.PrimaryScreen.Bounds.Width;screenHeight = Screen.PrimaryScreen.Bounds.Height;}public static void MoveMouseWheel(int offset){mouse_event(MOUSEEVENTF_WHEEL, 0, 0, offset, 0);//鼠标滚动,使界面向下滚动offset的高度}public static void MoveMousePoint(int x, int y){ mouse_event(MOUSEEVENTF_MOVE, x, y, 0, 0);//相对当前鼠标位置x轴和y轴分别移动50像素}public static void SetMousePoint(int x, int y){// 需要说明的是,如果没有使用MOUSEEVENTF_ABSOLUTE,函数默认的是相对于鼠标当前位置的点,如果dx,和dy,用0,0表示,这函数认为是当前鼠标所在的点。mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x * 65536 / screenWidth, y * 65536 / screenHeight, 0, 0);}public static void MouseLeftDown(){mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);}public static void MouseLeftUp(){mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);}public static void MouseLeftClick(){mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);}public static void MouseRightClick(){mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);}}

总结

已在项目中实际应用过了,真实可靠,可放心使用。

http://www.lryc.cn/news/248121.html

相关文章:

  • content_script.js、background.js和popup.js之间的通讯
  • python的requests请求参数带files
  • Elk:filebeat 日志收集工具和logstash
  • [设计模式] 常见的设计模式
  • 报错解决:You may need an additional loader to handle the result of these loaders.
  • 配置自动化部署Jenkins和Gitea
  • VSCODE+QEMU+WSL调试RISCV代码(SBI、kernel)
  • 二叉树(判断是否为对称二叉树)
  • STM32开发学习(地址映射)
  • 证明E(X+Y) =E(X) + E(Y)
  • ClickHouse入门手册1.0
  • 10个火爆的设计素材网站推荐
  • SQL注入 - CTF常见题型
  • android keylayout键值适配
  • python读取excel自动化生成sql建表语句和java实体类字段
  • Unity求向量A在平面L上的投影向量
  • 人机交互2——任务型多轮对话的控制和生成
  • 【数据结构】八大排序 (三)
  • Redis 命令处理过程
  • python爬虫进阶教程之如何正确的使用cookie
  • 【hacker送书第4期】推荐4本Java必读书籍(各送一本)
  • [密码学]DES
  • 15个超级实用的Python操作,肯定有你意想不到的!
  • GitHub上8个强烈推荐的 Python 项目
  • 什么是依赖倒置原则
  • 异常数据检测 | Python实现oneclassSVM模型异常数据检测
  • using meta-SQL 使用元SQL (3)
  • Spinnaker 基于 docker registry 触发部署
  • 2023亚马逊云科技re:Invent,在开发者板块探究如何利用技术重塑业务
  • JAVA 使用stream流将List中的对象某一属性创建新的List