MFC 多文件原始碼分析1
阿新 • • 發佈:2019-01-10
新增模板
在複寫的CWinApp::InitInstance()函式中新增下面程式碼
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(
IDR_SMARTTTYPE,
RUNTIME_CLASS(CCosiWorksDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CCosiWorksView));
AddDocTemplate(pDocTemplate);
new一個CMultiDocTemplate,並用doc,frame,view的CRuntimeClass類初始化CMultiDocTemplate然後呼叫AddDocTemplate,往CWinApp的成員m_pDocManager變數,文件管理器中新增模板
void CWinApp::AddDocTemplate(CDocTemplate* pTemplate)
{
if (m_pDocManager == NULL)
m_pDocManager = new CDocManager;
m_pDocManager->AddDocTemplate(pTemplate);
}
開啟文件到這裡呼叫到了CMultiDocTemplate中實際的文件裡,就是RUNTIME_CLASS(CCosiWorksDoc)CDocument* CWinApp::OpenDocumentFile(LPCTSTR lpszFileName) { ENSURE_VALID(m_pDocManager); return m_pDocManager->OpenDocumentFile(lpszFileName); } CDocument* CDocManager::OpenDocumentFile(LPCTSTR lpszFileName) { POSITION pos = m_templateList.GetHeadPosition(); CDocTemplate* pBestTemplate = NULL; while (pos != NULL) { CDocTemplate* pTemplate = (CDocTemplate*)m_templateList.GetNext(pos); CDocTemplate::Confidence match; match = pTemplate->MatchDocType(szPath, pOpenDocument); if (match > bestMatch) { bestMatch = match; pBestTemplate = pTemplate; } if (match == CDocTemplate::yesAlreadyOpen) break; // stop here } return pBestTemplate->OpenDocumentFile(szPath); } CDocument* CMultiDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName,BOOL bMakeVisible) { CDocument* pDocument = CreateNewDocument(); } CDocument* CDocTemplate::CreateNewDocument() { CDocument* pDocument = (CDocument*)m_pDocClass->CreateObject(); return pDocument; } void CMultiDocTemplate::AddDocument(CDocument* pDoc) { ASSERT_VALID(pDoc); CDocTemplate::AddDocument(pDoc); ASSERT(m_docList.Find(pDoc, NULL) == NULL); // must not be in list m_docList.AddTail(pDoc); } CDocument* CMultiDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName,BOOL bMakeVisible) { CDocument* pDocument = CreateNewDocument(); CFrameWnd* pFrame = CreateNewFrame(pDocument, NULL); } CFrameWnd* CDocTemplate::CreateNewFrame(CDocument* pDoc, CFrameWnd* pOther) { CFrameWnd* pFrame = (CFrameWnd*)m_pFrameClass->CreateObject(); } CDocument* CMultiDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName,BOOL bMakeVisible) { CDocument* pDocument = CreateNewDocument(); CFrameWnd* pFrame = CreateNewFrame(pDocument, NULL); pDocument->OnOpenDocument(lpszPathName); }