-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMouseHook.cs
More file actions
264 lines (236 loc) · 9.34 KB
/
MouseHook.cs
File metadata and controls
264 lines (236 loc) · 9.34 KB
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
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
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
namespace SpecialClick
{
/// <summary>
/// 鼠标钩子类 - 用于捕获系统级鼠标事件
/// 通过 Windows API 设置全局鼠标钩子,监听鼠标操作
/// </summary>
/// <remarks>
/// 伪代码:
/// 1. 定义鼠标事件常量(按下、抬起等)
/// 2. 设置全局鼠标钩子
/// 3. 处理捕获的鼠标消息
/// 4. 触发相应的事件给订阅者
/// </remarks>
class MouseHook
{
/// <summary>
/// 存储当前鼠标位置的私有变量
/// </summary>
private Point point;
/// <summary>
/// 鼠标位置属性 - 当位置改变时触发 MouseMoveEvent 事件
/// </summary>
private Point Point
{
get { return point; }
set
{
if (point != value)
{
point = value;
if (MouseMoveEvent != null)
{
var e = new MouseEventArgs(MouseButtons.None, 0, (int)point.X, (int)point.Y, 0);
MouseMoveEvent(this, e);
}
}
}
}
/// <summary>
/// 鼠标钩子句柄
/// </summary>
private int hHook;
//private static int hMouseHook = 0;
//private const int WM_MOUSEMOVE = 0x200;//鼠标移动
/// <summary>
/// 鼠标左键按下消息常量
/// </summary>
private const int WM_LBUTTONDOWN = 0x201; //鼠标左键按下
/// <summary>
/// 鼠标右键按下消息常量
/// </summary>
private const int WM_RBUTTONDOWN = 0x204; //鼠标右键按下
/// <summary>
/// 鼠标中键按下消息常量
/// </summary>
private const int WM_MBUTTONDOWN = 0x207; //鼠标中键按下
/// <summary>
/// 鼠标左键抬起消息常量
/// </summary>
private const int WM_LBUTTONUP = 0x202; //鼠标左键抬起
/// <summary>
/// 鼠标右键抬起消息常量
/// </summary>
private const int WM_RBUTTONUP = 0x205; //鼠标右键抬起
/// <summary>
/// 鼠标中键抬起消息常量
/// </summary>
private const int WM_MBUTTONUP = 0x208; //鼠标中键抬起
// private const int WM_LBUTTONDBLCLK = 0x203;
// private const int WM_RBUTTONDBLCLK = 0x206;
// private const int WM_MBUTTONDBLCLK = 0x209;
/// <summary>
/// 全局鼠标钩子类型常量
/// </summary>
public const int WH_MOUSE_LL = 14;
/// <summary>
/// 鼠标钩子处理程序委托
/// </summary>
public Win32Api.HookProc hProc;
/// <summary>
/// MouseHook 类的构造函数
/// 初始化鼠标位置
/// </summary>
/// <remarks>
/// 伪代码:
/// 1. 创建一个新的 Point 对象作为初始鼠标位置
/// </remarks>
public MouseHook()
{
this.Point = new Point();
}
/// <summary>
/// 设置鼠标钩子
/// 安装全局鼠标钩子以捕获鼠标事件
/// </summary>
/// <returns>钩子句柄,如果设置失败则返回 0</returns>
/// <remarks>
/// 伪代码:
/// 1. 将 MouseHookProc 方法赋值给 hProc 委托
/// 2. 调用 Win32Api.SetWindowsHookEx 设置低级鼠标钩子
/// 3. 返回钩子句柄
/// </remarks>
public int SetHook()
{
hProc = MouseHookProc;
hHook = Win32Api.SetWindowsHookEx(WH_MOUSE_LL, hProc, IntPtr.Zero, 0);
return hHook;
}
/// <summary>
/// 卸载鼠标钩子
/// 移除之前设置的全局鼠标钩子
/// </summary>
/// <remarks>
/// 伪代码:
/// 1. 调用 Win32Api.UnhookWindowsHookEx 函数卸载钩子
/// </remarks>
public void UnHook()
{
Win32Api.UnhookWindowsHookEx(hHook);
}
/// <summary>
/// 鼠标钩子处理程序
/// 处理捕获到的鼠标消息并触发相应的事件
/// </summary>
/// <param name="nCode">钩子代码,用于确定如何处理消息</param>
/// <param name="wParam">消息的 wParam 参数</param>
/// <param name="lParam">消息的 lParam 参数</param>
/// <returns>调用下一个钩子的返回值</returns>
/// <remarks>
/// 伪代码:
/// 1. 将 lParam 转换为 MouseHookStruct 结构体
/// 2. 检查 nCode 是否小于 0,如果是则直接调用下一个钩子
/// 3. 根据 wParam 的值判断鼠标操作类型(左键、右键、中键按下或抬起)
/// 4. 创建相应的 MouseButtons 枚举值和点击次数
/// 5. 触发对应的鼠标事件(MouseDownEvent 或 MouseUpEvent)
/// 6. 更新当前鼠标位置
/// 7. 调用下一个钩子处理程序
/// </remarks>
private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
Win32Api.MouseHookStruct MyMouseHookStruct = (Win32Api.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32Api.MouseHookStruct));
if (nCode < 0)
{
return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam);
}
else
{
MouseButtons button = MouseButtons.None;
int clickCount = 0;
switch ((Int32)wParam)
{
case WM_LBUTTONDOWN:
button = MouseButtons.Left;
clickCount = 1;
MouseDownEvent?.Invoke(this, new MouseEventArgs(button, clickCount, (int)point.X, (int)point.Y, 0));
break;
case WM_RBUTTONDOWN:
button = MouseButtons.Right;
clickCount = 1;
MouseDownEvent?.Invoke(this, new MouseEventArgs(button, clickCount, (int)point.X, (int)point.Y, 0));
break;
case WM_MBUTTONDOWN:
button = MouseButtons.Middle;
clickCount = 1;
MouseDownEvent?.Invoke(this, new MouseEventArgs(button, clickCount, (int)point.X, (int)point.Y, 0));
break;
case WM_LBUTTONUP:
button = MouseButtons.Left;
clickCount = 1;
MouseUpEvent?.Invoke(this, new MouseEventArgs(button, clickCount, (int)point.X, (int)point.Y, 0));
break;
case WM_RBUTTONUP:
button = MouseButtons.Right;
clickCount = 1;
MouseUpEvent?.Invoke(this, new MouseEventArgs(button, clickCount, (int)point.X, (int)point.Y, 0));
break;
case WM_MBUTTONUP:
button = MouseButtons.Middle;
clickCount = 1;
MouseUpEvent?.Invoke(this, new MouseEventArgs(button, clickCount, (int)point.X, (int)point.Y, 0));
break;
}
this.Point = new Point(MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y);
return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam);
}
}
/// <summary>
/// 鼠标移动事件委托定义
/// </summary>
/// <param name="sender">事件发送对象</param>
/// <param name="e">鼠标事件参数</param>
public delegate void MouseMoveHandler(object sender, MouseEventArgs e);
/// <summary>
/// 鼠标移动事件
/// 当鼠标位置改变时触发
/// </summary>
public event MouseMoveHandler MouseMoveEvent;
/// <summary>
/// 鼠标点击事件委托定义
/// </summary>
/// <param name="sender">事件发送对象</param>
/// <param name="e">鼠标事件参数</param>
public delegate void MouseClickHandler(object sender, MouseEventArgs e);
/// <summary>
/// 鼠标点击事件
/// 当鼠标点击时触发
/// </summary>
public event MouseClickHandler MouseClickEvent;
/// <summary>
/// 鼠标按下事件委托定义
/// </summary>
/// <param name="sender">事件发送对象</param>
/// <param name="e">鼠标事件参数</param>
public delegate void MouseDownHandler(object sender, MouseEventArgs e);
/// <summary>
/// 鼠标按下事件
/// 当鼠标按键按下时触发
/// </summary>
public event MouseDownHandler MouseDownEvent;
/// <summary>
/// 鼠标抬起事件委托定义
/// </summary>
/// <param name="sender">事件发送对象</param>
/// <param name="e">鼠标事件参数</param>
public delegate void MouseUpHandler(object sender, MouseEventArgs e);
/// <summary>
/// 鼠标抬起事件
/// 当鼠标按键抬起时触发
/// </summary>
public event MouseUpHandler MouseUpEvent;
}
}