/
MouseHook.cs
211 lines (173 loc) · 4.92 KB
/
MouseHook.cs
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
// Copyright (c) 2015, Ben Baker
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Reflection;
using System.Drawing;
using System.Diagnostics;
namespace CPWizard
{
public class MouseHook : IDisposable
{
private bool m_isDisposed = false;
private object m_tag = null;
private Win32.HookType m_hookType = Win32.HookType.WH_MOUSE_LL;
private IntPtr m_hookHandle = IntPtr.Zero;
private Win32.HookProc m_hookFunction = null;
public event EventHandler<MouseEventArgs> MouseEvent = null;
public MouseHook(object tag)
{
m_tag = tag;
m_hookFunction = new Win32.HookProc(HookCallback);
InstallHook();
}
~MouseHook()
{
Dispose(false);
}
private int HookCallback(int code, IntPtr wParam, IntPtr lParam)
{
if (code < 0 || MouseEvent == null)
return Win32.CallNextHookEx(m_hookHandle, code, wParam, lParam);
Win32.MOUSELLHOOKSTRUCT mouseData = (Win32.MOUSELLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(Win32.MOUSELLHOOKSTRUCT));
MouseButtons button = MouseButtons.None;
short mouseDelta = 0;
switch (wParam.ToInt32())
{
case Win32.WM_LBUTTONDOWN:
button = MouseButtons.Left;
break;
case Win32.WM_RBUTTONDOWN:
button = MouseButtons.Right;
break;
case Win32.WM_MOUSEWHEEL:
mouseDelta = (short)((mouseData.mouseData >> 16) & 0xffff);
break;
}
int clickCount = 0;
if (button != MouseButtons.None)
clickCount = (wParam.ToInt32() == Win32.WM_LBUTTONDBLCLK || wParam.ToInt32() == Win32.WM_RBUTTONDBLCLK) ? 2 : 1;
MouseEventArgs mouseEventArgs = new MouseEventArgs(button, clickCount, mouseData.pt.X, mouseData.pt.Y, mouseDelta, m_tag);
if (MouseEvent != null)
MouseEvent(this, mouseEventArgs);
return (mouseEventArgs.Handled ? 1 : Win32.CallNextHookEx(m_hookHandle, code, wParam, lParam));
}
private void InstallHook()
{
if (m_hookHandle != IntPtr.Zero)
return;
//m_hookHandle = SetWindowsHookEx(m_hookType, m_hookFunction, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
m_hookHandle = Win32.SetWindowsHookEx(m_hookType, m_hookFunction, Process.GetCurrentProcess().MainModule.BaseAddress, 0);
}
private void UnInstallHook()
{
if (m_hookHandle != IntPtr.Zero)
{
Win32.UnhookWindowsHookEx(m_hookHandle);
m_hookHandle = IntPtr.Zero;
}
}
#region IDisposable Members
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (m_isDisposed)
return;
if (disposing)
{
// Dispose managed resources
}
// Dispose unmanaged resources
UnInstallHook();
m_isDisposed = true;
}
#endregion
}
public class MouseEventArgs : EventArgs
{
public MouseButtons Button;
public int Clicks;
public int X;
public int Y;
public int Delta;
public object Tag;
public bool Handled = false;
public MouseEventArgs(MouseButtons button, int clicks, int x, int y, int delta, object tag)
{
Button = button;
Clicks = clicks;
X = x;
Y = y;
Delta = delta;
Tag = tag;
}
public Point Location
{
get { return new Point(X, Y); }
}
}
public partial class Win32
{
public const int WM_MOUSEMOVE = 0x200;
public const int WM_LBUTTONDOWN = 0x201;
public const int WM_RBUTTONDOWN = 0x204;
public const int WM_MBUTTONDOWN = 0x207;
public const int WM_LBUTTONUP = 0x202;
public const int WM_RBUTTONUP = 0x205;
public const int WM_MBUTTONUP = 0x208;
public const int WM_LBUTTONDBLCLK = 0x203;
public const int WM_RBUTTONDBLCLK = 0x206;
public const int WM_MBUTTONDBLCLK = 0x209;
public const int WM_MOUSEWHEEL = 0x020A;
/* public enum HookType : int
{
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_CALLWNDPROC = 4,
WH_CBT = 5,
WH_SYSMSGFILTER = 6,
WH_MOUSE = 7,
WH_HARDWARE = 8,
WH_DEBUG = 9,
WH_SHELL = 10,
WH_FOREGROUNDIDLE = 11,
WH_CALLWNDPROCRET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
} */
[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int X;
public int Y;
}
[StructLayout(LayoutKind.Sequential)]
public class MOUSELLHOOKSTRUCT
{
public POINT pt;
public uint mouseData;
public uint flags;
public uint time;
public UIntPtr dwExtraInfo;
}
/* public delegate int HookProc(int code, int wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern IntPtr SetWindowsHookEx(HookType code, HookProc func, IntPtr instance, int threadID);
[DllImport("user32.dll")]
public static extern int UnhookWindowsHookEx(IntPtr hook); */
[DllImport("user32.dll")]
public static extern int CallNextHookEx(IntPtr hook, int code, int wParam, IntPtr lParam);
}
}