using System; using System.Drawing; using System.Runtime.InteropServices; using System.Security.Permissions; using System.Windows.Forms; namespace Vicky.Helpers { /// <summary> /// 模拟鼠标点击 /// </summary> [HostProtection(SecurityAction.LinkDemand, Resources = HostProtectionResource.ExternalProcessMgmt)] public static class Mouse { #region 鼠标操作函数 [DllImport("user32.dll")] private static extern void mouse_event(MouseEvent flags, int dx, int dy, uint data, UIntPtr extraInfo); /// <summary> /// 连续两次鼠标单击之间会被处理成双击事件的间隔时间。 /// </summary> /// <returns>以毫秒表示的双击时间</returns> [DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")] public static extern int GetDoubleClickTime();/// <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); [Flags] private enum MouseEvent : 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 } #endregion #region 鼠标相关属性 /// <summary> /// 检查鼠标是否已经安装. /// </summary> public static bool Present { get { return SystemInformation.MousePresent; } } /// <summary> /// 检查鼠标是否存在滚轮 /// </summary> public static bool WheelPresent { get { if (!Present) { throw new InvalidOperationException("没有找到鼠标."); } return SystemInformation.MouseWheelPresent; } } /// <summary> /// 获取鼠标滚轮每次滚动的行数 /// </summary> public static int WheelScrollLines { get { if (!WheelPresent) { throw new InvalidOperationException("没有找到鼠标滑轮."); } return SystemInformation.MouseWheelScrollLines; } } #endregion #region 封装函数 /// <summary> /// 在当前鼠标的位置左键点击一下 /// </summary> public static void MouseClick() { mouse_event(MouseEvent.LeftDown, 0, 0, 0, UIntPtr.Zero); mouse_event(MouseEvent.LeftUp, 0, 0, 0, UIntPtr.Zero); } /// <summary> /// 移动到坐标位置点击 /// </summary> /// <param name="location">要点击的坐标位置,屏幕绝对值</param> public static void MouseClick(Point location) { MouseMove(location); mouse_event(MouseEvent.LeftDown, 0, 0, 0, UIntPtr.Zero); mouse_event(MouseEvent.LeftUp, 0, 0, 0, UIntPtr.Zero); } /// <summary> /// 移动到坐标位置点击 /// </summary> /// <param name="location">要点击的坐标位置,屏幕绝对值</param> public static void MouseRightClick(Point location) { MouseMove(location); mouse_event(MouseEvent.RightDown, 0, 0, 0, UIntPtr.Zero); mouse_event(MouseEvent.RightUp, 0, 0, 0, UIntPtr.Zero); } /// <summary> /// 移动到坐标位置 /// </summary> /// <param name="location">要移动到的坐标位置,屏幕绝对值</param> public static void MouseMove(Point location) { SetCursorPos(location.X, location.Y); } #endregion } }
测试类
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Vicky.Helpers.TestBenches { internal class MouseTestBench : TestBench { Point point = new Point(0, 0); internal override void Test() { Console.WriteLine(Mouse.Present ? "Mouse presents" : "Mouse does not present."); Console.WriteLine(Mouse.WheelPresent ? "Mouse wheel presents" : "Mouse wheel does not present."); Console.WriteLine($"Mouse wheel scrollLines: {Mouse.WheelScrollLines}."); Console.WriteLine($"Mouse double-click time: {Mouse.GetDoubleClickTime()}."); Mouse.MouseClick(); Console.WriteLine($"Mouse left-clicked at location: ({point.X}, {point.Y})"); point = new Point(10, 10); Mouse.MouseClick(point); Console.WriteLine($"Mouse left-clicked at location: ({point.X}, {point.Y})"); point = new Point(400, 300); Mouse.MouseRightClick(point); Console.WriteLine($"Mouse right-clicked at the location: ({point.X}, {point.Y})"); point = new Point(200, 200); Mouse.MouseMove(point); Console.WriteLine($"Mouse moved to the location: ({point.X}, {point.Y})"); } } }
标签:鼠标,point,C#,System,static,location,using,Mouse,模拟 From: https://www.cnblogs.com/vicky2021/p/17067237.html