Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic menu using mfc

Tags:

c++

mfc

I would like to add a menu item to my main menu and then populate it with items at run time. How would I do this? And besides adding items how would I have a message map entry for them since I do not know the id?

like image 270
Ira Avatar asked Sep 09 '10 03:09

Ira


1 Answers

You can create a CMenu object dynamically like this:

CMenu *menu = new CMenu;
menu->CreatePopupMenu();
// Add items to the menu
menu->AppendMenu(MF_STRING, menuItemID, "Text");
...

Then add this sub-menu to your main menu:

wnd->GetMenu()->AppendMenu(MF_POPUP, (UINT_PTR)menu->m_hMenu, "Menu Name");

As for the message map, assuming all your menu item IDs are within a certain range, you can use ON_COMMAND_RANGE to map the entire range to a single function. This function will receive the ID as a parameter, and within the function, you can perform different operations based on the ID.

like image 93
casablanca Avatar answered Sep 28 '22 05:09

casablanca