相信以前用过VB、Delphi,特别是VC的程序员应该对钩子程序都不陌生。在C#中我们同样可以使用钩子程序来实现特殊效果,比如当用户按下某个特殊键时提示,比如关闭应用程序前提示等。
当然使用方法相对VC来说要稍微复杂一点,有的地方还不太方便,下面的例子中实现两个基本功能:
1、按下Alt+F4时使窗口最小化
2、关闭应用程序前提示
不过目前只能捕获消息,不能屏蔽消息,我正在实验,也希望知道的高手能多多指教
一、加入winuser.h中的定义
因为钩子程序一般情况下都是在vc下使用的,在c#里面并没有对应的方法、结构等的定义,我们首先需要把winuser.h中的相关定义加入自己的类
钩子类型的枚举
public enum HookType : int
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
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
}
具体的说明在msdn中都可以查到,主要的比如WH_KEYBOARD是监控按键事件,WH_CALLWNDPROC是在消息触发时执行
虚键值的定义
public enum VirtualKeys
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
VK_SHIFT = 0x10,
VK_CONTROL = 0x11,
VK_MENU = 0x12, //ALT
VK_PAUSE = 0x13,
VK_CAPITAL = 0x14
}
这个不用说明了,对应ALT、CTRL等键
消息结构体
public struct CWPSTRUCT
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public IntPtr lparam;
public IntPtr wparam;
public int message;
public IntPtr hwnd;
}
这个是windows内部传递过来的消息的结构
二、加入自己定义的委托和事件参数
钩子委托
public delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
public delegate void HookEventHandler(object sender, HookEventArgs e);
HokkProc是SetWindowsHookEx调用时的委托事件,HookEventHandler是自己的委托事件
钩子事件参数
public class HookEventArgs : EventArgs
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public int HookCode;
public IntPtr wParam;
public IntPtr lParam;
public Keys key;
public bool bAltKey;
public bool bCtrlKey;
}
是自己的委托事件中接受的事件参数
三、实现自己的钩子类
这一步是最重要的,要使用钩子,我们需要引用user32.dll中的相应方法:
[DllImport(
"
user32.dll
"
)]
static
extern
IntPtr SetWindowsHookEx(HookType hook, HookProc callback, IntPtr hMod,
uint
dwThreadId);
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
[DllImport(
"
user32.dll
"
)]
static
extern
bool
UnhookWindowsHookEx(IntPtr hhk);
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
[DllImport(
"
user32.dll
"
)]
static
extern
int
CallNextHookEx(IntPtr hhk,
int
nCode, IntPtr wParam, IntPtr lParam);
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
[DllImport(
"
user32.dll
"
)]
static
extern
short
GetKeyState(VirtualKeys nVirtKey);
SetWindowsHookEx是注册一个钩子程序,UnhookWindowsHookEx是释放钩子程序,CallNextHookEx调用钩子的后续事件处理,GetKeyState得到所按的虚键
然后就可以调用这些方法来实现钩子程序,比如注册一个钩子可以调用:
m_hook
=
SetWindowsHookEx(m_hooktype, m_hookproc, IntPtr.Zero, (
uint
)AppDomain.GetCurrentThreadId());
其中m_hooktype就是HookType中定义的类型,m_hookproc就是实际的钩子处理程序:
m_hookproc
=
new
HookProc(KeyHookProcedure);
最关键的就是KeyHookProcedure等钩子处理程序:
protected
int
KeyHookProcedure(
int
code, IntPtr wParam, IntPtr lParam)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (code != 0)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return CallNextHookEx(m_hook, code, wParam, lParam);
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
if (HookInvoked != null)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Keys key