【转载】让文档模板支持多文件扩展名

出处: How to support multiple file extensions with one document type in Visual C++ MFC applications

How to support multiple file extensions with one document type in Visual C++ MFC applications

Article ID:198538
Last Review:November 21, 2006
Revision:4.1
This article was previously published under Q198538

SUMMARY

DocMgrEx.exe is a sample that shows how to support multiple extensions associated with one document type (or template) in MFC's Doc/View architecture.

This sample implements the code required to allow registration, unregistration, file open, and file save operations in an otherwise regular MFC application. The classes touched include the following:

The CWinApp-derived class.
The undocumented CDocManager class.
A custom CFileDialog-derived class that allows transparent file operations with the newly defined CDocManagerEx.

Back to the top

MORE INFORMATION

The following files are available for download from the Microsoft Download Center:

DocMgrEx.exe (http://download.microsoft.com/download/vc60pro/sample11/1/nt4/en-us/docmgrex.exe)

NOTE: To Build this project with VS .NET, change the path in the #include of AfxImpl.h on line 472 of DocManagerEx.cpp:
Change from:
#include <.."src"afximpl.h>

To:
#include <.."src"mfc"afximpl.h>

For more information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:
119591 (http://support.microsoft.com/kb/119591/) How to obtain Microsoft support files from online services
Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help prevent any unauthorized changes to the file.
To change the extensions supported by different document types, or to add new document types, edit the application's string table. In the string resource associated with the document type resource ID, the fourth ""n" delimited substring should read as follows:
   DocType files (.ex1;.ex2;ex3)
The 5th substring should read as follows:
   .ex1;.ex2;.ex3
NOTE: DocType is the external name of the document type; and ex1, ex2, ex3 are the extensions you want to associate with your document type.

When testing the application, you will notice its behavior is similar to DevStudio with regard to file open/save operations. For example, when trying to open a file, the user specifies only a file name (without an extension), the application will successively append the extensions from the currently active filter, trying to build the file name of an existing file. If this yields no result, the application will either tell the user that the file does not exist (if the OFN_FILEMUSTEXIST flag was specified) or it will create a new file with no extension. Saving operations are very similar.

Back to the top

Implementation Details

To implement correct registration code we needed to override CDocManager::RegisterShellFileTypes().

Unregistration is a bit more complicated, because the corresponding functions in CWinApp and CDocManager were not declared virtual. Therefore, we needed to implement the correct code in an override of CDocManager::UnregisterShellFileTypes(), and copy and paste the code from CWinApp::UnregisterShellFileTypes() and CWinApp::ProcessShellCommand() in our own CWinApp-derived class.

To implement file operations transparently, we overrode CDocManager::DoPromptFileName() and CDocManager::OpenDocumentFile(). To minimize the number of changes, the global function MatchDocType() has been defined to replace the use of CDocTemplate::MatchDocType() member function. The new function behaves as expected when multiple extensions are provided for one document template.

Back to the top

REFERENCES

For more information, click the following article number to view the article in the Microsoft Knowledge Base:
141921 (http://support.microsoft.com/kb/141921/) How to support two file extensions per MFC document type

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值