日期:2014-05-16 浏览次数:20605 次
1.Hook简介:作用是拦截某些消息,关键函数是SetWindowsHookEX()
2.示例程序:
a.新建一基于对话框工程,InnerHook,此过程的钩子是只拦截本进程的。
b.在OnInitDialog()中添加代码:
g_hWnd=m_hWnd;
g_hMouse=SetWindowsHookEx(WH_MOUSE,MouseProc,NULL,GetCurrentThreadId());设置了鼠标钩子
g_hKeyboard=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,NULL,GetCurrentThreadId());设置了键盘钩子
c.完成钩子函数的编写:
HHOOK g_hKeyboard=NULL;
HHOOK g_hMouse;
HWND g_hWnd=NULL;
LRESULT CALLBACK MouseProc(
int nCode, // hook code
WPARAM wParam, // message identifier
LPARAM lParam // mouse coordinates
)
{
return 1;
}
LRESULT CALLBACK KeyboardProc(
int code, // hook code
WPARAM wParam, // virtual-key code
LPARAM lParam // keystroke-message information
)
{
//if(VK_SPACE==wParam || VK_RETURN==wParam)如果是空格键