在win32中挂钩

时间:2009-05-04 05:37:03

标签: winapi

我希望任何窗口在鼠标悬停在非客户区域的关闭按钮上时立即关闭。我试图在SetWindowsHookEx中使用WH_GETMESSAGE捕获wm_ncmousemove然后 使用SendMessage将WM_DESTROY消息发送到指定的窗口但窗口没有关闭。任何帮助????

LRESULT CALLBACK CallWndProc(int code, WPARAM wParam, LPARAM lParam)

{

MSG* msg = (MSG*) lParam;
if(code == HC_ACTION)
{
    if(msg->message == WM_NCMOUSEMOVE)
    {
        if(msg->wParam == HTCLOSE)
        {
            SendMessage(hwndTarget, WM_DESTROY, wParam, lParam);
        }
    }
}

return CallNextHookEx(g_hkMsg, code, wParam, lParam);
}

INT WINAPI InstallW(HWND hwnd, HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow)
{

DWORD dwTarget = 0;
POINT point;

GetCursorPos(&point);

hwndTarget = WindowFromPoint(point);
dwTarget = GetWindowThreadProcessId(hwndTarget, NULL);
g_hkMsg = SetWindowsHookEx(WH_GETMESSAGE, CallWndProc, g_hInstance, 0);
if(g_hkMsg)
{
    MessageBox(NULL, L"Message hook installed, press OK to uninstall.", L"HLHookTest", MB_ICONEXCLAMATION);
    UnhookWindowsHookEx(g_hkMsg);
}
else
    MessageBox(NULL, L"Hook installation failed.", L"HLHookTest", MB_ICONERROR);

return 0;   
 }

1 个答案:

答案 0 :(得分:2)

使用WM_CLOSE发送WM_SYSCOMMANDwParam=SC_CLOSE

WM_CLOSEWM_SYSCOMMAND / SC_CLOSE要求窗口关闭。 WM_DESTROY通知窗口已关闭。说“你已经关闭”到一个窗口不会让它关闭。