C#模拟鼠标点击
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | /// <summary> /// 模拟鼠标点击 /// </summary> [HostProtection(SecurityAction.LinkDemand, Resources = HostProtectionResource.ExternalProcessMgmt)] public class MouseHelper { #region 鼠标相关属性 /// <summary> /// 检查鼠标是否已经安装. /// </summary> public static bool MousePresent { get { return SystemInformation.MousePresent; } } /// <summary> /// 检查鼠标是否存在滚轮 /// </summary> public static bool WheelExists { get { if (!SystemInformation.MousePresent) { throw new InvalidOperationException( "没有找到鼠标." ); } return SystemInformation.MouseWheelPresent; } } /// <summary> /// 获取鼠标滚轮每次滚动的行数 /// </summary> public static int WheelScrollLines { get { if (!WheelExists) { throw new InvalidOperationException( "没有找到鼠标滑轮." ); } return SystemInformation.MouseWheelScrollLines; } } #endregion #region 鼠标操作函数 [DllImport( "user32.dll" )] static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo); [Flags] enum MouseEventFlag : uint { Move = 0x0001, LeftDown = 0x0002, LeftUp = 0x0004, RightDown = 0x0008, RightUp = 0x0010, MiddleDown = 0x0020, MiddleUp = 0x0040, XDown = 0x0080, XUp = 0x0100, Wheel = 0x0800, VirtualDesk = 0x4000, Absolute = 0x8000 } /// <summary> /// 连续两次鼠标单击之间会被处理成双击事件的间隔时间。 /// </summary> /// <returns>以毫秒表示的双击时间</returns> [DllImport( "user32.dll" , EntryPoint = "GetDoubleClickTime" )] public static extern int GetDoubleClickTime(); /// <summary> /// 检取光标的位置,以屏幕坐标表示。 /// </summary> /// <param name="lpPoint">POINT结构指针,该结构接收光标的屏幕坐标。</param> /// <returns>如果成功,返回值非零;如果失败,返回值为零。</returns> [DllImport( "user32.dll" , EntryPoint = "GetCursorPos" )] public static extern int GetCursorPos(Point lpPoint); /// <summary> /// 把光标移到屏幕的指定位置。如果新位置不在由 ClipCursor函数设置的屏幕矩形区域之内,则系统自动调整坐标,使得光标在矩形之内。 /// </summary> /// <param name="x">指定光标的新的X坐标,以屏幕坐标表示。</param> /// <param name="y">指定光标的新的Y坐标,以屏幕坐标表示。</param> /// <returns>如果成功,返回非零值;如果失败,返回值是零</returns> [DllImport( "user32.dll" )] public static extern int SetCursorPos( int x, int y); #endregion #region 封装函数 /// <summary> /// 在当前鼠标的位置左键点击一下 /// </summary> public static void MouseClick() { mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero); mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero); } /// <summary> /// 移动到坐标位置点击 /// </summary> /// <param name="location">要点击的坐标位置,屏幕绝对值</param> public static void MouseClick(Point location) { MouseMove(location); mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero); mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero); } /// <summary> /// 移动到坐标位置点击 /// </summary> /// <param name="location">要点击的坐标位置,屏幕绝对值</param> public static void MouseRightClick(Point location) { MouseMove(location); mouse_event(MouseEventFlag.RightDown, 0, 0, 0, UIntPtr.Zero); mouse_event(MouseEventFlag.RightUp, 0, 0, 0, UIntPtr.Zero); } /// <summary> /// 移动到坐标位置 /// </summary> public static void MouseMove(Point location) { SetCursorPos(location.X, location.Y); } #endregion } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)