1

7
8
namespace Windows.Forms.Base
9
{
10
public class Mouse
11
{
12
MouseEventFlag enum
32
33
internal const byte SM_CMOUSEBUTTONS = 43;
34
internal const byte SM_MOUSEPRESENT = 19;
35
internal const byte SM_MOUSEWHEELPRESENT = 75;
36
37
public static int FullScreenPosition_X
38
{
39
get
40
{
41
POINTAPI _POINTAPI = new POINTAPI();
42
GetCursorPos(ref _POINTAPI);
43
return _POINTAPI.x;
44
}
45
}
46
47
public static int FullScreenPosition_Y
48
{
49
get
50
{
51
POINTAPI _POINTAPI = new POINTAPI();
52
GetCursorPos(ref _POINTAPI);
53
return _POINTAPI.y;
54
}
55
}
56
57
public static string Type
58
{
59
get
60
{
61
if (GetSystemMetrics(SM_MOUSEPRESENT) == 0)
62
{
63
return "本计算机尚未安装鼠标";
64
}
65
if (GetSystemMetrics(SM_MOUSEWHEELPRESENT) != 0)
66
{
67
return GetSystemMetrics(SM_CMOUSEBUTTONS) + "键滚轮鼠标";
68
}
69
return GetSystemMetrics(SM_CMOUSEBUTTONS) + "键鼠标";
70
}
71
}
72
73
/// <summary>
74
///鼠标左右键功能互换
75
/// </summary>
76
/// <param name="bSwap"></param>
77
/// <returns></returns>
78
[DllImport("user32.dll", EntryPoint = "SwapMouseButton")]
79
public static extern int SwapMouseButton(int bSwap);
80
81
/// <summary>
82
/// 鼠标的移动区域限制
83
/// 0为释放限制
84
/// </summary>
85
/// <param name="lpRect"></param>
86
/// <returns></returns>
87
[DllImport("user32", EntryPoint = "ClipCursor")]
88
public static extern int ClipCursor(ref RECT lpRect);
89
90
/// <summary>
91
/// 获取鼠标坐标
92
/// </summary>
93
/// <param name="lpPoint"></param>
94
/// <returns></returns>
95
[DllImport("user32.dll", EntryPoint = "GetCursorPos")]
96
public static extern int GetCursorPos(ref POINTAPI lpPoint);
97
98
/// <summary>
99
/// 显示和隐藏鼠标指针.
100
/// 1为显示0为隐藏
101
/// </summary>
102
/// <param name="bShow"></param>
103
/// <returns></returns>
104
[DllImport("user32.dll", EntryPoint = "ShowCursor")]
105
public static extern bool ShowCursor(bool bShow);
106
107
/// <summary>
108
/// 将非模态窗口显示为模态窗口
109
/// </summary>
110
/// <param name="hwnd"></param>
111
/// <param name="fEnable"></param>
112
/// <returns></returns>
113
[DllImport("user32.dll", EntryPoint = "EnableWindow")]
114
public static extern int EnableWindow(int hwnd, int fEnable);
115
116
/// <summary>
117
/// 获得窗口的大小
118
/// </summary>
119
/// <param name="hwnd"></param>
120
/// <param name="lpRect"></param>
121
/// <returns></returns>
122
[DllImport("user32.dll", EntryPoint = "GetWindowRect")]
123
public static extern int GetWindowRect(int hwnd, ref RECT lpRect);
124
125
/// <summary>
126
/// 设置鼠标坐标
127
/// </summary>
128
/// <param name="x"></param>
129
/// <param name="y"></param>
130
/// <returns></returns>
131
[DllImport("user32.dll", EntryPoint = "SetCursorPos")]
132
public static extern int SetCursorPos(int x, int y);
133
134
/// <summary>
135
/// 返回Win桌面中各种显示单元的宽度和高度、是否安装了鼠标、是否调换了鼠标左右键的定义等
136
/// </summary>
137
/// <param name="nIndex"></param>
138
/// <returns></returns>
139
[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
140
public static extern int GetSystemMetrics(int nIndex);
141
142
/// <summary>
143
/// 参数wCount,表示鼠标双击时间,为毫秒级,系统默认时间为500
144
/// </summary>
145
/// <param name="wCount"></param>
146
/// <returns></returns>
147
[DllImport("user32.dll", EntryPoint = "SetDoubleClickTime")]
148
public static extern int SetDoubleClickTime(int wCount);
149
150
/// <summary>
151
/// 该函数无参数;它的返回值为毫秒,为双击鼠标双击有效的时间间隔。
152
/// </summary>
153
/// <returns></returns>
154
[DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")]
155
public static extern int GetDoubleClickTime();
156
157
/// <summary>
158
/// 只是休眠而已
159
/// </summary>
160
/// <param name="dwMilliseconds"></param>
161
[DllImport("kernel32.DLL", EntryPoint = "Sleep")]
162
public static extern void Sleep(int dwMilliseconds);
163
164
/// <summary>
165
/// 模拟鼠标操作
166
/// 调用方法
167
/// mouse_event(MOUSEEVENTF_LEFTDOWN, X * 65536 / 1024, Y * 65536 / 768, 0, 0);
168
/// mouse_event(MOUSEEVENTF_LEFTUP, X * 65536 / 1024, Y * 65536 / 768, 0, 0);
169
/// 其中X,Y分别是你要点击的点的横坐标和纵坐标
170
/// </summary>
171
/// <param name="dwFlags"></param>
172
/// <param name="dx"></param>
173
/// <param name="dy"></param>
174
/// <param name="dwData"></param>
175
/// <param name="dwExtraInfo"></param>
176
[DllImport("user32.dll")]
177
public static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
178
179
// 隐藏 显示 鼠标
180
public static void Hide()
181
{
182
ShowCursor(false);
183
}
184
185
public static void Show()
186
{
187
ShowCursor(true);
188
}
189
190
// 将鼠标锁定在你的Form里 不过你得将你的Form先锁了,Form Resize 就失效了
191
public static void Lock(System.Windows.Forms.Form ObjectForm)
192
{
193
RECT _FormRect = new RECT();
194
GetWindowRect(ObjectForm.Handle.ToInt32(), ref _FormRect);
195
ClipCursor(ref _FormRect);
196
}
197
198
public static void UnLock()
199
{
200
RECT _ScreenRect = new RECT();
201
_ScreenRect.top = 0;
202
_ScreenRect.left = 0;
203
_ScreenRect.bottom = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom;
204
_ScreenRect.right = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right;
205
ClipCursor(ref _ScreenRect);
206
}
207
208
// 鼠标失效,不过失效的好像不只是鼠标,小心哦
209
public static void Disable(System.Windows.Forms.Form ObjectForm)
210
{
211
EnableWindow(ObjectForm.Handle.ToInt32(), 0);
212
}
213
214
public static void Enable(System.Windows.Forms.Form ObjectForm)
215
{
216
EnableWindow(ObjectForm.Handle.ToInt32(), 1);
217
}
218
219
// 鼠标自己移动
220
public static void Move(int From_Handle_ToInt32, int To_Handle_ToInt32)
221
{
222
RECT rectFrom = new RECT();
223
RECT rectTo = new RECT();
224
int i;
225
GetWindowRect(From_Handle_ToInt32, ref rectFrom);
226
GetWindowRect(To_Handle_ToInt32, ref rectTo);
227
if ((rectFrom.left + rectFrom.right)/2 - (rectTo.left + rectTo.right)/2 > 0)
228
{
229
for (i = (rectFrom.left + rectFrom.right)/2; i >= (rectTo.left + rectTo.right)/2; i--)
230
{
231
SetCursorPos(i, (rectFrom.top + rectFrom.bottom)/2);
232
Sleep(1);
233
}
234
}
235
else
236
{
237
for (i = (rectFrom.left + rectFrom.right)/2; i <= (rectTo.left + rectTo.right)/2; i++)
238
{
239
SetCursorPos(i, (rectFrom.top + rectFrom.bottom)/2);
240
Sleep(1);
241
}
242
}
243
if ((rectFrom.top + rectFrom.bottom)/2 - (rectTo.top + rectTo.bottom)/2 > 0)
244
{
245
for (i = (rectFrom.top + rectFrom.bottom)/2; i >= (rectTo.top + rectTo.bottom)/2; i--)
246
{
247
SetCursorPos((rectTo.left + rectTo.right)/2, i);
248
Sleep(1);
249
}
250
}
251
else
252
{
253
for (i = (rectFrom.top + rectFrom.bottom)/2; i <= (rectTo.top + rectTo.bottom)/2; i++)
254
{
255
SetCursorPos((rectTo.left + rectTo.right)/2, i);
256
Sleep(1);
257
}
258
}
259
}
260
261
// 得到你的鼠标类型
262
263
// 设置鼠标双击时间
264
public static void DoubleClickTime_Set(int MouseDoubleClickTime)
265
{
266
SetDoubleClickTime(MouseDoubleClickTime);
267
}
268
269
public static string DoubleClickTime_Get()
270
{
271
return GetDoubleClickTime().ToString();
272
}
273
274
// 设置鼠标默认主键
275
public static void DefaultRightButton()
276
{
277
SwapMouseButton(1);
278
}
279
280
public static void DefaultLeftButton()
281
{
282
SwapMouseButton(0);
283
}
284
285
Nested type: POINTAPI
294
295
Nested type: RECT
306
}
307
}


7

8

9

10

11

12

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

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

294

295

306

307
