MFC 为同一个文档创建不同视图

// CMainFrame message handlers
//
//MDI架构下,为一个document创建多个视图的方法,既是同一个document的不同view的表现形式
//
int CMainFrame::CreateNewView(UINT nIDResource,CRuntimeClass* pFrameClass,CRuntimeClass* pViewClass,CDocument* pDoc)
{
/***********************************************************
* 函数名: CreateNewView
* 参数: UINT nIDResource,CRuntimeClass* pFrameClass,CRuntimeClass* pViewClass,CDocument* pDoc
* 功能描述:
* 1. 根据框架/视图/文档动态创建视图
* 2. 利用CCreateContext context关联文档/视图
* 3. 可以再进一步的抽象参数为CCreateContext* pContext
* 4. 具体可以参考CMDIChildWnd* CMDIFrameWnd::CreateNewChild()进行进一步的改写
* 返回值:
* 作者: Sam Zhao
* 日期:
* 注释:
* 日志:
* 1.
* 2.
* 3.
***********************************************************/

//CMyHtmlChildFrame* pHtmlChild=new CMyHtmlChildFrame();
//use CRuntimeClass is also OK
//CMyHtmlChildFrame* pHtmlChild=(CMyHtmlChildFrame*)pFrameClass->CreateObject();
CMDIChildWnd* pFrame=(CMDIChildWnd*)pFrameClass->CreateObject();
ASSERT_KINDOF(CMDIChildWnd, pFrame);
//CMDIDemoDoc* pDoc=(CMDIDemoDoc*)(MDIGetActive()->GetActiveDocument());
ASSERT_VALID(pDoc);

//CMultiDocTemplate* pTemplate=(CMultiDocTemplate*)(pDoc->GetDocTemplate());
CDocTemplate* pTemplate=pDoc->GetDocTemplate();
//ASSERT_VALID(pTemplate);

CCreateContext context;
context.m_pCurrentDoc=pDoc;// use existing document
context.m_pCurrentFrame=pFrame;// new child frame to be upon
context.m_pNewViewClass=pViewClass;// RUNTIME_CLASS(CMyHtmlView);// new view to be created
context.m_pNewDocTemplate=pTemplate;// use existing document template
context.m_pLastView=NULL;// no
TRACE0("Before LoadFrame()\n");
if (!pFrame->LoadFrame(nIDResource,WS_OVERLAPPEDWINDOW|FWS_ADDTOTITLE,AfxGetMainWnd()/*this*/,&context))
{
TRACE0("Warning: Couldn't load frame window!\n");
return -1;
}
TRACE0("After LoadFrame()\n");
//pHtmlChild->ShowWindow(SW_SHOWNORMAL);
pFrame->InitialUpdateFrame(pDoc,TRUE);
//pHtmlChild->InitialUpdateFrame(pDoc,TRUE);

//just delagate to implementation in CFrameWnd
//pTemplate->InitialUpdateFrame(pHtmlChild,pDoc,TRUE);

//it worked!
return 0;
}

void CMainFrame::OnCreateView()
{
// TODO: Add your command handler code here
CDocument* pDoc=MDIGetActive()->GetActiveDocument();
ASSERT_VALID(pDoc);
//create html view and attach with pDoc
CreateNewView(IDI_ICON1,RUNTIME_CLASS(CMyHtmlChildFrame),RUNTIME_CLASS(CMyHtmlView),pDoc);
//create tree view and attach with pDoc
CreateNewView(IDI_ICON2,RUNTIME_CLASS(CMyTreeChildFrame),RUNTIME_CLASS(CMyTreeView),pDoc);
}

你可能感兴趣的:(MFC知识)