How do I associate multiple file extensions with the same document class?


Ans: In applications like Notepad and MS-Word we can open files with different file extensions. For example, in Notepad we can open a ‘.txt’ file as well as ‘.htm’ file. We can also implement this feature in our MDI application. For this create a document template object for each file extension that you want to use in your application. Here, we intend to create two objects, one associated with ‘.txt’ extension and the other with ‘.ini’ extension. Modify the InitInstance( ) function as shown below:

BOOL CMultiextApp::InitInstance( )

{

// AppWizard added code

CMultiDocTemplate* pDocTemplate ;

pDocTemplate = new CMultiDocTemplate ( IDR_MULTIETYPE, RUNTIME_CLASS ( CMultiextDoc ),            RUNTIME_CLASS (      CChildFrame ), RUNTIME_CLASS ( CMultiextView ) ) ;

AddDocTemplate ( pDocTemplate ) ;

CMultiDocTemplate* pDocTemplate1 ;

pDocTemplate1 = new CMultiDocTemplate ( IDR_TEXTTYPE, RUNTIME_CLASS(CMultiextDoc),                                  RUNTIME_CLASS ( CChildFrame ), RUNTIME_CLASS ( CMultiextView ) ) ;

AddDocTemplate ( pDocTemplate1 ) ;

// AppWizard added code

return TRUE;

}

For each object all the parameters except the resource ID must be same. We have kept the default resource ID for ‘.ini’ file. Create a resource string having ID IDR_TEXTTYPE for the ‘.txt’ file. Also add the icon and menu having ID IDR_TEXTTYPE. Modify the string so that it contains suitable filterName and filterExt substrings. Now you can create or open both the text files and ini files.

Subscribe / Share

It's very calm over here, why not leave a comment?

Leave a Reply




Categories

Powered by Yahoo! Answers