如何确定菜单项是否启用? [英] How do you determine whether a menu item is enabled?

查看:21
本文介绍了如何确定菜单项是否启用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用 EnableMenuItem 启用或禁用菜单项一>.您如何确定现有项目是否已启用?

A menu item can be enabled or disabled using EnableMenuItem. How do you determine whether an existing item is enabled?

推荐答案

菜单项是否启用是作为菜单项状态信息的一部分存储的.以下函数报告是否启用了菜单项(由 ID 标识):

Whether a menu item is enabled is stored as part of a menu item's state information. The following function reports, whether a menu item (identified by ID) is enabled:

bool IsMenuItemEnabled( HMENU hMenu, UINT uId ) {
    UINT state = GetMenuState( hMenu, uId, MF_BYCOMMAND );
    return !( state & ( MF_DISABLED | MF_GRAYED ) );
}

关于实现的一些注意事项:

A few notes on the implementation:

  • 菜单项可以同时具有 MF_DISABLEDMF_GRAYED 状态.禁用的项目看起来就像启用的项目,但在其他方面处于非活动状态.不能选择禁用或变灰的项目.1)
  • MF_ENABLED 状态等于 0.因此,不能直接测试它,但必须使用表达式代替(请参阅 GetMenuState).
  • A menu item can have both MF_DISABLED and MF_GRAYED states. A disabled item looks just like an enabled item, but is otherwise inactive. Neither disabled nor grayed items can be selected.1)
  • The MF_ENABLED state equates to 0. As a consequence, it cannot be tested directly, but an expression must be used instead (see GetMenuState).


为了完整起见,这里有一个使用较新 API 的实现(GetMenuItemInfo).两种实现在功能上是相同的:


For completeness, here's an implementation using the newer API (GetMenuItemInfo). Both implementations are functionally identical:

bool IsMenuItemEnabled( HMENU hMenu, UINT uId ) {
    MENUITEMINFO mii = { 0 };
    mii.cbSize = sizeof( mii );
    mii.fMask = MIIM_STATE;
    GetMenuItemInfo( hMenu, uId, FALSE, &mii );
    return !( mii.fState & MFS_DISABLED );
}


1)灰色项目和禁用项目之间的区别记录在 关于菜单:启用、变灰和禁用的菜单项.这种区别在较新的 API 中不再公开(请参阅 MENUITEMINFO),其中 MFS_DISABLEDMFS_GRAYED 具有相同的值.


1)The distinction between grayed and disabled items is documented under About Menus: Enabled, Grayed, and Disabled Menu Items. This distinction is no longer exposed in the newer APIs (see MENUITEMINFO), where both MFS_DISABLED and MFS_GRAYED have the same value.

这篇关于如何确定菜单项是否启用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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