
Mainframe Close & WM_QUIT
Quote:
>Hi,
>I would like to prevent the clicking of the X at the top right of the
>mainframe or at least catch it BEFORE any windows are destroyed.
>Does anyone know how to do this, I can't find any info on this anywhere. If
>you trap WM_QUIT - its too late the window's gone!
>Alex.
You want a handler like this:
afx_msg void CMyMainFrame::OnSysCommand(UINT nID, LPARAM lParam)
{
if (nID == SC_CLOSE) {
if (IDontWantToLetTheUserCloseMeRightNow()) {
return;
}
}
CMainFrame::OnSysCommand(nID, lParam);
Quote:
}
Use this technique if you want to display an "Are you sure?" warning
before closing the app, or if there are certain times or circumstances where
the app shouldn't be closed. (And if you're refusing to close because
something special is going on, you should display a dialog explaining to the
user exactly what that special something is, and exactly what the user needs
to do to allow the closure to occur.)
If you _never_ want to let the user close the app, then this is bad UI;
you're presenting a command as if it were available (the "X" in the caption
bar, and the "Close" command in the control menu), when you know it will
never be. To fix this, in addition to the handler above, you should add this
code to your app's initialization code:
CMenu * pSysMenu = AfxGetMainWnd()->GetSystemMenu();
if (pSysMenu) {
VERIFY(pSysMenu->DeleteMenu(SC_CLOSE,MF_BYCOMMAND));
Quote:
}
Also, if your app doesn't always allow the Close command, you should
add handlers to your mainframe class for the WM_QUERYENDSESSION and
WM_ENDSESSION messages. If you get a WM_QUERYENDSESSION message, the user
has just chosen to shut down or restart Windows, which will involve killing
your app. :) If you decide to allow the shutdown, your OnQueryEndSession
handler should return TRUE. If you decide to deny it, your handler should
return FALSE.
--
\o\ If you're interested in books and stories with transformation themes, \o\
/o/ please have a look at <URL:http://www.halcyon.com/phaedrus>. Thanks! /o/
\o\ FC1.21:FC(W/C)p6arw A- C->++ D>++ H+ M>+ P R T++++ W** Z+ Sm RLCT \o\
/o/ a cmn++++$ d e++ f+++ h- i++wf p-- sm# /o/