限制鼠标移动范围[类]
工作需要,须限制鼠标移动范围,类如下:
调用如下:
1 一个鼠标类#region 一个鼠标类
2 /**//// <summary>
3 /// Mouse Control
4 /// Made by Michael
5 /// date 2008-01-30
6 /// </summary>
7 class Mouse
8 {
9 internal const byte SM_MOUSEPRESENT = 19;
10 internal const byte SM_CMOUSEBUTTONS = 43;
11 internal const byte SM_MOUSEWHEELPRESENT = 75;
12
13 internal struct POINTAPI
14 {
15 internal int x;
16 internal int y;
17 }
18
19 internal struct RECT
20 {
21 internal int left;
22 internal int top;
23 internal int right;
24 internal int bottom;
25 }
26
27 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SwapMouseButton")]
28 internal extern static int SwapMouseButton(int bSwap);
29
30 [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "ClipCursor")]
31 internal extern static int ClipCursor(ref RECT lpRect);
32
33 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetCursorPos")]
34 internal extern static int GetCursorPos(ref POINTAPI lpPoint);
35
36 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "ShowCursor")]
37 internal extern static bool ShowCursor(bool bShow);
38
39 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "EnableWindow")]
40 internal extern static int EnableWindow(int hwnd, int fEnable);
41
42 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetWindowRect")]
43 internal extern static int GetWindowRect(int hwnd, ref RECT lpRect);
44
45 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetCursorPos")]
46 internal extern static int SetCursorPos(int x, int y);
47
48 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
49 internal extern static int GetSystemMetrics(int nIndex);
50
51 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetDoubleClickTime")]
52 internal extern static int SetDoubleClickTime(int wCount);
53
54 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")]
55 internal extern static int GetDoubleClickTime();
56
57 [System.Runtime.InteropServices.DllImport("kernel32.DLL", EntryPoint = "Sleep")]
58 internal extern static void Sleep(int dwMilliseconds);
59
60 //得到鼠标相对与全屏的坐标,不是相对与你的Form的,且与你的分辨率有关系
61
62 public static int FullScreenPosition_X
63 {
64 get
65 {
66 POINTAPI _POINTAPI = new POINTAPI();
67
68 GetCursorPos(ref _POINTAPI);
69
70 return _POINTAPI.x;
71 }
72 }
73
74 public static int FullScreenPosition_Y
75 {
76 get
77 {
78 POINTAPI _POINTAPI = new POINTAPI();
79
80 GetCursorPos(ref _POINTAPI);
81
82 return _POINTAPI.y;
83 }
84 }
85
86 //隐藏 显示 鼠标
87 public static void Hide()
88 {
89 ShowCursor(false);
90 }
91
92 public static void Show()
93 {
94 ShowCursor(true);
95 }
96
97 //将鼠标锁定在你的Form里 不过你得将你的Form先锁了,Form Resize 就失效了
98 public static void Lock(System.Windows.Forms.Form ObjectForm)
99 {
100 RECT _FormRect = new RECT();
101
102 GetWindowRect(ObjectForm.Handle.ToInt32(), ref _FormRect);
103
104 ClipCursor(ref _FormRect);
105 }
106
107 public static void UnLock()
108 {
109 RECT _ScreenRect = new RECT();
110
111 _ScreenRect.top = 0;
112 _ScreenRect.left = 0;
113 _ScreenRect.bottom = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom;
114 _ScreenRect.right = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right;
115
116 ClipCursor(ref _ScreenRect);
117 }
118
119 //鼠标失效,不过失效的好像不只是鼠标,小心哦
120 public static void Disable(System.Windows.Forms.Form ObjectForm)
121 {
122 EnableWindow(ObjectForm.Handle.ToInt32(), 0);
123 }
124
125 public static void Enable(System.Windows.Forms.Form ObjectForm)
126 {
127 EnableWindow(ObjectForm.Handle.ToInt32(), 1);
128 }
129 // 得到你的鼠标类型
130 public static string Type
131 {
132 get
133 {
134 if (GetSystemMetrics(SM_MOUSEPRESENT) == 0)
135 {
136 return "本计算机尚未安装鼠标";
137 }
138 else
139 {
140 if (GetSystemMetrics(SM_MOUSEWHEELPRESENT) != 0)
141 {
142 return GetSystemMetrics(SM_CMOUSEBUTTONS) + "键滚轮鼠标";
143 }
144 else
145 {
146 return GetSystemMetrics(SM_CMOUSEBUTTONS) + "键鼠标";
147 }
148 }
149 }
150 }
151
152 // 设置鼠标双击时间
153 public static void DoubleClickTime_Set(int MouseDoubleClickTime)
154 {
155 SetDoubleClickTime(MouseDoubleClickTime);
156 }
157
158 public static string DoubleClickTime_Get()
159 {
160 return GetDoubleClickTime().ToString();
161 }
162
163 //设置鼠标默认主键
164 public static void DefaultRightButton()
165 {
166 SwapMouseButton(1);
167 }
168
169 public static void DefaultLeftButton()
170 {
171 SwapMouseButton(0);
172 }
173 }
2 /**//// <summary>
3 /// Mouse Control
4 /// Made by Michael
5 /// date 2008-01-30
6 /// </summary>
7 class Mouse
8 {
9 internal const byte SM_MOUSEPRESENT = 19;
10 internal const byte SM_CMOUSEBUTTONS = 43;
11 internal const byte SM_MOUSEWHEELPRESENT = 75;
12
13 internal struct POINTAPI
14 {
15 internal int x;
16 internal int y;
17 }
18
19 internal struct RECT
20 {
21 internal int left;
22 internal int top;
23 internal int right;
24 internal int bottom;
25 }
26
27 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SwapMouseButton")]
28 internal extern static int SwapMouseButton(int bSwap);
29
30 [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "ClipCursor")]
31 internal extern static int ClipCursor(ref RECT lpRect);
32
33 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetCursorPos")]
34 internal extern static int GetCursorPos(ref POINTAPI lpPoint);
35
36 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "ShowCursor")]
37 internal extern static bool ShowCursor(bool bShow);
38
39 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "EnableWindow")]
40 internal extern static int EnableWindow(int hwnd, int fEnable);
41
42 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetWindowRect")]
43 internal extern static int GetWindowRect(int hwnd, ref RECT lpRect);
44
45 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetCursorPos")]
46 internal extern static int SetCursorPos(int x, int y);
47
48 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
49 internal extern static int GetSystemMetrics(int nIndex);
50
51 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetDoubleClickTime")]
52 internal extern static int SetDoubleClickTime(int wCount);
53
54 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")]
55 internal extern static int GetDoubleClickTime();
56
57 [System.Runtime.InteropServices.DllImport("kernel32.DLL", EntryPoint = "Sleep")]
58 internal extern static void Sleep(int dwMilliseconds);
59
60 //得到鼠标相对与全屏的坐标,不是相对与你的Form的,且与你的分辨率有关系
61
62 public static int FullScreenPosition_X
63 {
64 get
65 {
66 POINTAPI _POINTAPI = new POINTAPI();
67
68 GetCursorPos(ref _POINTAPI);
69
70 return _POINTAPI.x;
71 }
72 }
73
74 public static int FullScreenPosition_Y
75 {
76 get
77 {
78 POINTAPI _POINTAPI = new POINTAPI();
79
80 GetCursorPos(ref _POINTAPI);
81
82 return _POINTAPI.y;
83 }
84 }
85
86 //隐藏 显示 鼠标
87 public static void Hide()
88 {
89 ShowCursor(false);
90 }
91
92 public static void Show()
93 {
94 ShowCursor(true);
95 }
96
97 //将鼠标锁定在你的Form里 不过你得将你的Form先锁了,Form Resize 就失效了
98 public static void Lock(System.Windows.Forms.Form ObjectForm)
99 {
100 RECT _FormRect = new RECT();
101
102 GetWindowRect(ObjectForm.Handle.ToInt32(), ref _FormRect);
103
104 ClipCursor(ref _FormRect);
105 }
106
107 public static void UnLock()
108 {
109 RECT _ScreenRect = new RECT();
110
111 _ScreenRect.top = 0;
112 _ScreenRect.left = 0;
113 _ScreenRect.bottom = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom;
114 _ScreenRect.right = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right;
115
116 ClipCursor(ref _ScreenRect);
117 }
118
119 //鼠标失效,不过失效的好像不只是鼠标,小心哦
120 public static void Disable(System.Windows.Forms.Form ObjectForm)
121 {
122 EnableWindow(ObjectForm.Handle.ToInt32(), 0);
123 }
124
125 public static void Enable(System.Windows.Forms.Form ObjectForm)
126 {
127 EnableWindow(ObjectForm.Handle.ToInt32(), 1);
128 }
129 // 得到你的鼠标类型
130 public static string Type
131 {
132 get
133 {
134 if (GetSystemMetrics(SM_MOUSEPRESENT) == 0)
135 {
136 return "本计算机尚未安装鼠标";
137 }
138 else
139 {
140 if (GetSystemMetrics(SM_MOUSEWHEELPRESENT) != 0)
141 {
142 return GetSystemMetrics(SM_CMOUSEBUTTONS) + "键滚轮鼠标";
143 }
144 else
145 {
146 return GetSystemMetrics(SM_CMOUSEBUTTONS) + "键鼠标";
147 }
148 }
149 }
150 }
151
152 // 设置鼠标双击时间
153 public static void DoubleClickTime_Set(int MouseDoubleClickTime)
154 {
155 SetDoubleClickTime(MouseDoubleClickTime);
156 }
157
158 public static string DoubleClickTime_Get()
159 {
160 return GetDoubleClickTime().ToString();
161 }
162
163 //设置鼠标默认主键
164 public static void DefaultRightButton()
165 {
166 SwapMouseButton(1);
167 }
168
169 public static void DefaultLeftButton()
170 {
171 SwapMouseButton(0);
172 }
173 }
调用如下:
1 //锁窗体
2 Mouse.Lock(this);
3 //锁光标
4 Mouse.Disable(this);
2 Mouse.Lock(this);
3 //锁光标
4 Mouse.Disable(this);