#region using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; #endregion namespace CSFrameworkV5.UserCustom { public class GZLockMouse { /// /// 指定区域锁定鼠标 /// /// /// [DllImport("User32")] public static extern int ClipCursor(ref Rect r); /// /// 获得指定句柄RECT结构 /// /// /// /// [DllImport("User32")] public static extern int GetWindowRect(int h, ref Rect r); /// /// 锁定鼠标 /// /// /// public static void Lock(Point Start, Point End) { var GZRect = new Rect(); GZRect.Left = Start.X; GZRect.Top = Start.Y; GZRect.Right = End.X; GZRect.Bottom = End.Y; ClipCursor(ref GZRect); } /// /// 锁定鼠标在指定句柄区域 /// /// public static void Lock(int Handle) { var formRect = new Rect(); GetWindowRect(Handle, ref formRect); ClipCursor(ref formRect); } /// /// 锁定鼠标在指定RECT结构 /// /// internal static void Lock(Rect rt) { ClipCursor(ref rt); } /// /// 释放鼠标 /// /// /// public static void Unlock() { var GZRect = new Rect(); GZRect.Left = 0; GZRect.Top = 0; GZRect.Bottom = Screen.PrimaryScreen.Bounds.Bottom; GZRect.Right = Screen.PrimaryScreen.Bounds.Right; ClipCursor(ref GZRect); } public struct Rect { public int Left; public int Top; public int Right; public int Bottom; } } }