如何在 MFC 中获取子菜单? [英] How to Get Submenu in MFC?

查看:23
本文介绍了如何在 MFC 中获取子菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取一个子菜单,以便在它显示之前对其进行更改.

I'm trying to get a submenu so that I can make changes to it before it is displayed.

所以我为我的窗口创建了一个 OnInitMenu() 处理程序.我曾计划使用 pMenu->GetMenuItemInfo() 来获取子菜单.

So I created an OnInitMenu() handler for my window. And I had planned to use pMenu->GetMenuItemInfo() to get the submenu.

但是,这似乎不起作用.为了找到我想要的菜单,我必须提供菜单命令 ID(我不认为硬编码项目位置令人满意).但是打开子菜单的菜单项没有命令 ID.我可以得到一个存在于该子菜单中的菜单命令,但是我仍然没有菜单本身.

However, it doesn't appear this will work. In order to locate the menu I want, I must supply the menu command ID (I do not consider it satisfactory to hard code item positions). But menu items that open submenus do not have command IDs. I can get a menu command that exists inside that submenu, but then I still don't have the menu itself.

如何在不依赖 MF_BYPOSITION 的情况下找到嵌套在主菜单中的子菜单?

How can I locate a submenu nested in my main menu, without relying on MF_BYPOSITION?

推荐答案

看来答案是你不能.使用命令 ID 定位菜单命令非常有意义,因为这样的代码将在您重新排列菜单项时继续工作.但是,作为子菜单的菜单项根本没有命令 ID.

It appears the answer is that you can't. Using command IDs to locate a menu command makes great sense because such code will continue to work as you rearrange menu items. However, menu items that are sub menus simply do not have a command ID.

一种方法是使用已知的菜单命令,您可以按 ID 搜索该命令,然后在该命令旁边插入新项目.但是,您仍然需要包含菜单.

One approach is to have a known menu command, which you can search for by ID, and then insert new items next to that command. However, you still need the containing menu.

我最终使用的方法是研究 MFC 用于填充文件"菜单中的最近使用文件列表的代码.一般技术在有点过时的 Paul DiLascia's Q &来自 Microsoft Systems Journal 的专栏.

The approach I ended up using resulted from studying the code MFC uses to populate the most recently used file list in the File menu. The general technique is described in the somewhat dated Paul DiLascia's Q & A column from Microsoft Systems Journal.

这篇关于如何在 MFC 中获取子菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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