MFC 的MDI创建空文档调用过程

来源:互联网 发布:sql开发工程师招聘 编辑:程序博客网 时间:2024/06/18 13:02

1,XXApp类中的消息映射

ON_COMMAND(ID_FILE_NEW, CWinAppEx::OnFileNew)


2,

void CWinApp::OnFileNew()

{

       if(m_pDocManager != NULL)

              m_pDocManager->OnFileNew();

}


3,

void CDocManager::OnFileNew()

{

       if(m_templateList.IsEmpty())

       {

              TRACE(traceAppMsg, 0, "Error: no document templates registered withCWinApp.\n");

              AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);

              return;

       }

 

       CDocTemplate* pTemplate = (CDocTemplate*)m_templateList.GetHead();

       if (m_templateList.GetCount()> 1)

       {

              // morethan one document template to choose from

              // 超过一个doctemplate出现对话框让用户选择

              CNewTypeDlg dlg(&m_templateList);

              INT_PTR nID = dlg.DoModal();

              if(nID == IDOK)

                     pTemplate =dlg.m_pSelectedTemplate;

              else

                     return;     // none - canceloperation

       }

 

       ASSERT(pTemplate != NULL);

       ASSERT_KINDOF(CDocTemplate, pTemplate);

 

       pTemplate->OpenDocumentFile(NULL);

              // ifreturns NULL, the user has already been alerted

}

4,

CDocument*CMultiDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName, BOOL bAddToMRU, BOOLbMakeVisible)

{

       CDocument* pDocument = CreateNewDocument(); 创建Doc类对象

       if(pDocument == NULL)

       {

              TRACE(traceAppMsg, 0, "CDocTemplate::CreateNewDocument returnedNULL.\n");

              AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);

              returnNULL;

       }

       ASSERT_VALID(pDocument);

 

       BOOL bAutoDelete =pDocument->m_bAutoDelete;

       pDocument->m_bAutoDelete = FALSE;   // don't destroyif something goes wrong

       CFrameWnd* pFrame = CreateNewFrame(pDocument, NULL); 创建ChildFrame和View对象

       pDocument->m_bAutoDelete = bAutoDelete;

       if(pFrame == NULL)

       {

              AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);

              deletepDocument;      //explicit delete on error

              returnNULL;

       }

       ASSERT_VALID(pFrame);

 

       if(lpszPathName == NULL)

       {

              //create a new document - with default document name

              SetDefaultTitle(pDocument);

 

              // avoidcreating temporary compound file when starting up invisible

              if(!bMakeVisible)

                     pDocument->m_bEmbedded =TRUE;

 

              if(!pDocument->OnNewDocument())虚函数

              {

                     //user has be alerted to what failed in OnNewDocument

                     TRACE(traceAppMsg, 0, "CDocument::OnNewDocument returned FALSE.\n");

                     pFrame->DestroyWindow();

                     returnNULL;

              }

 

              // itworked, now bump untitled count

              m_nUntitledCount++;

       }

       else

       {

              // openan existing document

              CWaitCursor wait;

              if(!pDocument->OnOpenDocument(lpszPathName))

              {

                     //user has be alerted to what failed in OnOpenDocument

                     TRACE(traceAppMsg, 0, "CDocument::OnOpenDocument returned FALSE.\n");

                     pFrame->DestroyWindow();

                     returnNULL;

              }

              pDocument->SetPathName(lpszPathName,bAddToMRU);

              pDocument->OnDocumentEvent(CDocument::onAfterOpenDocument);

       }

 

       InitialUpdateFrame(pFrame, pDocument,bMakeVisible);

       return pDocument;

}