First: Dynamically change the menu according to the conditions of the program
Rendering:
(When the security button is "allowed")
(When the security button is "prohibited")
2: Implementation steps
For a better description, a small program that enables four numbers can be implemented and calculated to calculate their average.
1: Create a dialog application in the VC's MFC AppWizard (EXE), and others are default
2: Point VC menu "Insert" - "resource creates a menu, finally as shown in the figure:
3: Because it is a dynamically generated menu option, we have to implement its functions, but don't be afraid, very simple 2 steps,
A: Open the resource.h in your project to join the ID of the menu you want to generate, such as:
#define IDM_SUM 32772 / / Manual Add in Menu IDM_SUM
#define idm_average 32773 / / Manual Add in Menu IDM_AVERAGE
B: Message mapping in Begin_Message_Map (CMenuofdlgdlg, CDIALOG)
ON_COMMAND (IDM_SUM, ONSUM) // Manually add IDM_SUM to the function onsum mapping
ON_COMMAND (IDM_average, onaverage) // Manually add IDM_average to the mapping of the function onAverage
4: Generate a menu when needed
// Dynamically generate submenu function implementation
Void cmenuofdlgdlg :: onallow ()
{
// Todo: Add Your Control Notification Handler Code Here
IF (! ballow)
{
Cmenu * pmenu = getMenu ();
CMenu * psubmenu = pmenu-> getSubmenu (0);
Psubmenu-> InsertMenu (0, MF_BYPOSITION, IDM_SUM, "Squiration");
Psubmenu-> InsertMenu (1, MF_BYPOSITION, IDM_AVERAGE, "Ask for average");
// psubmenu-> insertmenu (NULL, MF_BYCOMMAND 1, NULL, "Help");
Ballow = True;
M_Allow.SetWindowText ("Prohibited Calculation");
}
Else
{
Cmenu * pmenu = getMenu ();
CMenu * psubmenu = pmenu-> getSubmenu (0);
Psubmenu-> DeleteMenu (IDM_SUM, MF_BYCOMMAND);
Psubmenu-> DeleteMenu (IDM_average, mf_bycommand);
BALLOW = false;
M_Allow.SetWindowText ("Allow Calculation");
}
}
It is generated in such a dynamic menu.
CMenu class method:
Appendmenu adds a new option to a given menu end checkmenuitem () In the pop-up menu, put a test mark in the next menu item or cancel a test mark from a menu item CHECKMENURADIOITEM () in this group, put one The radio button is placed on the menu item or discloses an existing radio button EnableMenuItem () activates menu item or disables menu items or disables menu items and makes it getting upMenuContexthelpid () retrieves the help of the menu combined with the menu Adding IDGetMenuitemid. ) Get menu items getMenustate () get the state of the specified menu item or the menu item member getMenUString () get the specified menu item, getSubMenu () get the specified menu item, getSubMenu () get the pointer to the Pointer INSERTMENU. () Inserting a new menu item in the specified location Moves other options to change MODIFYMENU () Change the existing menu item RemoveMenu () from the specified menu to the specified menu SetMenuContextHelpid () Settings with the menu The associated help context IDSETMENUITEMBITMAPS () associated with the menu item 4 (Southwest Jiaotong University Emei Campus 01 3 class Li Jianchao)