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

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;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值