Wednesday, December 9, 2009

HOWTO: Close Application on Minimize

This is a popular question lately: how do I close my application when the user clicks the "smart minimize button"? This button does exactly that - it minimizes your application and does not close it. Your main application window is minimized when it receives a WM_SIZE message with the SIZE_MINIMIZED constant in the wParam parameter. All you have to do is call PostMessage(WM_CLOSE) and you are done. Here's a sample WTL handler:


LRESULT CCloseOnMinFrame::OnSize(UINT /*uMsg*/,WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled){
if(wParam == SIZE_MINIMIZED) {
PostMessage(WM_CLOSE);
bHandled = TRUE;
return 0;
}
// Not handled here
bHandled = FALSE;
return 1;
}

No comments: