My main frame has a CMFCMenuBar member, which contains the menu of the current document type. I would like to add/remove a sub-menu dynamically. For example, if the user chooses to display a map pane, I want to add a map submenu next to the "File" menu.
Vice versa, if the map pane gets closed, I also want to remove the map menu items.
One thing that works but which I don't like is to simply disable the menu items in the ON_UPDATE_COMMAND_UI
handlers.
The Frame has a method called GetMenuBar()
but that one returns me a const CMFCMenuBar *
so I can't modify it from the outside. I added a getter so I get a non-const reference to the menu bar but that didn't work either:
CMenu menu;
VERIFY(menu.LoadMenu(IDR_MAP));
CMFCMenuBar & menuBar = pFrm->GetNonConstMenuBar(); // Custom getter
menuBar.InsertButton(CMFCToolBarMenuButton(0, menu, -1));
menuBar.AdjustLayout();
menuBar.AdjustSizeImmediate();
The above code is an adaption of void CMyMenuBar::AddSubMenu ()
in the DynamicMenu
sample. I have the feeling though, that this sample is broken as I couldn't find out if or when that particular code is executed. In the sample, the code is only executed when the menu bar is being reset or when no state has been saved to the registry yet.
Is this just not possible or am I doing something wrong?
Would there be a nice alternative to adding/removing a sub-menu?