using System.Runtime.InteropServices; namespace Common.system { public class MouseEventCommon { //[DllImport("user32.dll")] //static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo); [DllImport("user32")] private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); /// /// 设置鼠标的坐标 /// /// 横坐标 /// 纵坐标 /// [DllImport("user32.dll")] public static extern int SetCursorPos(int x, int y); /// /// 获取鼠标的坐标 /// /// 获取成功返回真 [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern bool GetCursorPos(out Point pt); /// /// 鼠标点击左键 /// /// 坐标x /// 坐标Y /// public static void MouseLefClickEvent(int dx, int dy, int data) { GetCursorPos(out Point pointRecord); mouse_event((int)MouseEventFlag.MiddleDown, pointRecord.X, pointRecord.Y, data, 0); mouse_event((int)MouseEventFlag.MiddleUp, pointRecord.X, pointRecord.Y, data, 0); } public static void MouseText(int dx, int dy) { mouse_event((int)MouseEventFlag.LeftDown, dx, dy, 0, 0); mouse_event((int)MouseEventFlag.LeftUp, dx, dy, 0, 0); } /// /// 鼠标点击中中键 /// /// public static void MouseMiddleClickEvent(int data) { GetCursorPos(out Point pointRecord); mouse_event((int)MouseEventFlag.MiddleDown, pointRecord.X, pointRecord.Y, data, 0); mouse_event((int)MouseEventFlag.MiddleUp, pointRecord.X, pointRecord.Y, data, 0); } public struct Point { public int X; public int Y; public Point(int x, int y) { this.X = x; this.Y = y; } } 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 } } }