How can an MFC application terminate itself?

What is the proper way for an MFC application to cleanly close itself?

18.08.2008 12:04:06
5 ОТВЕТОВ
РЕШЕНИЕ

Programatically Terminate an MFC Application

 void ExitMFCApp()
   {
        // same as double-clicking on main window close box
        ASSERT(AfxGetMainWnd() != NULL);
        AfxGetMainWnd()->SendMessage(WM_CLOSE);
   }

http://support.microsoft.com/kb/117320

10
7.01.2012 10:23:40
AfxGetMainWnd()->PostMessage(WM_CLOSE);
14
3.07.2012 14:44:32

In support of @Mike's answer, the reason to use this method is to trigger the correct shutdown sequence. Especially important for MDI/SDI applications because it gives a chance for documents to prompt for save before exit or to cancel the exit.

@Matt Noguchi, your method will circumvent this sequence (which may be the desired effect, I suppose, but you've probably got problems if you're short-circuiting the normal teardown.

4
19.08.2008 01:08:09
PostQuitMessage( [exit code] );
4
3.07.2012 14:44:27

If it is a dialog based application you can do it by calling EndDialog() function.

If it is an SDI/MDI based application you can call DestroyWindow. But before which you will need to do the cleanup yourself (closing documents, deallocating memory and resources, destroying any additional windows created etc).

1
18.09.2008 10:23:26