MFC 菜单项保持灰色 [英] MFC Menu Item remains grayed

查看:75
本文介绍了MFC 菜单项保持灰色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 Properties 的 CDialogEx 类,我在其中处理 ON_COMMAND 消息.现在,当我单击菜单项 ID_EDIT_PROPERTIES(作为主菜单的子菜单)时,应该调用 ON_COMMAND.事件处理程序向导为我编写了该代码,但是当我启动应用程序时,菜单项保持灰色.我尝试在 ON_UPDATE_COMMAND_UI 发生时通过调用 EnableMenuItem 手动激活它,但无济于事.

I have a CDialogEx Class called Properties in which I handle the ON_COMMAND message. Now, ON_COMMAND should get called when I click the menu item ID_EDIT_PROPERTIES (as a submenu from main menu). The event handler wizard wrote that code for me, but when I start the Application the menu item remains grayed out. I've tried to manually activate it by calling EnableMenuItem when ON_UPDATE_COMMAND_UI happens, but to no avail.

任何帮助将不胜感激.

推荐答案

您只需要了解如何处理菜单项启用/禁用:

You just need to understand how menu items enabling/disabling is handled:

  • 如果既没有 ON_COMMAND 也没有 ON_UPDATE_COMMAND_UI 处理程序,则项目被禁用.
  • 如果不存在 ON_UPDATE_COMMAND_UI 处理程序,但在当前活动的文档或视图(甚至大型机")中有一个 ON_COMMAND 处理程序,则该项目已启用.
  • 如果存在 ON_UPDATE_COMMAND_UI 处理程序,则启用/禁用该项目由处理程序决定 (pCmdUI->Enable(bEnableState)).

还要记住:

  • 您不能自己调用​​ EnableMenuItem(),而是在 ON_UPDATE_COMMAND_UI 处理程序中调用 pCmdUI->Enable(bEnableState).这不仅会影响菜单项,还会影响任何其他命令"类型的项(具有相同 ID),例如主菜单、上下文菜单、工具栏或钢筋按钮.
  • 将处理程序放在哪里是应用程序设计的问题,取决于您正在处理或表示的数据.它可以放在大型机类(如果它依赖于某些全局"数据或设置)、文档类(如果它依赖于或更改文档中的某些数据或设置——可能会影响所有视图)或视图类(-es)(仅取决于或影响当前视图).
  • You may not call EnableMenuItem() yourself, instead call pCmdUI->Enable(bEnableState) in an ON_UPDATE_COMMAND_UI handler. This affects not only the menu item, but any other "command"-type item (with the same ID), eg main menu, context menu, toolbar or rebar button.
  • Where to put the handler, is a matter of application design and depends on the data you are processing or representing. It can be put in the mainframe class (if it depends on some "global" data or setting), in the document class (if it depends on or changes some data or setting in the document - possibly affecting all views), or in the view class(-es) (depending on or affecting the current view only).

在您的情况下,如果我理解正确,该项目被禁用,因为处理程序位于 CDialogEx 派生类中,但尚未创建此类的实例,即您的 ID_EDIT_PROPERTIES 命令不存在 ON_COMMAND 处理程序.

In your case, if I understand correctly, the item is disabled because the handler is in the CDialogEx-derived class, but no instance of this class has been created yet, ie there exists no ON_COMMAND handler for your ID_EDIT_PROPERTIES command.

这篇关于MFC 菜单项保持灰色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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