Dynamic menu using mfc(使用 mfc 的动态菜单)
问题描述
我想在我的主菜单中添加一个菜单项,然后在运行时用项目填充它.我该怎么做?除了添加项目之外,由于我不知道 id,我如何为它们提供消息映射条目?
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?
推荐答案
你可以像这样动态创建一个 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 的动态菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 mfc 的动态菜单


基础教程推荐
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07