-
-
[求助]怪异的鼠标钩子问题
-
发表于:
2009-5-5 18:46
4068
-
我写了一个小程序来截取鼠标的消息,但是出现三个很怪的现象,一是我已经在DLL中验证是否为鼠标右键按下的消息,但是主程序中还是会收到各种莫名的消息;二是我截取的鼠标坐标位置是错误的,我怀疑是传过来MOUSEHOOKSTRUCT不正确;三是我截取的鼠标消息只是本地窗口的,其他窗口的鼠标消息完全截取不到.
请各位路过的大哥帮忙解决下.
下面附上主要代码:
EXE程序:
long CMainDialog::OnHookKey(WPARAM wParam, LPARAM lParam)
{
MOUSEHOOKSTRUCT msg;
MoveMemory(&msg , (MOUSEHOOKSTRUCT*)lParam , sizeof(MOUSEHOOKSTRUCT));
if(wParam == WM_RBUTTONDOWN)
{
CString strItem ;
char *szWndName = (char*)::HeapAlloc(::GetProcessHeap() , HEAP_ZERO_MEMORY , 1024);
::GetWindowText(msg.hwnd , szWndName , 1024);
strItem.Format("窗口标题:%s, 鼠标位置: %d , %d\r\n" , szWndName ,msg.pt.x , msg.pt.y);
// 添加到编辑框中
CString strEdit;
GetDlgItem(IDC_KEYMSG)->GetWindowText(strEdit);
GetDlgItem(IDC_KEYMSG)->SetWindowText(strItem + strEdit);
::HeapFree(::GetProcessHeap() , 0 , szWndName);
}
return 0;
}
DLL代码:
// 鼠标钩子函数
LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
//这里做了验证,但是主程序还是会收到很多莫名奇妙的消息
if(nCode == HC_ACTION && wParam == WM_RBUTTONDOWN)
{
::PostMessage(g_hWndCaller, WM_MOUSEHOOK, wParam, lParam);
}
return ::CallNextHookEx(g_hHook, nCode, wParam, lParam);
}
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课