Click here to Skip to main content
15,891,184 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 
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 
Hi all.

I already know the solution, though I'm getting quite a lot of mails how did I solve this problem.

Window's main menu has items, which are submenus, and submenus has items
which are usually final items, or (somtimes) further submenus. Usual menu looks like this:

menu=
submenu1 submenu2 submenu3 ....
item11 item21 item31
item12 item22 ...
... ...
etc.

But the menus are stored dynamically, means: menu has only pointers to
each submenu, and when You call GetSubmenu(GetMainMenu(MainWindow,...));
it returns You the pointer to that submenu. But it is stored in some other
way (I've forgotten how and don't understand why), so when You set this
item, it seams to be changed (no error code). But it loads every time
dynamically - from the previous definition (From the resource file or menu
created firs time) and draws as You created it for the first time.

It is running on the main menu correctly, because it does not
reload from the resource. The main menu remains same all the time.

So the solution is:

when submenu1 has defined constatnt MI_ITEM1, submenu2 MI_ITEM2, etc:

//In windows API:
switch (msg)
case WM_COMMAND :
switch ( LOWORD(wParam) ) {
case ( MI_ITEM1 ) :
//Dynamically create submenu
PopupMenu = CreateMenu(...);
AppendMenu(PopupMenu, "Label 11", ..., CMD_ITEM11);
...

// show submenu:
TrackPopupMenuEx(PopupMenu, TPM_LEFTALIGN |
TPM_LEFTBUTTON, actualpos.x, actualpos.y, hWnd, NULL);

return TRUE;

switch (CMD_ITEM11) : ActionItem11();

// Using MFC:

CWhatEver::OnSubmenu1Click ( CWnd *pWnd, CPoint actualpos)
{
...
//Dynamically create submenu
PopupMenu = CreateMenu(...);
AppendMenu(PopupMenu, "Label 11", ..., CMD_ITEM11);
...

// show submenu:
TrackPopupMenuEx(PopupMenu, TPM_LEFTALIGN |
TPM_LEFTBUTTON, actualpos.x, actualpos.y, hWnd, NULL);

...
}

CWhatEver::OnItem11Click ( CWnd *pWnd, CPoint actualpos)
//means, when You click CMD_ITEM1
{
...
ActionItem11();
...
}

I hope it was understandable Smile | :) .

Email me for further questions.

Greetings

Martin Vrbovsky.



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.