参考:http://blog.csdn.net/segen_jaa/article/details/8489917
应用背景:
我用的是MDI框架,需要调用同事给的dll,才能确定本程序解析何种格式的文件。如他给了dll1,dll2,dll3,dll4,后缀分别是.format1,.format2,.format3,.format4。以后还可能会有(dll5,format5),(dll6,format6)
因此在MDI加载文档模板之前就要获得全部后缀名,然后动态的传给MDI。
首先,需要修改打开文件的后缀名。需要重载CMultiDocTemplate,参考文章的后缀也是在主程序中写好的,不是我想要的,
很简单,在继承类中多加一个参数 char *suffix,
具体实现如下:
[cpp] view plain copy
- #pragma once
- #include "afxwin.h"
-
- class CMultiDocTemplateSuffixEx : public CMultiDocTemplate
- {
- CString m_strSuffix;
- public:
- CMultiDocTemplateSuffixEx(char *suffix,UINT nIDResource, CRuntimeClass* pDocClass,
- CRuntimeClass* pFrameClass, CRuntimeClass* pViewClass):\
- CMultiDocTemplate( nIDResource, pDocClass, pFrameClass, pViewClass )
- {
- m_strSuffix.Format("%s",suffix);
- }
-
- virtual BOOL GetDocString(CString& rString, enum DocStringIndex index) const
- {
- if(index == CDocTemplate::filterExt)
- {
- rString = "."+m_strSuffix;
- }
- else if( index == CDocTemplate::fileNewName || index == CDocTemplate::filterName )
- {
- rString = m_strSuffix+"(*."+m_strSuffix+")"; }
- else
- {
- __super::GetDocString(rString,index);
- }
- return TRUE;
- };
- };
使用:
[cpp] view plain copy
- CMultiDocTemplate *pDocTemplate0;
- char *curSuffix[]={"format1","format2","format3"};
- for(int i=0; i<3; i++)
- {
- pDocTemplate0 = new CMultiDocTemplateSuffixEx
- (curSuffix[i],IDR_MAINFRAME,
- RUNTIME_CLASS(CTestDoc),
- RUNTIME_CLASS(CChildFrm),
- RUNTIME_CLASS(CTestView));
- AddDocTemplate(pDocTemplate0);
-
- }
- }