添加模板
在复写的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);
}
打开文档
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);
}
到这里调用到了CMultiDocTemplate中实际的文档里,就是RUNTIME_CLASS(CCosiWorksDoc)