5

我手中有一些代码可以在全局范围内打印鼠标的坐标(使用 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;

}
4

2 回答 2

7
// here I put WH_MOUSE instead of WH_MOUSE_LL
hMouseHook = SetWindowsHookEx( WH_MOUSE_LL, mouseProc, hInstance, NULL );

还必须将第四个参数更改为 GetCurrentThreadId() 以使其成为本地参数。

于 2012-06-24T20:04:06.417 回答
1

因为你在那里有一个“main”,我猜你需要把它变成一个 dll 才能处理 *_LL 类型以外的消息

了解低级鼠标和键盘钩子(win32)

http://developer-resource.blogspot.com/2008/07/setwindowshookex-example.html has a dll example

于 2013-09-10T20:01:48.610 回答