question

AndrewCunningham-7759 avatar image
0 Votes"
AndrewCunningham-7759 asked JeanineZhang-MSFT commented

CMFCMenuBar - adding sub menus dynamically while not removing any user customization

I am working with a legacy MFC application and there are requests to allow menu customization. This is something you get for "free" using MFC and the menu customization dialog. MFC will also load and save the menu state. Job done, right? Not so fast.

However, we also dynamically build some menus at program start up. For example File->Import-><list of import plugins>
There are many techniques for doing this with a CMFCMenuBar but they all manipulate the CMenu(s) and force the CMFCMenuBar to be updated from the modified CMenu

// for example
HMENU hMenu = mfcMenuBar->GetHMenu(); // get underlying menu handle
CMenu* menuMain = CMenu::FromHandle ( hMenu );

.... modify menus using CMenu APIs

mfcMenuBar->CreateFromMenu ( menuMain, false, true ); // force CMFCMenuBar to rebuild based on modified CMenu


This works fine.... except this blows away any Menu customization the user might have made during a previous session.

So the real limitation of the CMFCMenuBar is that although it is possible to add new "Buttons" to the top-level menu "toolbar" ( say alongside File/Edit etc) using InsertButton it seems impossible to insert new items INTO , say, the File Menu , or add items to say a submenu such as, File->Import, without reverting to using the above approach.

The two suggestions people I have found are to dynamically populate popup menus "on the fly" using CMainFrame::OnInitMenuPopup and/or CMainFrame::OnShowPopupMenu

This is doable, but requires a complex rewrite.
Any better ideas?




c++
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@AndrewCunningham-7759

You may try to use MENUINFO structure. You could dynamically add a menu item and append data to the menu item. And then use command range for dynamic menu or create your own within the data set.

I suggest you could refer to the links:
https://stackoverflow.com/questions/3673546/dynamic-menu-using-mfc
https://stackoverflow.com/questions/19108015/use-command-range-for-dynamic-menu


0 Votes 0 ·

Thanks, we have no problems creating dynamic menus using the CMenu APIs as laid out in those links. It is exactly what we do now.
It seems the only way to manipulate the CMFCMenuBar to add dynamic menus is by manipulating the underlying Windows CMenu/HMENU ( a sort of parallel data structure)
The issue is that once you synchronize CMFCMenuBar with these modified Menus using CreateFromMenu , then any customization of CMFCMenuBar the user might have made is lost.
It is possible to add "new" top level menus using the ToolBar API ( as below), but not add a submenu to a menuitem of a toplevel menu.
https://stackoverflow.com/questions/3908655/is-it-possible-to-manipulate-the-menu-of-my-cmdiframewndexs-cmfcmenubar
https://stackoverflow.com/questions/20824154/how-to-customize-cmfcmenubar-in-an-mfc-application

0 Votes 0 ·

@AndrewCunningham-7759

It is possible to add "new" top level menus using the ToolBar API ( as below), but not add a submenu to a menuitem of a toplevel menu.

First of all, you should create a new submenu that contains the submenu items you want to add. And then you could create a new menu button and attach submenu to it.

0 Votes 0 ·

0 Answers