我手上有一些代码可以全局打印鼠标坐标(使用WH_MOUSE_LL)。我的目标是使用WH_MOUSE而不是WH_MOUSE_LL,因为(从我读过的内容)它更快。我已经阅读了论坛,当使用WH_MOUSE时,它需要在DLL中声明以实现全局效果,但是,当在程序中使用时,它应该在声明它的应用程序上工作,但它不起作用(它当我只是将WH_MOUSE_LL更改为WH_MOUSE时,不打印任何内容。这是代码:
#define _WIN32_WINNT 0x0400
#pragma comment( lib, "user32.lib" )
#include <windows.h>
#include <stdio.h>
HHOOK hMouseHook;
LRESULT CALLBACK mouseProc (int nCode, WPARAM wParam, LPARAM lParam)
{
MOUSEHOOKSTRUCT * pMouseStruct = (MOUSEHOOKSTRUCT *)lParam;
if (pMouseStruct != NULL){
if(wParam == WM_LBUTTONDOWN)
{
printf( "clicked" );
}
printf("Mouse position X = %d Mouse Position Y = %d\n", pMouseStruct->pt.x,pMouseStruct->pt.y);
}
return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
}
DWORD WINAPI MyMouseLogger(LPVOID lpParm)
{
HINSTANCE hInstance = GetModuleHandle(NULL);
// here I put WH_MOUSE instead of WH_MOUSE_LL
hMouseHook = SetWindowsHookEx( WH_MOUSE_LL, mouseProc, hInstance, NULL );
MSG message;
while (GetMessage(&message,NULL,0,0)) {
TranslateMessage( &message );
DispatchMessage( &message );
}
UnhookWindowsHookEx(hMouseHook);
return 0;
}
int main(int argc, char** argv)
{
HANDLE hThread;
DWORD dwThread;
hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)MyMouseLogger, (LPVOID) argv[0], NULL, &dwThread);
if (hThread)
return WaitForSingleObject(hThread,INFINITE);
else
return 1;
}
答案 0 :(得分:7)
// here I put WH_MOUSE instead of WH_MOUSE_LL
hMouseHook = SetWindowsHookEx( WH_MOUSE_LL, mouseProc, hInstance, NULL );
还必须将第四个参数更改为GetCurrentThreadId()以使其成为本地参数。
答案 1 :(得分:1)
因为你有一个“主”,我的猜测是你需要把它变成一个dll才能使它适用于* _LL类型以外的消息
Understanding the low-level mouse and keyboard hook (win32)
http://developer-resource.blogspot.com/2008/07/setwindowshookex-example.html有一个dll示例