VC ++ technical insider (Fourth Edition) Note (chapter 2)

Source: Internet
Author: User

/***************************************/

Chapter 2: menu, keyboard acceleration key, complex text editing control, and Attribute Table

1. The main frame window contains the title bar and menu bar, and various subwindows (including the toolbar window, status bar window, and View window) cover the customer area of the main frame window. Applications control the interaction between frameworks and views through message transmission between frameworks and views.

2. the MFC Application Framework provides a very complex command message passing system. These messages come from the menu selection, keyboard acceleration, and toolbar and dialog buttons. In addition, command messages can also be sent by calling the cwnd: sendmessage or postmessage function.
1) Most command messages come from the main framework window, but the application framework does not respond to them one by one. We need to add the Response Function in the appropriate place for response processing.
2) When the application framework receives the Framework Window command, it will look for its message control function in the following order:
SDI Program (single document): View (View class)-document (Doc class)-SDI main framework window (frame class)-Application Program (APP class ).
MDI Program (Multi-document): View -- document --> MDI child frame window --> MDI main frame window --> application program.
Note:
When the framework receives the Framework Window command and finds its message control function in the order of Ampersand, once it is found, it stops searching, in this way, when there are multiple message control functions in the path, only the one found first is executed.

3. The command passing system reflects one aspect of Command Message control. the hierarchical structure of classes reflects another aspect of control and processing of command messages.
1) generally, the derived class inherits all the message ing functions of the base class, including the Command Message functions.
2) If you want to overload the message ing function of the base class, you must add the corresponding message ing function and the corresponding message ing entry (and function declaration) to the derived class ).

4. All pop-up menus belong to the main frame window.
1) The buttons (and menu items in the resource editor) in the pop-up menu must be in the range of 0x8000-0xdfff to generate a command message that can be passed.
2) enter the ID in the symbol editor to ensure that the ID range falls within the 0x8000-0xdfff range.
(Note: the pop-up dialog box is in the book, but it should be a pop-up menu .)

5, cframewnd: m_bautomenuenable
// Controls automatic enable and disable functionality for menu items.
// The bool data member in the cframewnd class. The default value is true.
// If m_bautomenuenable is set to true, if a menu item cannot find the corresponding Command Message control function in the current command path, the application framework will disable this menu item, set this menu item to Gray.
// If m_bautomenuenable is set to false, you can disable the features of the menu items of the uncontrolled function.
(Try: add this-> m_bautomenuenable = true in the framework class constructor cmainframe: cmainframe (), and check the menu items in the menu to see what changes have taken place)

6. Select an MFC Text Editor
1) ceditview class: The text size is limited to 64 KB, and the mixed font cannot be set. This provides the basic compilation function.
2) cricheditview class: uses rich text editing controls to support mixed fonts and large data volumes of text.
3) cricheditctrl class: encapsulates rich text editing controls.
// Cricheditctrl a window in which the user can enter and edit text with character and paragraph formatting. The control can include embedded OLE objects.

7. Example:
In case ex12a (before optimization), use the cricheditctrl class to design the editing function in the project where the base class is the cview class:
1) method: Construct a cricheditctrl Class Object, create a control window and associate it with it, set the control window to the size of the current view customer area, and make the cricheditctrl class control window overwrite the current view customer area, the cricheditctrl control window has the rich text editing function to complete the design.
2) Implementation Details:
Cricheditctrl m_rich; // construct the cricheditctrl object
M_rich.create (es_autovscroll | es_multiline | es_wantreturn | ws_child | ws_visible | ws_vscroll, rect, this, 1); // create a control window to initialize the cricheditctrl object
M_rich.setwindowpos (& wndtop, rect. right-rect.left, rect. bottom-rect.top, swp_showwindow );
// Set the size of the cricheditctrl control window (use getclientrect (rect) to obtain the size of the view client area) and position, that is, overwrite the view client area.
3) techniques for obtaining and saving controls:
Get the object pointer of the corresponding Doc class in the View class: cex12adoc * pdoc = getdocument ();
Obtain the control window text: m_rich.setwindowtext (pdoc-> m_strtext );
Set the control window text: m_rich.getwindowtext (pdoc-> m_strtext );
Tip: Use the getdocument () function in the View class to obtain the object pointer of the corresponding document class, so that the window text of the m_rich control can be kept in m_strtext, a data member of the document class.
4) cricheditctrl: setmodify
Void setmodify (bool bmodified = true );
Bmodified "a value of true indicates (symbolic, indicating) that the text has been modified, and a value of false indicates it is unmodified. By default, the modified flag is set.
5) about the acceleration key: add it to the Resource Manager accelerator (for example, id_getdata vk_f2 ).

8. learning to use the Attribute Table
Here we provide a method to create an Attribute Table Using Resource Manager:
Step 1: use the resource manager to add several dialogs as property pages (this allows you to easily add and process controls to the property page ).
Step 2: Create classes for the property page dialog box, and use cpropertypage as the base class.
Step 3: Use the Class Wizard to create a user Attribute Table class. The base class uses cpropertysheet.
Step 4: add the required property page data member to the created user property table class, and call cpropertysheet in the property table class constructor :: the addpage function adds the required attribute pages to the Attribute Table one by one. So far, the property table class has been created.
Step 5: Construct the user Attribute Table object where you want to create and display the Attribute Table, and call cpropertysheet: domodal display.
Note:
1) cpropertysheet
Objects of class cpropertysheet represent property sheets, otherwise known as tab dialog boxes. A property sheet consists of a cpropertysheet object and one or more cpropertypage objects. A property sheet is displayed by the framework as a window with a set of tab indices, with which the user selects the current page, and an area for the currently selected page.

Even though cpropertysheet is not derived from cdialog, managing a cpropertysheet object is similar to managing a cdialog object.

Exchanging data between a cpropertysheet object and some external object is similar to exchanging data with a cdialog object.

2) cpropertypage
Objects of class cpropertypage represent individual pages of a property sheet, otherwise known as a tab dialog box. as with standard dialog boxes, you derive a class from cpropertypage for each page in your property sheet. to use cpropertypage-derived objects, first create a cpropertysheet object, and then create an object for each page that goes in the property sheet. call cpropertysheet: addpage for each page in the sheet, and then display the property sheet by calling cpropertysheet: domodal for a modal property sheet, or cpropertysheet :: create for a modeless property sheet.

3) processing of the Apply button:
In all Attribute Table classes, if the control sends a message to the property page (for example, click or select the control on the property page, the Framework calls the oncommand function on the property page to respond to the announcement messages sent by these controls. The oncommand function is a virtual function). In each property page, the oncommand function is used to set setmodified (true) make the Apply button valid.
When you click the Apply button, the Framework automatically calls the onapply function to reload it to complete some important tasks.
Cwnd: oncommand
Virtual bool oncommand (wparam, lparam );
// The framework callthis member function when the user selects an item from a menu, when a child control sends a notification message, or when an accelerator keystroke is translated.

Cpropertypage: onapply
Virtual bool onapply ();
// This member function is called by the framework when the user chooses the OK or the Apply Now button. when the framework CILS this function, changes made on all property pages in the property sheet are accepted, the property sheet retains focus, and onapply returns true (the value 1 ). before onapply can be called by the framework, you must have called setmodified and set its parameter to true. this will activate the Apply Now button as soon as the user makes a change on the property page.

Override this member function to specify what action your program takes when the user clicks the Apply Now button. When overriding, the function shocould return true to accept changes and false to prevent changes from taking effect.

The default Implementation of onapply callonok.

9. cmenu class
Cmenu objects can be used to represent various windows menus, including top menu items and corresponding pop-up menus.
How to add a menu:
Program addition method: 1) define menu resources; 2) construct menu objects; 3) Call cmenu: loadmenu function to load menu resources; 4) Call cwnd :: the setmenu function connects the menu to the Framework Window. 5) Call the cmenu: Detach function to separate the hmenu handle of the object. (In this way, when the cmenu object is destroyed, the menu is not destroyed)
/////////////////////////////////
/////////////////////////////////

Another simple menu addition method is as follows: if you want to add a menu for a dialog box, you can select the required menu in resource manager from the menu item in the dialog box properties to complete the addition.

10. Create a floating pop-up menu
Steps:
1) use the resource editor to add menu Resources
2) Add the wm_contextmenu message control function to the View class or other window classes that receive and right-click the message. The editing code is as follows:
Void caboutdlg: oncontextmenu (cwnd * pwnd, cpoint point)
{
Cmenu menu;
Menu. loadmenu (idr_menu1 );
Menu. getsubmenu (0)-> trackpopupmenu (tpm_leftalign | tpm_rightbutton, point. X, point. Y, this );
 
}
Note:
The getsubmenu function returns the cmenu pointer pointing to the pop-up menu.
Trackpopupmenu displays the floating menu at the specified position and tracks the selection items in the pop-up menu.

11. Processing of extended commands:
You cannot use the Wizard to add extension commands. You need to add them by yourself. The method for adding extension commands is similar to that for Common commands. For details, see the book P256-257 page.

////////////////////////
/////////////////////
Subsequent chapter notes to be continued

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.