MFC 多文档源码分析1

添加模板

在复写的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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值