本例是C#编写的,演示如果设置鼠标Hook,拦截鼠标操作。

Code
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8
using System.Runtime.InteropServices;
9
10
namespace Hook
11

{
12
public partial class Form4 : Form
13
{
14
public delegate IntPtr HookProc(int nCode, Int32 wParam, IntPtr lParam);
15
16
[StructLayout(LayoutKind.Sequential)]
17
public class POINT
18
{
19
public int x;
20
public int y;
21
}
22
23
[StructLayout(LayoutKind.Sequential)]
24
public class MouseHookStruct
25
{
26
public POINT pt;
27
public int hwnd;
28
public int wHitTestCode;
29
public int dwExtraInfo;
30
}
31
32
Win32 API#region Win32 API
33
34
//设置挂钩
35
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
36
public static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
37
//调用下一个钩子
38
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
39
public static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, Int32 wParam, IntPtr lParam);
40
//移除挂钩
41
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
42
public static extern bool UnhookWindowsHookEx(IntPtr idHook);
43
//获取当前进程的编号
44
[DllImport("kernel32.dll")]
45
public static extern int GetCurrentThreadId();
46
//获取按键的状态
47
[DllImport("user32")]
48
public static extern short GetKeyState(int vKey);
49
50
#endregion
51
52
private IntPtr hookMouse = IntPtr.Zero;
53
private HookProc hpMouse;
54
55
private const int WH_MOUSE = 7;
56
private const int WM_MOUSEMOVE = 0x200;
57
private const int WM_LBUTTONDOWN = 0x201;
58
private const int WM_LBUTTONUP = 0x202;
59
private const int WM_LBUTTONDBLCLK = 0x203;
60
private const int WM_RBUTTONDOWN = 0x204;
61
private const int WM_RBUTTONUP = 0x205;
62
private const int WM_RBUTTONDBLCLK = 0x206;
63
private const int WM_MBUTTONDOWN = 0x207;
64
private const int WM_MBUTTONUP = 0x208;
65
private const int WM_MBUTTONDBLCLK = 0x209;
66
private const int WM_MOUSEWHEEL = 0x020A;
67
68
public event MouseEventHandler MouseEvent;
69
70
public Form4()
71
{
72
InitializeComponent();
73
74
hpMouse = new HookProc(MouseHookProc);
75
hookMouse = SetWindowsHookEx(WH_MOUSE, hpMouse, (IntPtr)0, GetCurrentThreadId());
76
77
this.MouseEvent += new MouseEventHandler(Form4_MouseEvent);
78
}
79
80
void Form4_MouseEvent(object sender, MouseEventArgs e)
81
{
82
this.Text = "X:" + e.X.ToString() +
83
" Y:" + e.Y.ToString() +
84
" Button:" + e.Button.ToString() +
85
" Clicks:" + e.Clicks.ToString() +
86
" Delta:" + e.Delta.ToString();
87
}
88
89
private IntPtr MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)
90
{
91
MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
92
93
if (nCode < 0)
94
{
95
return CallNextHookEx(hookMouse, nCode, wParam, lParam);
96
}
97
98
if (MouseEvent != null)
99
{
100
MouseButtons button = MouseButtons.None;
101
int clicks = 0;
102
int x = MyMouseHookStruct.pt.x;
103
int y = MyMouseHookStruct.pt.y;
104
int delta = 0;
105
106
switch (wParam)
107
{
108
case WM_LBUTTONDOWN:
109
{
110
button = MouseButtons.Left;
111
clicks = 1;
112
break;
113
}
114
case WM_LBUTTONUP:
115
{
116
button = MouseButtons.Left;
117
clicks = 1;
118
break;
119
}
120
case WM_LBUTTONDBLCLK:
121
{
122
button = MouseButtons.Left;
123
clicks = 2;
124
break;
125
}
126
case WM_RBUTTONDOWN:
127
{
128
button = MouseButtons.Right;
129
clicks = 1;
130
break;
131
}
132
case WM_RBUTTONUP:
133
{
134
button = MouseButtons.Right;
135
clicks = 1;
136
break;
137
}
138
case WM_RBUTTONDBLCLK:
139
{
140
button = MouseButtons.Right;
141
clicks = 2;
142
break;
143
}
144
case WM_MBUTTONDOWN:
145
{
146
button = MouseButtons.Middle;
147
clicks = 1;
148
break;
149
}
150
case WM_MBUTTONUP:
151
{
152
button = MouseButtons.Middle;
153
clicks = 1;
154
break;
155
}
156
case WM_MBUTTONDBLCLK:
157
{
158
button = MouseButtons.Middle;
159
clicks = 2;
160
break;
161
}
162
case WM_MOUSEWHEEL:
163
{
164
button = MouseButtons.Middle;
165
delta = 120;//好像没法判断滚轮方向
166
break;
167
}
168
}
169
170
MouseEventArgs e = new MouseEventArgs(button, clicks, x, y, delta);
171
MouseEvent(this, e);
172
}
173
return CallNextHookEx(hookMouse, nCode, wParam, lParam);
174
}
175
176
private void Form4_FormClosing(object sender, FormClosingEventArgs e)
177
{
178
UnhookWindowsHookEx(hookMouse);
179
hookMouse = IntPtr.Zero;
180
}
181
}
182
}