是否可以操作 CMDIFrameWndEx 的 CMFCMenuBar 的菜单?

发布于 2024-09-26 20:28:32 字数 845 浏览 1 评论 0原文

我的主框架有一个 CMFCMenuBar 成员,其中包含当前文档类型的菜单。 我想动态添加/删除子菜单。 例如,如果用户选择显示地图窗格,我想在“文件”菜单旁边添加一个地图子菜单。

反之亦然,如果地图窗格关闭,我也想删除地图菜单项。

一件可行但我不喜欢的事情是简单地禁用 ON_UPDATE_COMMAND_UI 处理程序中的菜单项。
Frame 有一个名为 GetMenuBar() 的方法,但该方法返回一个 const CMFCMenuBar *,因此我无法从外部修改它。我添加了一个 getter,这样我就获得了对菜单栏的非常量引用,但这也不起作用:

CMenu menu;
VERIFY(menu.LoadMenu(IDR_MAP));
CMFCMenuBar & menuBar = pFrm->GetNonConstMenuBar(); // Custom getter
menuBar.InsertButton(CMFCToolBarMenuButton(0, menu, -1));
menuBar.AdjustLayout();
menuBar.AdjustSizeImmediate();

上面的代码是 中的 void CMyMenuBar::AddSubMenu () 的改编DynamicMenu 示例。 但我有一种感觉,这个示例已损坏,因为我无法确定该特定代码是否或何时执行。 在示例中,仅在重置菜单栏时才执行代码或者当尚未将任何状态保存到注册表时。

这是不可能的还是我做错了什么?

除了添加/删除子菜单之外,还有其他好的选择吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稳稳的幸福 2024-10-03 20:28:32

一种方法是处理 WM_INITMENUPOPUP 消息。在显示菜单之前,将使用指向菜单的指针调用OnInitMenuPopup

请注意,每次菜单即将弹出时都会调用此函数,并且您所做的任何更改都会在调用之间丢失(每次调用 OnInitMenuPopup 时都必须添加菜单项)。

使用它的技巧是弄清楚如何识别已调用的菜单。一种方法是比较已知项目的菜单项 ID。例如,如果菜单上的第一项是 ID_FILE_OPEN,您可以查找它。如果找到,您会认为您的“文件”菜单就是正在打开的菜单,并且您可以添加自定义子菜单。

也许您还可以使用 MENUINFO 结构的 dwMenuData 成员,尽管我似乎记得由于 CMFCMenuBar 构建菜单。

One way to do this is to handle the WM_INITMENUPOPUP message. Just before a menu is displayed, OnInitMenuPopup will get called with a pointer to the menu.

Note that this gets called each time the menu is about to pop up, and any changes you make are lost between invocations (you have to add your menu items each time OnInitMenuPopup is called).

The trick to using this is figuring out how to identify the menu that has been invoked. One way would be to compare the menu item ID of a known item. For example, if the first item on the menu is ID_FILE_OPEN, you could look for that. If found, you would assume that your “File” menu is the one being opened, and you could add your custom submenu.

Possibly you could also use the dwMenuData member of the MENUINFO struct, although I seem to recall having problems with this due to the way CMFCMenuBar builds the menu.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文