CDocument* CMyApp::CreateOrShow(CMultiDocTemplate* pDocTemplate)
{
POSITION posDoc=pDocTemplate-> GetFirstDocPosition();
CDocument* pDocument =NULL;
if(posDoc!=NULL){
pDocument =pDocTemplate-> GetNextDoc(posDoc);
//To get the first view in the list of views:
POSITION posView = pDocument -> GetFirstViewPosition();
CView* pFirstView = pDocument -> GetNextView(posView);
ASSERT(pFirstView !=NULL);
pFirstView -> GetParentFrame()-> GetLastActivePopup()-> BringWindowToTop();
return pDocument;
}
else{
pDocument =pDocTemplate-> CreateNewDocument();
if (pDocument == NULL)
{
TRACE0( "CDocTemplate::CreateNewDocument returned NULL.\n ");
AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
return NULL;
}
ASSERT_VALID(pDocument);
CString strTitle;
pDocTemplate-> GetDocString(strTitle,CDocTemplate::windowTitle);
pDocument-> SetTitle(strTitle);
BOOL bAutoDelete = pDocument-> m_bAutoDelete;
pDocument-> m_bAutoDelete = FALSE; // don 't destroy if something goes wrong
CFrameWnd* pFrame = pDocTemplate-> CreateNewFrame(pDocument, NULL);
pDocument-> m_bAutoDelete = bAutoDelete;
if (pFrame == NULL)
{
AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
delete pDocument; // explicit delete on error
return NULL;
}
ASSERT_VALID(pFrame);
pDocTemplate-> InitialUpdateFrame(pFrame ,pDocument,TRUE);
pFrame-> ShowWindow(SW_SHOWMAXIMIZED);
}
return pDocument ;
}
==================================================================================
==================================================================================
BOOL CMainFrame::FntCreateOrShow(CMultiDocTemplate* pDocTemplate)
{
POSITION posDoc=pDocTemplate-> GetFirstDocPosition();
CDocument* pDocument =NULL;
if (posDoc!=NULL)
{
pDocument =pDocTemplate-> GetNextDoc(posDoc);
POSITION posView = pDocument -> GetFirstViewPosition();
CView* pFirstView = pDocument -> GetNextView(posView);
ASSERT(pFirstView !=NULL);
pFirstView-> GetParentFrame()-> GetLastActivePopup()-> BringWindowToTop();//激活视图
return TRUE;
}
else
{
pDocTemplate-> OpenDocumentFile(NULL);
}
return FALSE;
}