Sun Xin VC Study Notes 6 (3)

Source: Internet
Author: User

Cmenuapp is not derived from cwnd, so the MessageBox function cannot be used.
However, you can use the global MessageBox function: afxmessagebox function.
Int afxmessagebox (lpctstr lpsztext, uint ntype, uint nidhelp );
Order of the Test Response to the menu item: View-doc-mainframe-app
Message category
Standard Messages, except wm_command, all messages starting with WM. Classes derived from cwnd can receive such messages.

The command message comes from the menu, accelerator key, or toolbar button. These messages are all presented as wm_command.
In MFC, different command messages are distinguished by the menu item ID;
In the SDK, The wparam parameter of the message is recognized. Classes derived from csf-target can receive such messages.
A notification message is generated by the control, for example, when a button is clicked or a list box is selected,
In order to notify the parent window (usually a dialog box) of the event.
Such messages are also presented in the form of wm_command. Classes derived from csf-target can receive such messages.

Cwnd is derived from c1_target
Cwnd: getmenu
Cmenu * getmenu () const;
Cmenu encapsulates menu-related operations and Windows hmenu (menu handle)
Cmenu: getsubmenu
Cmenu * getsubmenu (INT NPOs) const; // obtain the sub-menu
Cmenu: checkmenuitem
Uint checkmenuitem (uint nidcheckitem, uint ncheck); // return the previous state of the item
Cmenu: setdefaultite
// Bool setdefaultitem (uint uitem, bool fbypos = false) // sets the default menu item
The specified default menu item will be blacklisted.
Caution! Only one default menu item can be found in one sub-menu.
Cmenu: setmenuitembitmaps
// Bool setmenuitembitmaps (uint nposition, uint nflags,
Const cbitmaps * pbmpunchecked, const cbitmaps * pbmpchecked)
Create a graphic tag menu
Obtain the bitmap size of the graphic tag menu:
Int getsystemmetrics (INT nindex)
Nindex: // System Metric or configuration setting
Sm_cxmenucheck // The height of the bitmap marked by the default menu
Sm_cymenucheck // The default menu to mark the width of the bitmap
Cstring STR;

Str. Format ("x = % d, y = % d", getsystemmetrics (sm_cxmenucheck ),
Getsystemmetrics (sm_cymenucheck ));
MessageBox (STR );

// Change the menu item status
Cmenu: enablemenuitem
Uint enablemenuitem (uint nidenableitem, uint nenable );
Caution! Only when m_bautomenuenable = false is set in the cmainframe constructor,
The enablemenuitem function takes effect.
When m_bautomenuenable = false, MFC will not use its command update mechanism to determine which menu item can be used.
Which menu item cannot be used? At this time, these operations must be completed by ourselves.

How to cancel the entire menu:
Cwnd: setmenu
// Bool setmenu (cmenu * pmenu );
Setmenu (null); // remove the current menu
Reload menu:
Cmenu menu; // it cannot be a local object.
Menu. loadmenu (idm_mainframe/× menu ID */) // dynamically change the menu
Setmenu (& menu );
Menu. Detach (); // If menu is a local object, you must call the detach () function, which disconnects the menu handle from our c ++ object,
In this way, the menu will not be destroyed when the local object cmenu is parsed.
The command update mechanism used by MFC for menu items: 01: 18: 00
Command update
The maintenance of menu item status depends on the cn_update_command_ui message. who captures the cn_update_command_ui message,
MFC creates a ccmdui object. We can add messages in message ing manually or by using classwizard.
On_update_command_ui macro to capture the cn_update_command_ui message.
In the background, the operating system sends the wm_initmenupopup message, which is then taken over by the basic class of MFC, such as cframewnd.
It creates a ccmdui object and is associated with the first menu item. It calls a member function doupdate () of the object ().
This function sends a cn_update_command_ui message with a pointer to the ccmdui object. Same
The ccmdui object is set to be associated with the second menu item, so that the order is performed until all menu items are completed.
The update command UI handler is only applicable to projects in pop-up menu items, and cannot be applied to permanently displayed top-level menu items.

Void cmainframe: onupdateeditcut (ccmdui * pcmdui)
{
Pcmdui-> enable (); // The default parameter of the Enable () function is bool type true.
}
The icon on the toolbar is the same as the ID of the corresponding menu item. Therefore, when the menu item is disable or enable
In the toolbar.
Caytion! : If the menu item ID is used during the process of changing the menu item status, the menu item status change can be corresponding on the toolbar
The icon is displayed, but if the position index of the menu item is used, the corresponding status is not displayed on the toolbar.
This is because the icons on the toolbar and corresponding menu items have the same ID number, but the location index is different.

6th menu Programming
1. pop-up menus cannot be used for command response.

2. If the menu item message in MFC uses classwizard to respond to the menu item message in the preceding four categories, the order of menu message transmission is as follows: view class -- Doc class -- cmainframe class -- app class. Once a menu message responds in one of the classes, it no longer finds the Response Function in other classes.
Details:
When you click a menu item, the cmainframe framework class receives the menu item message first. The cmainframe framework class delivers the menu item message to its subwindow View class, the View class processes the menu item first. If the View class detects that the menu item message is not responded to, the view class submits the menu item message to the doc class of the document class for processing; if the doc class detects that no response is made to the menu item message in the doc class, the doc class returns the menu item message to the View class, and the View class returns it to the cmainframe class for processing. If the cmainframe class shows that the cmainframe class does not respond to the message, it is finally handed over to the app class for processing.

3. Message classification: Standard Messages, command messages, and announcement messages.
[Standard message]: All messages starting with WM _ Except wm_command.
[Command Message]: Message from menu, accelerator key, or toolbar button. These messages are all presented as wm_command.
In MFC, different command messages are distinguished by the menu item ID. In SDK, messages are identified by the wparam parameter.
[Advertise a message]: a message generated by a control, such as clicking a button or selecting a list box, is generated to notify its parent window (usually a dialog box) of an event. Such messages are also presented in the form of wm_command.
Note:
1) classes derived from cwnd can receive [standard messages].
2) classes derived from cve-target can receive [command messages] and [notification messages].

4. A menu bar can have several sub-menus, and a sub-menu can have multiple menu items. Create an index starting from 0 on the left to right of the sub-menu bar. The top to bottom menu items of the special sub-menu are indexed starting from 0. You can access sub-menus and menu items through their indexes or identifiers (if any.
Related important functions:
Cmenu * getmenu (); // cwnd: getmenu.
Cmenu * getsubmenu (); // cmenu: getsubmenu
Uint checkmenuitem (); // cmenu: checkmenuitem adds check marks to or removes check marks from menu items in the pop-up menu.
Bool setdefaultitem (); // cmenu: setdefaultitem sets the default menu item for the specified menu.
Bool setmenuitembitmaps (); // cmenu: setmenuitembitmaps sets the bitmap Title menu.
Uint enablemenuitem (); // cmenu: enablemenuitem makes the menu item valid, invalid, or grayed out.
Bool setmenu (cmenu * pmenu); // cwnd: setmenu set a new menu or remove menu in the current window.
Hmenu detach (); // cmenu: Detach detaches a Windows menu from a cmenu object and returns the handle.
Note:
1) when calculating the index of the sub-menu item, the separator is also regarded as an index.
2) int getsystemmetrics () obtains the system information measurement. You can use it to obtain the size of the menu title to set the size of the bitmap in the bitmap Title menu.
3) In MFC, MFC provides us with a command update mechanism, which is used to update all menu items. Therefore, to use cmenu: enablemenuitem to control the menu usage or not to use gray change, you must set the variable m_bautomenuenable to false in the cmainframe constructor.
4) create a cmenu object on the stack frame as a local, then call cmenu's member functions to manipulate the new menu as needed. next, call cwnd: setmenu to set the menu to a window, followed immediately by a call to the cmenu object's detach member function. the cwnd: setmenu member function sets the window's menu to the new menu, causes the window to be redrawn to reflect the menu change, and also passes ownership of the menu to the window. the call to detach detaches the hmenu from the cmenu object, so that when the local cmenu variable passes out of scope, the cmenu object destructor does not attempt to destroy a menu it no longer owns. the menu itself is automatically destroyed when the window is destroyed.
5) You can use the loadmenuindirect member function to create a menu from a template in memory, but a menu created from a resource by a call to loadmenu is more easily maintained, and the menu resource itself can be created and modified by the menu editor.

6) Example:
Cmenu menu; // defines it as a local object.
Menu. loadmenu (idr_mainframe );
Setmenu (& menu );
Menu. Detach (); // The Menu object is a local object. Detach () is used to detach the window menu handle from the menu object, so that the window menu resources are not destroyed when the menu object is destructed.

5. Command update mechanism:
Maintenance of the menu item status depends on the cn_update_command_ui message. When the cn_update_command_ui message is captured, MFC creates a ccmdui object.
Wm_initmenupopup messages are sent in the background operating system. Then, the basic class of MFC, such as cframewnd, takes over and creates a ccmdui object and associates it with the first menu item to call the object member function doupdate () (Note: this function does not find the description in msdn) sends a cn_update_command_ui message with a pointer to the ccmdui object. After that, the same ccmdui object is set to be associated with the second menu item, so that the order is executed until all menu items are completed.
The update command UI handler is only applicable to projects in pop-up menu items, and cannot be applied to permanently displayed top-level menu items.
Note:
1) You can manually or use classwizard to add update_command_ui message responses to menu items. You can use the ccmdui object pointer in the Response Function to set menu items that can be used, unavailable, and grayed out, set the tag menu and other operations.

6. to associate an icon on the toolbar with a single dish of a menu item, set the icon ID to the ID of the menu item.
The index count order of the toolbar icons is: from 0 to the right, the separator is also counted as the index number.

7. Add the VC popmenu control to the project: Project-> Add to project-> components and controls ..
Content added by the System: A, a menu resource; B, added the oncontextmenu () function in the derived View class
Note:
1) cwnd: oncontextmenu called by the framework when the user has clicked the right mouse button (right clicked) in the window. you can process this message by displaying a Context Menu Using the trackpopupmenu.
2) bool trackpopupmenu (uint nflags, int X, int y, cwnd * pwnd, lpcrect lprect = NULL );
// Cmenu: trackpopupmenu displays a floating pop-up menu at the specified location and tracks the selection of items on the pop-up menu. A floating pop-up menu can appear anywhere on the screen.

8. manually add the pop-up menu by calling the trackpopupmenu function:
1) use resource manager to add a menu resource
2) Right-click the message response function, load the menu resource, obtain the sub-menu pointer to be displayed, and call the trackpopupmenu function to complete the task (note: the coordinates transmitted by the mouse response function are client zone coordinates, while the trackpopupmenu function uses screen coordinates. Before calling trackpopupmenu, call clienttoscreen to convert the client Region coordinates to the screen coordinates)
Example code:
Cmenu menu;
Menu. loadmenu (idr_menu1 );
Cmenu * ppopup = menu. getsubmenu (0 );
Clienttoscreen (& Point );
Ppopup-> trackpopupmenu (tpm_leftalign | tpm_rightbutton, point. X, point. Y, this );
Note:
Cwnd: clienttoscreen (...); // coordinates a coordinate point or a rectangular area into screen coordinates.
Cmenu: trackpopupmenu (...); // the pop-up menu is displayed at the specified position.
Cwnd: screentoclient (..);
// Converts the screen coordinates of a given point or rectangle on the display to client coordinates.

9. When the pop-up menu is in the Framework Window (which can be set in the trackpopupmenu function parameter), the message on the menu is displayed. During routing, still follow the response sequence of view-doc-mainframe-app.

10. Dynamic menu programming:
All resource objects have a data member who saves the resource handle.
Cmenu: appendmenu // appends a new item to the end of a menu.
Cmenu: createpopupmenu // creates an empty pop-up menu and attaches it to a cmenu object.
Cmenu: insertmenu
// Inserts a new menu item at the position specified by nposition and moves other items down the menu.
Cmenu: getsubmenu // retrieves a pointer to a pop-up menu.
Cwnd: getmenu // retrieves a pointer to the menu for this window.
Cmenu: deletemenu // deletes an item from the menu.

11. manually add a response function to the dynamic menu item:
You can add resource IDs in resource. h.
Prototype of the message function written in the header file
Add message ing and message response functions in the code file
Note:
You can first create a temporary menu item, set its ID to be consistent with the dynamic menu item, then send a message response to it using the wizard, and then delete the temporary menu.
Put the message ing outside the macro in the code file (Be sure to put it outside the macro, because the classwizard finds that the menu has been deleted, and delete the message ing in its macro pair)

12, cwnd: drawmenubar
// Redraws the menu bar. If a menu bar is changed after Windows has created the window, call this function to draw the changed menu bar

Cwnd: getparent // get a pointer to a Child Window's parent window (if any ).
Cwnd: invalidate // pay attention to the role of its parameters

13. Collection class:
Cstringarray, cstringarray, cdwordarray, cptrarray, cstringarray, cuintarray, cwordarray
Member functions:
Carray: getat
Carray: add

14. The command message is routed to the oncommand function.
As cwnd: oncommand is a virtual function, you can rewrite the oncommand function in the framework class to intercept menu messages so that it does not route down (View class.
Example:
Bool cmainframe: oncommand (wparam, lparam)
{
// Todo: add your specialized code here and/or call the base class
Int menustmid = loword (wparam); // obtain the command ID
Cmenu2view * pview = (cmenu2view *) getactiveview (); // obtain the current view class pointer
If (menustmid> = idm_phone1 & menustmid <idm_phone1 + pview-> m_strarray.getsize () // determine the message range
{
Cclientdc DC (pview );
DC. textout (, pview-> m_strarray.getat (MenuCmdId-IDM_PHONE1 ));
Return true;
// Function return to avoid calling the cframewnd: oncommand function. messages intercepted in cframewnd: oncommand are handled by the View class.
}
Return cframewnd: oncommand (wparam, lparam );
// Call the oncommand function of the base class. messages intercepted in cframewnd: oncommand are processed by the View class.
}
Msdn description:
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.
Oncommand processes the message map for control notification and on_command entries, and callthe appropriate member function.
Override this member function in your derived class to handle the wm_command message. an override will not process the message map unless the base class oncommand is called.

15. loword and hiword macros
Word loword (
DWORD dwvalue // value from which low-order word is retrieved
);
Word hiword (
DWORD dwvalue // value from which high-order word is retrieved
);

// The loword macro retrieves the low-order word from the given 32-bit value.
// The hiword macro retrieves the high-order word from the given 32-bit value.

16, cframewnd: getactiveview
Cview * getactiveview () const; // obtain the current window port pointer (in the single-document Framework)

17. The source files are independently compiled.

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.