Terminate MFC Application
Neeraj Bhatia -- Bhatia@mail.dec.com
Friday, November 01, 1996
Environment: Windows NT 4.0 VC++ 4.0
I have asked this question many times and seen many replies, but
nothing works for me in this situation.
> I would like to present a dialog box to the user when he/she decides
to terminate the MFC (MDI) application. User might decide NOT to
terminate the application. If the user decides to terminate the
application,
> I would like to save the MDI application workarea's dimensions into the
registry (using m_pMainWnd->GetWindowRect (&m_workArea)) and
perform other cleanup jobs.
What I need a single function that is called BEFORE the MFC application
is terminated. This function should be called by all the various
termination
methods --
1. Selecting the menu option (ID_APP_EXIT)
2. Using System Control Box option CLOSE
3. Selecting "X" option on the title bar
Is this kind of function available in MFC or I have to trap each of
these
messages ?
Thanks in advance.
-Neeraj Bhatia
>
Dmitry Davidovich -- dmitry@enigma.co.il
Monday, November 04, 1996
[Mini-digest: 7 responses]
At 14:01 1/11/96 -0500, you wrote:
>Environment: Windows NT 4.0 VC++ 4.0
>
> I have asked this question many times and seen many replies, but
> nothing works for me in this situation.
>
>> I would like to present a dialog box to the user when he/she decides
> to terminate the MFC (MDI) application. User might decide NOT to
> terminate the application. If the user decides to terminate the
>application,
>> I would like to save the MDI application workarea's dimensions into the
> registry (using m_pMainWnd->GetWindowRect (&m_workArea)) and
> perform other cleanup jobs.
>
> What I need a single function that is called BEFORE the MFC application
> is terminated. This function should be called by all the various
>termination
> methods --
> 1. Selecting the menu option (ID_APP_EXIT)
> 2. Using System Control Box option CLOSE
> 3. Selecting "X" option on the title bar
>
> Is this kind of function available in MFC or I have to trap each of
>these
> messages ?
>
> Thanks in advance.
>
> -Neeraj Bhatia
>>
>
Use OnClose (ON_WM_CLOSE) handler in main application window
-----------------------------------------
Dmitry Davidovich
CS Tel Aviv University
dmitry@enigma.co.il
ddmitry@libra.math.tau.ac.il
-----------------------------------------
-----From: David Little
I am doing exactly that, and I trap all methods of exit, but all the methods just call one that does the work:
void CAuxInput::OnSysCommand(UINT nID, LPARAM lParam)
{
// function to trap the user from hitting the "X" in
// the upper right corner, or pressing ...
// all other syscommands, we will let Windows take
// care of....
// 9/15/96
if(nID == SC_CLOSE)
{
// Only want one way out....
OnQuit();
return;
}
CDialog::OnSysCommand(nID, lParam);
}
The function that actually checks what to do:
void CAuxInput::OnQuit()
{
CMainFrame* mf = NULL;
CQuitOptions qo;
switch(qo.DoModal())
{
case IDC_EXITANDSAVE:
// one of the options on the dialog
// In this case, save the current data, then fall through
// to exitandforget....
case IDC_EXITANDFORGET:
CDialog::OnOK();
/* Be sure to kill the main window as well */
mf =(CMainFrame*)(GetParentFrame());
if(mf->GetSafeHwnd())
OnDestroy();
// This time, use PostMessage because we don't care about
// timing...Windows might have some other stuff to do first.
// Just stick a WM_QUIT message in the queue and return....
mf->PostMessage(WM_QUIT);
// fall through to kill with an OK....
break;
}
// default is to not process the OK button.
// Do nothing to return to editor....
}
And, finally, don't forget hitting the escape key:
void CAuxInput::OnCancel()
{
// Even though there isn't a cancel button on the dialog
// the user can still hit escape which translates to OnCancel..
// if that happens, just want to do an OnQuit.
OnQuit();
}
Hope this helps....
-----From: "GoroKhM1"
The method you are looking for is CWinApp::ExitInstance()
MarkG@usa.net
-----From: "Mike Ward"
Environment: Windows NT 4.0 VC++ 4.0
You could override CMainFrame's OnClose() and ask the question
there. If the user decides not to quit the app, don't call the base
class OnClose handler which by default calls DestroyWindow.
-----From: "alan ismail"
Unless something is severely messed up, all of the three numbered
methods below will cause a WM_CLOSE to be sent to your application's main
window.
So, you can catch WM_CLOSE in your app's main frame window and call your
function from there.
Use Class Gizzard to handle WM_CLOSE, and fill in the blanks like so:
void CMainFrame::OnClose()
{
int nRet = MessageBox( "Wanna save yer window positions?",
"Hey There", MB_ICONQUESTION | MB_YESNOCANCEL);
if ( IDCANCEL == nRet )
return; // App will NOT close
if ( IDYES == nRet )
{
// Save your stuff here...
// -----------------------
MyCoolSavePositionsFunc();
}
CFrameWnd::OnClose(); // This closes app.
}
P.S. This works. I just tested it as a sanity check. So, if it does not "work
for you in this situation," you really need to describe the situation in more
detail.
Later,
Alan Ismail
-----From: Tony DiBlasio
I have successfully done this by overriding CMainFrame::OnClose()
TonyD@Barudan.com
-----From: rkumar@mail.cswl.com
All the actions
1. Selecting the menu option (ID_APP_EXIT)
2. Using System Control Box option CLOSE
3. Selecting "X" option on the title bar
send a WM_CLOSE message to the applicaton all u have to do is to
handel this message in the app class.
Ratan
rkumar@cswl.com
| Вернуться в корень Архива
|