是否可以操纵我的CMDIFrameWndEx的CMFCMenuBar的菜单? [英] Is it possible to manipulate the menu of my CMDIFrameWndEx's CMFCMenuBar?

查看:933
本文介绍了是否可以操纵我的CMDIFrameWndEx的CMFCMenuBar的菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

有一件事情,但我不喜欢的是简单地禁用菜单项在 ON_UPDATE_COMMAND_UI 处理程序中。

该框架有一个名为 GetMenuBar()的方法,但是那个给我一个 const CMFCMenuBar * ,所以我不能从外面修改它。我添加了一个getter,所以我得到一个非常量的参考菜单栏,但是没有工作:

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();

上述代码是适应 void CMyMenuBar :: AddSubMenu()中的 DynamicMenu 样本。 我有感觉,这个例子是坏的,因为我找不到该特定的代码是执行还是什么时候。在示例中,代码只在菜单栏被复位时被执行或者没有状态已经保存到注册表中。

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?

推荐答案

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

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.

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

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).

使用它的诀窍是弄清楚如何识别被调用的菜单。一种方法是比较已知项目的菜单项ID。例如,如果菜单上的第一个项目是ID_FILE_OPEN,则可以查找该项目。如果找到,您可以假设您的文件菜单是正在打开的菜单,您可以添加自定义子菜单。

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.

可能您也可以使用 dwMenuData 成员的 MENUINFO struct,虽然我似乎记得有问题,由于 CMFCMenuBar 建立菜单。

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.

这篇关于是否可以操纵我的CMDIFrameWndEx的CMFCMenuBar的菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆