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

Andrew Cunningham 21 Reputation points

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?

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,559 questions
{count} votes