1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
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
#region
 
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
 
#endregion
 
namespace CSFrameworkV5.UserCustom
{
    public class GZLockMouse
    {
        /// <summary>
        ///     指定区域锁定鼠标
        /// </summary>
        /// <param name="r"></param>
        /// <returns></returns>
        [DllImport("User32")]
        public static extern int ClipCursor(ref Rect r);
 
        /// <summary>
        ///     获得指定句柄RECT结构
        /// </summary>
        /// <param name="h"></param>
        /// <param name="r"></param>
        /// <returns></returns>
        [DllImport("User32")]
        public static extern int GetWindowRect(int h, ref Rect r);
 
        /// <summary>
        ///     锁定鼠标
        /// </summary>
        /// <param name="Start"></param>
        /// <param name="End"></param>
        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);
        }
 
        /// <summary>
        ///     锁定鼠标在指定句柄区域
        /// </summary>
        /// <param name="Handle"></param>
        public static void Lock(int Handle)
        {
            var formRect = new Rect();
            GetWindowRect(Handle, ref formRect);
            ClipCursor(ref formRect);
        }
 
        /// <summary>
        ///     锁定鼠标在指定RECT结构
        /// </summary>
        /// <param name="rt"></param>
        internal static void Lock(Rect rt)
        {
            ClipCursor(ref rt);
        }
 
        /// <summary>
        ///     释放鼠标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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;
        }
    }
}