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

5
namespace convert6


{7
class Class48

{9

一个鼠标类#region 一个鼠标类10

11

12
internal const byte SM_MOUSEPRESENT = 19;13
internal const byte SM_CMOUSEBUTTONS = 43;14
internal const byte SM_MOUSEWHEELPRESENT = 75;15

16
internal struct POINTAPI17

{18
internal int x;19
internal int y;20
}21

22
internal struct RECT23

{24
internal int left;25
internal int top;26
internal int right;27
internal int bottom;28
}29

30
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SwapMouseButton")]31
internal extern static int SwapMouseButton(int bSwap);32

33
[System.Runtime.InteropServices.DllImport("user32", EntryPoint = "ClipCursor")]34
internal extern static int ClipCursor(ref RECT lpRect);35

36
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetCursorPos")]37
internal extern static int GetCursorPos(ref POINTAPI lpPoint);38

39
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "ShowCursor")]40
internal extern static bool ShowCursor(bool bShow);41

42
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "EnableWindow")]43
internal extern static int EnableWindow(int hwnd, int fEnable);44

45
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetWindowRect")]46
internal extern static int GetWindowRect(int hwnd, ref RECT lpRect);47

48
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetCursorPos")]49
internal extern static int SetCursorPos(int x, int y);50

51
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]52
internal extern static int GetSystemMetrics(int nIndex);53

54
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetDoubleClickTime")]55
internal extern static int SetDoubleClickTime(int wCount);56

57
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")]58
internal extern static int GetDoubleClickTime();59

60
[System.Runtime.InteropServices.DllImport("kernel32.DLL", EntryPoint = "Sleep")]61
internal extern static void Sleep(int dwMilliseconds);62

63
//得到鼠标相对与全屏的坐标,不是相对与你的Form的,且与你的分辨率有关系 64

65
public static int FullScreenPosition_X66

{67
get68

{69
POINTAPI _POINTAPI = new POINTAPI();70

71
GetCursorPos(ref _POINTAPI);72

73
return _POINTAPI.x;74
}75
}76

77
public static int FullScreenPosition_Y78

{79
get80

{81
POINTAPI _POINTAPI = new POINTAPI();82

83
GetCursorPos(ref _POINTAPI);84

85
return _POINTAPI.y;86
}87
}88

89
//隐藏 显示 鼠标 90
public static void Hide()91

{92
ShowCursor(false);93
}94

95
public static void Show()96

{97
ShowCursor(true);98
}99

100
//将鼠标锁定在你的Form里 不过你得将你的Form先锁了,Form Resize 就失效了 101
public static void Lock(System.Windows.Forms.Form ObjectForm)102

{103
RECT _FormRect = new RECT();104

105
GetWindowRect(ObjectForm.Handle.ToInt32(), ref _FormRect);106

107
ClipCursor(ref _FormRect);108
}109

110
public static void UnLock()111

{112
RECT _ScreenRect = new RECT();113

114
_ScreenRect.top = 0;115
_ScreenRect.left = 0;116
_ScreenRect.bottom = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom;117
_ScreenRect.right = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right;118

119
ClipCursor(ref _ScreenRect);120
}121

122
//鼠标失效,不过失效的好像不只是鼠标,小心哦 123
public static void Disable(System.Windows.Forms.Form ObjectForm)124

{125
EnableWindow(ObjectForm.Handle.ToInt32(), 0);126
}127

128
public static void Enable(System.Windows.Forms.Form ObjectForm)129

{130
EnableWindow(ObjectForm.Handle.ToInt32(), 1);131
}132
// 得到你的鼠标类型 133
public static string Type134

{135
get136

{137
if (GetSystemMetrics(SM_MOUSEPRESENT) == 0)138

{139
return "本计算机尚未安装鼠标";140
}141
else142

{143
if (GetSystemMetrics(SM_MOUSEWHEELPRESENT) != 0)144

{145
return GetSystemMetrics(SM_CMOUSEBUTTONS) + "键滚轮鼠标";146
}147
else148

{149
return GetSystemMetrics(SM_CMOUSEBUTTONS) + "键鼠标";150
}151
}152
}153
}154

155
// 设置鼠标双击时间156
public static void DoubleClickTime_Set(int MouseDoubleClickTime)157

{158
SetDoubleClickTime(MouseDoubleClickTime);159
}160

161
public static string DoubleClickTime_Get()162

{163
return GetDoubleClickTime().ToString();164
}165

166
//设置鼠标默认主键 一般都习惯用右手用鼠标 167
public static void DefaultRightButton()168

{169
SwapMouseButton(1);170
}171

172
public static void DefaultLeftButton()173

{174
SwapMouseButton(0);175
}176
}177
#endregion178

179
}180

使用如下:
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Windows.Forms;
5![]()
6
namespace convert
7![]()
![]()
{
8
public class loadform:ApplicationContext
9![]()
{
10
public loadform()
11
:base()
12![]()
{
13
Form5 ff = new Form5();
14
15
ff.Show();
16
Class4.Lock(ff);
17
Class4.Enable(ff);
18
19
}
20
}
21
}
22![]()
注意,窗体须不能改变大小,可将窗体的FormBorderStyle 设置为None。
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Windows.Forms;5

6
namespace convert7


{8
public class loadform:ApplicationContext 9

{10
public loadform()11
:base()12

{13
Form5 ff = new Form5(); 14
15
ff.Show();16
Class4.Lock(ff);17
Class4.Enable(ff);18
19
}20
}21
}22

浙公网安备 33010602011771号