1.在App类中创建两个public变量:
CMultiDocTemplate * m_pDocTemplate;
CMultiDocTemplate * m_pNewDocTemplate;
2.在App类中的Initlnstance()函数中添加以后程序:
m_pDocTemplate = new CMultiDocTemplate(
??IDR_MULTIDTYPE,
??RUNTIME_CLASS(CMultiDocumentStudyDoc),
??RUNTIME_CLASS(CChildFrame), // custom MDI child frame
??RUNTIME_CLASS(CMultiDocumentStudyView));
?AddDocTemplate(m_pDocTemplate);
?//CMultiDocTemplate * pNewDocTemplate;
?m_pNewDocTemplate = new CMultiDocTemplate(
??IDR_MULTIDTYPE,
??RUNTIME_CLASS(CMultiDocumentStudyDoc),
??RUNTIME_CLASS(CChildFrame), // custom MDI child frame
??RUNTIME_CLASS(CNewView));
?AddDocTemplate(m_pNewDocTemplate);
3.重载OnFileNew函数(App类中),默认只打开一个视图:
m_pDocTemplate->OpenDocumentFile(NULL);
4.在Doc类中重载OnNewDocument()函数
?CMultiDocumentStudyApp*p_app = (CMultiDocumentStudyApp*)AfxGetApp();
?EkCreateNewWindow(p_app->m_pNewDocTemplate, this );?
?
CFrameWnd* CMultiDocumentStudyDoc::EkCreateNewWindow( CDocTemplate* pTemplate,CDocument* pDocument )
{
?ASSERT_VALID( pTemplate );??
??? ASSERT_VALID( pDocument );??
??? CFrameWnd* pFrame =???
??pTemplate->CreateNewFrame(pDocument, NULL );
?CRect rect;
?GetClientRect(pFrame->m_hWnd,&rect);
?pFrame->MoveWindow(100,150,rect.Width(),rect.Height());
??? if( pFrame == NULL )??
??? {TRACE0( "Warning: failed to create new frame.\n" );??
??? return NULL;??
??? }??
??? ASSERT_KINDOF( CFrameWnd, pFrame );??
??? pTemplate->InitialUpdateFrame( pFrame, pDocument );??
?
??? return pFrame;??
}