Example #1
0
void CFrameWnd::OnClose()
/***********************/
{
    if( m_lpfnCloseProc != NULL ) {
        m_lpfnCloseProc( this );
    } else {
        CWinApp *pApp = AfxGetApp();
        if( pApp != NULL && pApp->m_pMainWnd == this ) {
            if( !pApp->SaveAllModified() ) {
                return;
            }
            pApp->CloseAllDocuments( FALSE );
            if( pApp->m_pMainWnd == NULL ) {
                // If the main window has been destroyed by CloseAllDocuments()
                // (e.g. in an SDI application), just return since trying to execute the
                // rest of OnClose() will cause a crash due to an invalid this pointer.
                return;
            }
        }
        
        CDocument *pDoc = GetActiveDocument();
        if( pDoc != NULL ) {
            if( !pDoc->CanCloseFrame( this ) ) {
                return;
            }
            
            BOOL        bOnlyFrame = TRUE;
            POSITION    position = pDoc->GetFirstViewPosition();
            while( position != NULL ) {
                CView *pView = pDoc->GetNextView( position );
                ASSERT( pView != NULL );
                if( pView->GetParentFrame() != this ) {
                    bOnlyFrame = FALSE;
                    break;
                }
            }
            if( bOnlyFrame ) {
                // OnCloseDocument destroys the frame, so just return after calling it.
                pDoc->OnCloseDocument();
                return;
            }
            pDoc->PreCloseFrame( this );
        }
        DestroyWindow();
    }
}