动态菜单使用mfc [英] Dynamic menu using 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?
推荐答案
除了添加项目,我还有一个消息映射条目,因为我不知道id。 CMenu
对象动态如下:
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");
对于消息映射,假设所有菜单项ID都在一定范围内, ON_COMMAND_RANGE
以将整个范围映射到单个函数。此函数将接收ID作为参数,在函数中,您可以根据ID执行不同的操作。
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.
这篇关于动态菜单使用mfc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!