1、全局变量
HHOOK g_hook; HINSTANCE g_hInstance; HWND g_hWnd;
2、 安装钩子
g_hInstance = NULL; g_hWnd = m_hWnd; g_hook = SetWindowsHookEx(WH_MOUSE_LL, (HOOKPROC)LowLevelMouseProc, g_hInstance, 0);
3、消息回调
LRESULT CALLBACK LowLevelMouseProc( int nCode, WPARAM wParam, LPARAM lParam ) { if (wParam == WM_LBUTTONDOWN) { MOUSEHOOKSTRUCT* mhookstruct; //鼠标HOOK结构体 mhookstruct = (MOUSEHOOKSTRUCT*)lParam; POINT pt = mhookstruct->pt; //将当前鼠标坐标点的x,y坐标作为参数向主程序窗口发送消息 ::PostMessage(g_hWnd, WM_LBUTTONDOWN, MK_CONTROL, MAKELPARAM(pt.x, pt.y)); } return CallNextHookEx(g_hook, nCode, wParam, lParam); }