SetWindowsHookEx WH_MOUSE [英] SetWindowsHookEx for WH_MOUSE
问题描述
我有一些代码在我手中,打印鼠标的全球坐标(使用WH_MOUSE_LL)。我的目标是使用WH_MOUSE而不是WH_MOUSE_LL,因为(从我读过)它是更快。我读过论坛,当使用WH_MOUSE它需要在DLL中声明实现全局效果,但仍然,当在程序中使用它应该工作在那个应用程序,它被声明,但它不工作当我将WH_MOUSE_LL更改为WH_MOUSE时,不打印任何内容)。这是代码:
I've got some code into my hands that prints coordinates of the mouse globally (using WH_MOUSE_LL). My target is to use WH_MOUSE instead of WH_MOUSE_LL because (from what I've read) it is faster. I've read over the forum that when using WH_MOUSE it needs to be declared in DLL to achieve global effect, but still, when used in the program it should work over that application where it was declared, but it doesn't work (it prints nothing) when I just change the WH_MOUSE_LL to WH_MOUSE. This is the code:
#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;
}
推荐答案
// here I put WH_MOUSE instead of WH_MOUSE_LL
hMouseHook = SetWindowsHookEx( WH_MOUSE_LL, mouseProc, hInstance, NULL );
第四个参数也必须更改为GetCurrentThreadId
Fourth param must also be changed to GetCurrentThreadId() to make it local.
这篇关于SetWindowsHookEx WH_MOUSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!