MFC -- CMyApp::InitInstance()过程分析

 
  1. BOOL CMyApp::InitInstance()
  2. {
  3.     CSingleDocTemplate* pDocTemplate;
  4.     pDocTemplate = new CSingleDocTemplate( //步骤一
  5.         IDR_MAINFRAME,
  6.         RUNTIME_CLASS(CMyDoc),
  7.         RUNTIME_CLASS(CMainFrame),
  8.         RUNTIME_CLASS(CMyView));
  9.     if (!pDocTemplate)
  10.         return FALSE;
  11.     AddDocTemplate(pDocTemplate);  //步骤二 m_pDocManager->AddDocTemplate(pDocTemplate); 
  12.     
  13.     CCommandLineInfo cmdInfo;
  14.     ParseCommandLine(cmdInfo);    //步骤三 通过void CWinApp::OnFileNew()=>m_pDocManager->OnFileNew();创建Doc、Frame、View
  15. }
  16. //================================分析如下======================================================
  17. //========步骤一=======
  18. RUNTIME_CLASS(CMainFrame)
  19. //展开得==》
  20. (CRuntimeClass*)(&CMainFrame::classCMainFrame)
  21. //从而在
  22. pDocTemplate = new CSingleDocTemplate(
  23.     IDR_MAINFRAME,
  24.     ((CRuntimeClass*)(&CMyDoc::classCMyDoc)),
  25.     ((CRuntimeClass*)(&CMainFrame::classCMainFrame)),
  26.     ((CRuntimeClass*)(&CMyView::classCMyView)));
  27. //通过构造函数初始化了三大关键成员===>
  28. class AFX_NOVTABLE CDocTemplate : public CCmdTarget
  29. {
  30.     CRuntimeClass* m_pDocClass;    //=((CRuntimeClass*)(&CMyDoc::classCMyDoc))        // class for creating new documents
  31.     CRuntimeClass* m_pFrameClass;  //=((CRuntimeClass*)(&CMainFrame::classCMainFrame))// class for creating new frames   
  32.     CRuntimeClass* m_pViewClass;   //=((CRuntimeClass*)(&CMyView::classCMyView)))     // class for creating new views    
  33. };
  34. //========步骤二=======
  35. //将文档模板(含三口组)放入到文档模板链表中,并由CDocManager类对象进行管理
  36. //========步骤三=======
  37. //m_pDocManager->OnFileNew()调用CDocTemplate中CFrame/CDocument的创建方法,这里终于用到了“执行期动态创建”
  38.     CFrameWnd* CDocTemplate::CreateNewFrame
  39.     {
  40.         context.m_pNewViewClass = m_pViewClass;
  41.         CDocument* pDocument = (CDocument*)m_pDocClass->CreateObject();
  42.     }
  43.     CDocument* CDocTemplate::CreateNewDocument
  44.     {
  45.         CFrameWnd* pFrame = (CFrameWnd*)m_pFrameClass->CreateObject();
  46.     }

你可能感兴趣的:(Class,文档,mfc)