1、调用此函数,程序会自动判断文档是否已经打开,若已经打开则把打开的此文档设为活动文档。
CRadPupApp*pApp=(CRadPupApp*)AfxGetApp();
pApp->OpenDocumentFile(filepath);//调用此函数,程序会自动判断
2、自己写代码判断
void CRadPupApp::OpenFile(LPCTSTR lpszPathName)//OpenFile为自己写的打开函数
{
POSITION curTemplatePos = GetFirstDocTemplatePosition();
while(curTemplatePos != NULL)
{
CDocTemplate* curTemplate = GetNextDocTemplate(curTemplatePos);
POSITION posDoc = curTemplate->GetFirstDocPosition();
while (posDoc!= NULL)
{
CRadPupDoc * pDoc = (CRadPupDoc *)curTemplate->GetNextDoc(posDoc);
if (DocumentExist(pDoc,lpszPathName))///当文档存在的时候就在当前文档打开
{
CView * pView = NULL;
POSITION pos = pDoc->GetFirstViewPosition();
while (pos)
{
pView = pDoc->GetNextView(pos);
if (pView->IsKindOf(RUNTIME_CLASS(CRadPupView)))
{
break;
}
else
{
pView = NULL;
}
}
if (pView == NULL)
{
return ;
}
CFrameWnd* pFrame = pView->GetParentFrame();
pFrame->ActivateFrame();//关键在这句话
return;
}
}
curTemplate->OpenDocumentFile(lpszPathName);
return;
}
}
BOOL CRadPupApp::DocumentExist(CRadPupDoc * pDoc, LPCTSTR lpszPathName)
{
if (pDoc == NULL)
{
return FALSE;
}
CString strTitle = pDoc->GetTitle();
CString strFile = lpszPathName;
CString strFileTitle = strFile.Right(strFile.GetLength() - strFile.ReverseFind('\\') - 1);
if (strTitle == strFileTitle)
{
return TRUE;
}
return 0;
}
注:不管是pApp->OpenDocumentFile(filepath)还是curTemplate->OpenDocumentFile(lpszPathName),最终都会调用Doc类的BOOLCRadPupDoc::OnOpenDocument(LPCTSTRlpszPathName),只要在Doc类写打开代码即可。