Visual C + +. NET Programming Lecture IV

Source: Internet
Author: User
Tags constructor variables requires resource

3rd talk about the interaction between the menu, toolbar, and status bar

Summary

First of all, we use the resource editor to visually design the menus and toolbars, then discuss command and UPDATE_COMMAND_UI message mappings for commands, and describe the linkage methods of the tool buttons and menu commands, and finally discuss the display and hiding of toolbars, the implementation of shortcut menus, And how to display the specified text on the status bar.

Visual design of menu and its command mapping

In the last lecture, we discussed the reading and display of document data, where we first open the viewer for the single Document application project in the previous lecture and then design its menus and toolbars. It should be explained that Visual C + +. NET stores the graphical elements required by various Windows applications, such as menus, toolbars, dialogs, icons, cursors, and so on, as a resource that can be loaded into an application. For example, Visual C + +. NET will be the viewer application resources are stored in the Viewer.rc file, the source of the same source code separation mechanism, can greatly facilitate the user's operation, and each resource element with the corresponding resource ID number to identify.

1. Visual design of the menu

Before the menu design, we first look at the menu and its design specifications.

A menu can have a multilevel structure, where a menu item can have multiple submenus, and a submenu can contain multiple submenus at the next level, and so on. But in the actual design of the menu, the order of the menu is generally 2~3 level, and the design should pay attention to some menu principles. For example, if you click a menu item to pop up a dialog box, add "..." after the menu item text. If the menu item requires a mnemonic (underlined character), enclose it in parentheses, and for the top-level menu item, when holding down the ALT key and then keys the character corresponding to the mnemonic, The corresponding top-level menu is opened, and if the submenu item has a mnemonic, you can execute the menu command as long as the corresponding character is keys, and when you define a mnemonic, just precede the character with the "&" symbol, and if a menu requires the support of a shortcut key, it is usually listed after the corresponding menu item text.

The following adds a Format menu to the viewer item, with two items under it. One is the "Set font" menu, and the other is the text color menu, which is used to change the font and color of the text display. The specific steps are as follows:

(1) Switch Solution Explorer to Resource View without the label, open the View menu, and select the Resource View menu command.

(2) Expand all the nodes of the resource, double-click the Idr_mainframe under the menu, and open the menus resource of the program. It should be explained that all resources identified as Idr_mainframe are the default resources installed by the program framework.

(3) as shown in Figure 1, click "Enter Here" at the far right of the top-level menu, press the Insert key directly or right-click "Enter Here" and click "New Insert" in the menu that pops up. And then click "Enter Here" and the position becomes an editable text box with the caret present. Type the menu text content format (&m), and then press the ENTER key.

Figure 1 Top-level menu Resources

(4) Click enter here below the Format (&M) menu item, press the INSERT key, type the menu text content "Set font (&F)", and then press ENTER.

(5) Click Set Font (&F) and all of its properties are listed in the Properties window in the lower-right corner, as shown in Figure 2. It is important to note that in the Properties window, we can edit the text content of the menu and the ID of the resource ID again. The Caption (Caption) property is used to identify menu item display text. If a mnemonic is used, the letter must have a & symbol before it, and when the popup (eject) property is True, the menu item is a pop-up menu, that is, there are multiple submenus under the menu, and the property ID, Separator and prompt entries are not valid; Therefore, if you add a menu command that can be mapped, the Popup property must be set to false. When the separator (delimiter) property is True, the menu item is either a separator or a horizontal line, and the prompt (hint) property is used to indicate the message that appears on the status bar when the mouse pointer moves over the menu item.

Figure 2 Menu Properties

(6) Change the default menu item font (&F) identification id_130 to Id_fomat_txtfont. Edit directly in the box to the right of the ID bar in the Properties window when you change it, and press ENTER after you modify it.

(7) Repeat the steps above, add a menu item "text color (&c)" Under the Format menu, and the ID is id_format_txtcolor and the result is shown in Figure 3.

Figure 3 The menu added

(8) Click the Format menu to not release, and then drag and drop it between view and help.

2. Menu's command map

When you run the program, the commands on the Format menu are grayed out (that is, the color is grayed out) and we can't select the menu command because we haven't mapped the menu's command message yet. Here's a map, because the menu commands we added are to change the font that the text content displays, so we add the map of the menu command to the View class Cviewerview, as in the following procedure:

(1) Switch Solution Explorer to Class View, expand the node, select Cviewerview, and in its Properties window, click the Events button, and the result is shown in Figure 4.

Figure 4 Event Mappings

(2) Find the previously added menu item Id_format_txtfont, click the "+" in front of the ID, expand the message that can be mapped, because the menu message is the command message, because we are on the right side of the command message box, click then select "< add > Onformattxtfont ", as shown in Figure 4. The corresponding mapping is added to the Cviewerview class, where the document window is automatically positioned to the implementation code of the function.

Figure 5 Mapping command messages

(3) Repeat the previous step to add a command message map to the menu item Id_format_txtcolor.

Note: The response of the same command message is based on the level of the object, and for a single document application, the level of each object, from highest to lowest, is the View Class (document window), the document class, the main frame window class, the application class.

3. Perfect the "format" menu code

(1) Add two member variables to the Cviewerview class (the method for adding the member variable is discussed above), one is the m_lftextfont of the LogFont type, and the other is M_crtxtcolor of the COLORREF type. LogFont is a logical font type, called a "logical Font", which is a description of an ideal font for an application. When drawing text using logical fonts, the system uses a specific algorithm to map the logical font to the most matching physical font (the fonts that are actually installed in the operating system). COLORREF is specifically used to define the RGB color of the data type, RGB color is through the red (R), Green (G), Blue (B) Three of the different values of the base component of the mixed.

(2) Add the initialization code for the above two member variables in the constructor Cviewerview::cviewerview (), as shown in Figure 5.

Figure 5 Code added in the Cviewerview class constructor

(3) Add the code shown in Figure 6 in the Cviewerview::onformattxtfont () function.

Figure 6 Onformattxtfont () function code

The CFontDialog class provides us with a common dialog box for font and its text color selection, specifying a m_lftextfont pointer in the constructed object to set the logical font that the dialog box displays so that the current font characteristics are displayed when the Font dialog box is displayed.

(4) Add the code shown in Figure 7 in the Cviewerview::onformattxtcolor () function.

Figure 7 Onformattxtcolor () function code

The CColorDialog class encapsulates all the actions of the Universal Color dialog box. When you define a dialog object, you can specify a color value that is selected by default and, if not specified, the default color value is RGB (0,0,0) (black).

(5) Modify the Cviewerview::ondraw () function code, as shown in Figure 8 in the box section.

Figure 8 Modified OnDraw () function code

(6) Run the program, open the current directory of the ReadMe.txt document, open the "Format" menu, select the appropriate menu commands, change its font and color. Figure 9 is one of the results.

Figure 9 Program results after the run

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.