Neal
Neal

Reputation: 463

Hooking in win32

I want any window to close as soon as the mouse hovers on the close button on its non client area. I tried to trap wm_ncmousemove using WH_GETMESSAGE in SetWindowsHookEx and then using SendMessage to send a WM_DESTROY message to the specified window but window is not closing. Any help????

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;   
 }

Upvotes: 1

Views: 1225

Answers (1)

RichieHindle
RichieHindle

Reputation: 281355

Send either WM_CLOSE or WM_SYSCOMMAND with wParam=SC_CLOSE instead.

WM_CLOSE and WM_SYSCOMMAND / SC_CLOSE ask the window to close. WM_DESTROY informs the window that it has been closed. Saying "You have been closed" to a window won't make it close.

Upvotes: 2

Related Questions