如何使用 MFC 禁用顶级菜单项并将其变灰 [英] How can I disable and gray the top level menu item using MFC

查看:79
本文介绍了如何使用 MFC 禁用顶级菜单项并将其变灰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对话框应用程序,我希望在对话框顶部有可点击的菜单项.这些项目不显示下拉菜单,但实际运行相关命令.

I have a dialog application in which I want to have clickable menu items at the top of the dialog. These items do not show a drop down menu but actually run the associated commands.

我通过在对话框属性中设置 Popup=False 并分配消息 ID 来做到这一点,但我的问题是当项目可点击没有意义时,我的问题是无法正确禁用项目(取决于内部状态)存储在对话框中)

I did this by setting Popup=False in the dialogs properties and assigning a message-id but my problem is not having the ability to disable the item properly when it makes no sense for the item to be clickable (depending on internal state stored in the dialog)

我已经找到了如何从 http 禁用任何弹出式父菜单项://www.microsoft.com/msj/0299/c/c0299.aspx,但这不是我想要的

I have already found out how to disable any popup-parent menu items from http://www.microsoft.com/msj/0299/c/c0299.aspx, but this isn't exactly what I want

我还从 msdn 知识库文章 KB242577 中了解了如何将菜单命令路由添加到对话框.

I have also found out how to add menu command routing to dialogs from the msdn knowledgebase article KB242577.

这适用于子菜单项,但不适用于顶级菜单.

This works fine for sub-menu items, but not for the top level menu.

我目前正在使用以下功能进行禁用

I am currently using the following function to do the disabling

void CYourDlg::EnableMenuItem(UINT nCommand, BOOL bEnable)
{
   CMenu* pMenu = GetMenu();
   pMenu->EnableMenuItem(nCommand, bEnable ? 0 : MF_DISABLED | MF_GRAYED);
}

这一半有效,如果您在应用程序之外按 alt-tab 键,它会显示为禁用,否则不会.

This half works, if you alt-tab away from the app it does show as disabled, otherwise it doesn't.

有没有办法以编程方式使该区域无效?

Is there a way to invalidate the area programmatically?

我认为可能涉及非客户区消息.

I think an non-client area message may be involved.

推荐答案

我没试过但是在普通窗口(不是对话框)CWnd::DrawMenuBar 应该做你想做的.它也可能适用于基于对话框的应用程序.

I have not tried but in regular window (not dialog) CWnd::DrawMenuBar should do what you want. It might work with dialog based applications as well.

void CYourDlg::EnableMenuItem(UINT nCommand, BOOL bEnable)
{
   CMenu* pMenu = GetMenu();
   pMenu->EnableMenuItem(nCommand, bEnable ? 0 : MF_DISABLED | MF_GRAYED);
   DrawMenuBar();
}

这篇关于如何使用 MFC 禁用顶级菜单项并将其变灰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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