在打开文档时自动选择合适的对应的doc类.下面是我看参考了csdn上的解决方法后的实现代码:
后面附有csdn上的参考方法.
我的:
CDocument* CRFMApp::OpenDocumentFile(LPCTSTR lpszFileName)
{
// TODO: 在此添加专用代码和/或调用基类
enum DOCTYPE
{
PICDOC = 0,
VIDEODOC
}docType;
CString filePath(lpszFileName);
CString fileType(filePath.Right(3));
fileType.MakeLower();
if( (fileType == _T("bmp")) || (fileType == _T("jpg")) )
{
docType = PICDOC;
}
else if( fileType == _T("avi") )
{
docType = VIDEODOC;
}
CDocTemplate* pCurTemplate = NULL;
POSITION curTemplatePos = GetFirstDocTemplatePosition();
switch( docType )
{
case PICDOC:
while( curTemplatePos != NULL )
{
pCurTemplate = GetNextDocTemplate(curTemplatePos);
CString str;
pCurTemplate->GetDocString(str, CDocTemplate::fileNewName);
if( str == _T("Picture") )
{
return pCurTemplate->OpenDocumentFile(lpszFileName);
break;
}
}
break;
case VIDEODOC:
while( curTemplatePos != NULL )
{
pCurTemplate = GetNextDocTemplate(curTemplatePos);
CString str;
pCurTemplate->GetDocString(str, CDocTemplate::docName);
if( str == _T("Video") )
{
return pCurTemplate->OpenDocumentFile(lpszFileName);
}
}
break;
default:
break;
}
return NULL;
}
csdn的:
一
当你在 程序的InitInstance() 函数调用AddDocTemplate()来注册多个文档模板对象时,MFC无法知道应该如何使用哪一个文档模板对象来完成用户的“File”->“New”请求,因此MFC弹出一个对话框,该对话框列出了各种注册过的文档模板对象以允许用户指出应该使用哪一个来调用CDocTemplate::OpenDocumentFile()。
显示该对话框的代码在CDocManager::OnFileNew()函数中:
...
if (m_templateList.GetCount() > 1)
{
CNewTypeDlg dlg(&m_templateList);
int nID = dlg.DoModal(); // here
if(nID == IDOK)
...
}
...
你不太可能重载CDocTemplate::OpenDocumentFile()函数
该函数由CWinApp::OnFileNew()所调用
...
m_pDocManager->OnFileNew();
...
你可以通过重载CWinApp::OnFileNew()来控制具体用哪个模板:
void CMyApp::OnFileNew()
{
m_ptDefaultTemplate->OpenDocumentFile(NULL); // 你指定的缺省模板
}
实际可能跟复杂,你可以看看《MFC经典问答》
二(注意此代码有错,后面作者提供了改错方法.我也参考了)
我重载CDocument* CIntegralEnviromentApp::OpenDocumentFile(LPCTSTR lpszFileName) 函数后,的确是打开了正确的模版,但是程序还同时打开了其他的模版,这是为什么?
CDocument* CIntegralEnviromentApp::OpenDocumentFile(LPCTSTR lpszFileName)
{
// TODO: Add your specialized code here and/or call the base class
TRACE("\n\n\nlpszFileName=%s\n\n\n",lpszFileName);
CDocument* TempDoc=CWinApp::OpenDocumentFile(lpszFileName);
//--------setActiveTemplate------------------------------------------
int m_nFileType=0;
CString s_cFileType="";
BOOL m_bIsFileOrImage=TRUE;
if(lpszFileName!="")
{
s_cFileType=lpszFileName;
s_cFileType=s_cFileType.Right(3);
s_cFileType.MakeLower();
m_nFileType=USR_NONE;
if(s_cFileType=="bmp")
{
m_nFileType=USR_BMP;
m_bIsFileOrImage=FALSE;
}
if(s_cFileType=="jpg")
{
m_nFileType=USR_JPG;
m_bIsFileOrImage=FALSE;
}
if(s_cFileType=="gif")
{
m_nFileType=USR_GIF;
m_bIsFileOrImage=FALSE;
}
if(s_cFileType=="txt")
m_nFileType=USR_TXT;
}
POSITION curTemplatePos = GetFirstDocTemplatePosition();
CDocTemplate* curTemplate;
switch(m_bIsFileOrImage)
{
case FALSE:
while(curTemplatePos != NULL)
{
curTemplate =GetNextDocTemplate(curTemplatePos);
CString str;
curTemplate->GetDocString(str, CDocTemplate::docName);
if(str == _T("图形文档"))
{
curTemplate->OpenDocumentFile(lpszFileName);
break;
}
}
break;
case TRUE:
while(curTemplatePos != NULL)
{
curTemplate =GetNextDocTemplate(curTemplatePos);
CString str;
curTemplate->GetDocString(str, CDocTemplate::docName);
if(str == _T("可编辑文档"))
{
curTemplate->OpenDocumentFile(lpszFileName);
break;
}
}
break;
}
//--------setActiveTemplate------------------------------------------
return TempDoc;
}
终于解决了,CDocument* TempDoc=CWinApp::OpenDocumentFile(lpszFileName);屏蔽掉
把curTemplate->OpenDocumentFile(lpszFileName);改为return curTemplate->OpenDocumentFile(lpszFileName);
