VC ++ notes-supports the visual structure of multiple document types

Source: Internet
Author: User
Previous Page

8.7 Support multiple document-based structured programs

To support multiple document types, you can create and register an additional cmultidoctemplate object in the cwinapp derived class object. To add additional document types to an MFC application, the procedure can be divided into five steps. Next we try to add the text editor in the previous chapter to the drawing program. In this way, the program not only supports drawing, but also supports text editing.

(1) Use classwizard to create a new document class and View class:

Because you already have the preceding text editor program, you only need to copy the file, and then run the project-> Add to project-> files command to edit the editordoc. h. editordoc. CPP, editorview. h. editorview. CPP is added to the project.

(2) Use the resource editor to add new strings for the new document type.

First, let's take a look at the document template String Structure of the drawing program. Open the string editor and find the idr_drawtype string, which is defined as follows:

\ Ndraw files (*. drw) \ n. drw \ ndraw. Document \ ndraw document

The document template string contains seven substrings ending with '\ n' and separated. If the substring does not contain '\ n', it will appear as a placeholder character.' \ n' is not required after the last string '. These substrings describe the types of documents, which represent:

1. window title: for example, Microsoft Word. This string only appears in the SDI program and is blank for multi-document programs. Therefore, idr_drawtype starts with \ n.

2. Document Name: Create a new document name when you select a new menu item from the File menu. The new document name uses the document name string as the prefix, followed by a number to use as the default new file name, such as "draw1" and "draw2. If not specified, "Untitled" is used as the default value.

3. New Document Type name: when the application supports multiple document types, this string is displayed in the file new dialog box. If this parameter is not specified, you cannot use the file-new menu to create a document of this type.

4. Filter Name: allows you to specify descriptions related to this document type. This description is displayed in the Type drop-down list in the open dialog box.

5. Filter Suffix: used together with the filter name to specify the extension of the file associated with the document type. We have previously specified the plotting program as ". drw ".

6. Mark the document type ID in the registration database maintained by windows. When the application is running, the ID is added to the registration database. In this way, file manager can open the corresponding application through the ID and the registered document type name below.

7. Registration document type name: it is stored in the registration database and the name that identifies the document type.

Now we want to add the document template string of the text editor. Add a string resource in the string editor and specify the ID as idr_editortype. The content is:

\ Neditor files (*. txt) \ n.txt \ neditor. Document \ neditor document

(3) Use the resource editor to add additional icons and menu resources. Note that the IDs of these resources must be the same as those used in the document template string created in step 2. The cmultidoctemplate class uses this ID to identify all resources (including icons and menus) related to the attached document type ). After opening the draw project workspace file, you can use project-insert project into workspace to add the editor project file to the draw project workspace. Copy the resource from the editor to the draw project and change it to idr_editortype.

(4) In the initinstance () method of the application class, create another cmultidoctemplate object and use the cwinapp: adddoctemplate () member function to register the template object. The modified code is as follows:

Cmultidoctemplate * pdoctemplate;

Pdoctemplate = new cmultidoctemplate (

Idr_drawtype,

Runtime_class (cdrawdoc ),

Runtime_class (cchildframe), // custom MDI child frame

Runtime_class (cdrawview ));

Adddoctemplate (pdoctemplate );

 

Cmultidoctemplate * pdoctemplate2 = new cmultidoctemplate (idr_editortype,

Runtime_class (ceditordoc ),

Runtime_class (cmdichildwnd), runtime_class (ceditorview ));

Adddoctemplate (pdoctemplate2 );

(5) add custom serialization methods and drawing methods to the newly added document and view classes.

This step has been completed before ceditordoc and ceditorview.

Compile and run the program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.