Friday, November 06, 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;
}

4 comments:

Christopher Fairbairn said...

Another option is to call SHDoneButton with the SHDB_SHOWCANCEL flag.

That way you get a WM_COMMAND message with IDCANCEL whenever the [X] is tapped, which you can then handle in the same way as a menu softkey etc.

João Paulo Figueira said...

I'm always learning... Thanks for your comment Chris!

Jake Lin said...

I prefer to hide the 'X' & 'OK' button 'cause the new 6.5 ROM has some issues to display these buttons.

I would like to use menu only to control minimise and close actions.

Tony said...

Hi, only a question: how and where can I access to modify the native code in a windows mobile phone? thanks