全局鼠标钩子

#include <windows.h>
#include <stdio.h>

HHOOK g_hMouse;

LRESULTCALLBACK MouseProc(intnCode, WPARAMwParam, LPARAMlParam)
{
    if(nCode >= 0)
    {
        PMSLLHOOKSTRUCT pmll = (PMSLLHOOKSTRUCT) lParam;

        printf("msg: %lu, x:%ld, y:%ld\n", wParam, pmll->pt.x, pmll->pt.y);
        //改成你需要的程式...
    }

    returnCallNextHookEx(g_hMouse, nCode, wParam, lParam);
}


int main(void)
{
    MSG msg;

    g_hMouse = SetWindowsHookEx(WH_MOUSE_LL, MouseProc, GetModuleHandle(NULL), 0);
    if(!g_hMouse)
    {
        printf("Hook error: %d\n", GetLastError());
    }

    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    UnhookWindowsHookEx(g_hMouse);

    return(int)msg.wParam;
}

你可能感兴趣的:(全局鼠标钩子)