tags:

views:

145

answers:

2
+1  Q: 

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;

}

+1  A: 

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.

RichieHindle
but this closes only the command prompt when mouse pointer is over the close button of the messagebox of the program
Neal
why WM_DESTROY didnot work
Neal
@Neal: See my expanded answer. And yes, it closes the window whose Close button you move your mouse over, which is what you asked for. If you want to close the whole application, you're in for a lot of work. For instance, what if the application pops up a "Save changes, Yes/No" dialog that doesn't have a close button?
RichieHindle
A: 

*> Send either WM_CLOSE ...*

No, WM_CLOSE must never be sent (Win32 basis...)

[citation needed]
RichieHindle