Click here to Skip to main content
15,884,298 members
Articles / Desktop Programming / MFC

Dynamic submenus

Rate me:
Please Sign up or sign in to vote.
2.53/5 (8 votes)
20 Mar 2000 140.6K   3.9K   27   13
Some handy functions for adding and deleting submenus and menuitems in runtime

Sample Image - DynMenu.gif

Here are some short handy functions for adding and deleting submenus and menu items in runtime.

To add a menu with some items in the file menu at a specified index, use:

C++
MenuItemData data[] ={
  ID_COMMAND_1, "First Item",
  ID_COMMAND_2, "Second Item",
  0, NULL
};
CMenu *pMenu = AddSubMenu(pMainFrm, file_menu_index, sub_menu_index, "Dynamic Menu", data);

To append an item to the menu later on, use:

C++
AddSubMenuItem(pMenu, ID_COMMAND_3, "Third Item");

To remove a submenu again, use:

C++
RemoveSubMenu(pMainFrm, file_menu_index, sub_menu_index);

To remove only one item, use:

C++
RemoveSubMenuItem(pMenu, ID_COMMAND_1);

That's all. Enjoy!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Web Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow can I change a submenus caption at runtime? Pin
rajksingh30-Oct-03 19:19
rajksingh30-Oct-03 19:19 
GeneralThe one error I get. Pin
DarrollWalsh28-Sep-02 20:30
DarrollWalsh28-Sep-02 20:30 
GeneralCDialog versio, how to here Pin
DarrollWalsh28-Sep-02 20:28
DarrollWalsh28-Sep-02 20:28 
GeneralStatic Resource ID Pin
Anonymous12-Aug-02 16:03
Anonymous12-Aug-02 16:03 
GeneralRe: Static Resource ID Pin
DarrollWalsh28-Sep-02 20:21
DarrollWalsh28-Sep-02 20:21 
QuestionCreating view menu and sub menu? Pin
Wilson5-Aug-01 22:08
Wilson5-Aug-01 22:08 
QuestionCreating view menu and sub menu? Pin
Wilson5-Aug-01 22:08
Wilson5-Aug-01 22:08 
Questionwhat's function AddSubMenu, AddSubmenuItem? Pin
30-Jul-01 2:35
suss30-Jul-01 2:35 
GeneralFirst submenu Pin
16-Jul-01 10:40
suss16-Jul-01 10:40 
GeneralRe: First submenu Pin
Anneke Sicherer-Roetman16-Jul-01 21:28
Anneke Sicherer-Roetman16-Jul-01 21:28 
I do not quite understand. The way I see it, what you want is exactly what it does.
QuestionOut of topic... - How to disable/gray/enable normal menu item? Pin
Martin Vrbovsky6-Oct-00 1:58
sussMartin Vrbovsky6-Oct-00 1:58 
AnswerRe: Out of topic... - How to disable/gray/enable normal menu item? Pin
16-Dec-01 15:15
suss16-Dec-01 15:15 
AnswerRe: Out of topic... - How to disable/gray/enable normal menu item? Pin
Roman1982Murk6-Sep-07 14:23
Roman1982Murk6-Sep-07 14:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.