LRESULT CallNextHookEx(HHOOK hhk;int nCode;WPARAM wParam;LPARAM lParam); |
BOOL UnhookWindowsHookEx(HHOOK hhk); |
#pragma data_seg("mydata") HWND glhPrevTarWnd = NULL; // 上次鼠标所指的窗口句柄 HWND glhDisplayWnd = NULL; // 显示目标窗口标题编辑框的句柄 HWND glhHook = NULL; // 安装的鼠标钩子句柄 HINSTANCE glhInstance = NULL; // DLL实例句柄 #pragma data_seg() …… SECTIONS // def文件中将数据段TestData设置为读写共享 TestData READ WRITE SHARED |
BOOL CMouseHook::StartHook(HWND hWnd) { BOOL result = FALSE; // 安装钩子 glhHook = (HWND)SetWindowsHookEx(WH_MOUSE, MouseProc, glhInstance, 0); if (glhHook != NULL) result = TRUE; glhDisplayWnd = hWnd; // 设置显示目标窗口标题编辑框的句柄 return result; } |
LRESULT WINAPI MouseProc(int nCode, WPARAM wParam, LPARAM lParam) { LPMOUSEHOOKSTRUCT pMouseHook = (MOUSEHOOKSTRUCT FAR *) lParam; if (nCode >= 0) { HWND glhTargetWnd = pMouseHook->hwnd; // 取目标窗口句柄 HWND ParentWnd = glhTargetWnd; while (ParentWnd != NULL){ glhTargetWnd = ParentWnd; ParentWnd = GetParent(glhTargetWnd); // 取应用程序主窗口句柄 } if (glhTargetWnd != glhPrevTarWnd) { char szCaption[100]; GetWindowText(glhTargetWnd, szCaption, 100); // 取目标窗口标题 if (IsWindow(glhDisplayWnd)) SendMessage(glhDisplayWnd, WM_SETTEXT, 0, (LPARAM)(LPCTSTR)szCaption); glhPrevTarWnd = glhTargetWnd; // 保存目标窗口 } } // 继续传递消息 return CallNextHookEx((HHOOK)glhHook, nCode, wParam, lParam); } |