如果菜单附加到 Visual C++ 中的对话框,则无法通过命令用户界面处理程序更改菜单项的状态。

本文可帮助你解决在 Visual C++ 中将菜单附加到对话框时发生的问题。

原始产品版本: Visual C++,.NET 2002
原始 KB 数: 242577

症状

注释

Microsoft Visual C++ .NET 2002 和 Visual C++ .NET 2003 都支持 .NET Framework 提供的托管代码模型和非托管本机 Windows 代码模型。 本文中的信息仅适用于非托管的 Visual C++ 代码。 Visual C++ 2005 支持 .NET Framework 提供的托管代码模型和非托管本机 Windows 代码模型。

如果菜单附加在对话框中时,通过其命令用户界面 (UI) 处理程序尝试更改菜单项状态(启用/禁用、勾选/取消勾选、更改文本),则可能无法正确运行。

void CTestDlg::OnUpdateFileExit(CCmdUI* pCmdUI)
{
    pCmdUI->Enable(FALSE); //Not calling the command handler, but does not show as disabled.
    pCmdUI->SetCheck(TRUE); // Does not show check mark before the text.
    pCmdUI->SetRadio(TRUE); // Does not show dot before the text.
    pCmdUI->SetText("Close"); //Does not change the text.
}

原因

显示下拉菜单时,在 WM_INITMENUPOPUP 显示菜单项之前会发送该消息。 MFC CFrameWnd::OnInitMenuPopup 函数循环访问菜单项,并调用项的更新命令 UI 处理程序(如果有)。 更新每个菜单项的外观以反映其状态(已启用/禁用、已选中/未选中)。

更新 UI 机制不适用于基于对话框的应用程序,因为 CDialog 没有 OnInitMenuPopup 处理程序,并且它使用 CWnd 的默认处理程序,该处理程序不为菜单项调用更新命令 UI 处理程序。

决议

使用以下步骤解决此问题:

  1. 在消息映射中添加一个 ON_WM_INITMENUPOPUP 条目。

    BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
    // }} AFX_MSG_MAP
    
    ON_WM_INITMENUPOPUP()
    END_MESSAGE_MAP()
    
  2. OnInitMenuPopup 成员函数添加到对话框类并复制以下代码:

    注释

    此代码主要摘自 WinFrm.cpp 中的 CFrameWnd::OnInitMenuPopup)

void CTestDlg::OnInitMenuPopup(CMenu *pPopupMenu, UINT nIndex,BOOL bSysMenu)
{
    // Make sure this is actually a menu. When clicking the program icon
    // in the window title bar this function will trigger and pPopupMenu 
    // will NOT be a menu.
    if (!IsMenu(pPopupMenu->m_hMenu))
		return;
        
    ASSERT(pPopupMenu != NULL);
    // Check the enabled state of various menu items.

    CCmdUI state;
    state.m_pMenu = pPopupMenu;
    ASSERT(state.m_pOther == NULL);
    ASSERT(state.m_pParentMenu == NULL);

    // Determine if menu is popup in top-level menu and set m_pOther to
    // it if so (m_pParentMenu == NULL indicates that it is secondary popup).
    HMENU hParentMenu;
    if (AfxGetThreadState()->m_hTrackingMenu == pPopupMenu->m_hMenu)
    state.m_pParentMenu = pPopupMenu; // Parent == child for tracking popup.
    else if ((hParentMenu = ::GetMenu(m_hWnd))!= NULL)
    {
        CWnd* pParent = this;
        // Child windows don't have menus--need to go to the top!
        if (pParent != NULL &&
        (hParentMenu = ::GetMenu(pParent->m_hWnd))!= NULL)
        {
            int nIndexMax = ::GetMenuItemCount(hParentMenu);
            for (int nIndex = 0; nIndex < nIndexMax; nIndex++)
            {
                if (::GetSubMenu(hParentMenu, nIndex) == pPopupMenu->m_hMenu)
            {
                // When popup is found, m_pParentMenu is containing menu.
                state.m_pParentMenu = CMenu::FromHandle(hParentMenu);
                break;
            }
            }
        }
    }

    state.m_nIndexMax = pPopupMenu->GetMenuItemCount();
    for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;
    state.m_nIndex++)
    {
        state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);
        if (state.m_nID == 0)
        continue; // Menu separator or invalid cmd - ignore it.

        ASSERT(state.m_pOther == NULL);
        ASSERT(state.m_pMenu != NULL);
        if (state.m_nID == (UINT)-1)
        {
            // Possibly a popup menu, route to first item of that popup.
            state.m_pSubMenu = pPopupMenu->GetSubMenu(state.m_nIndex);
            if (state.m_pSubMenu == NULL ||
            (state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||
            state.m_nID == (UINT)-1)
            {
            continue; // First item of popup can't be routed to.
            }
            state.DoUpdate(this, TRUE); // Popups are never auto disabled.
        }
        else
        {
            // Normal menu item.
            // Auto enable/disable if frame window has m_bAutoMenuEnable
            // set and command is _not_ a system command.
            state.m_pSubMenu = NULL;
            state.DoUpdate(this, FALSE);
        }

        // Adjust for menu deletions and additions.
        UINT nCount = pPopupMenu->GetMenuItemCount();
        if (nCount < state.m_nIndexMax)
        {
            state.m_nIndex -= (state.m_nIndexMax - nCount);
            while (state.m_nIndex < nCount &&
            pPopupMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)
            {
                state.m_nIndex++;
            }
        }
        state.m_nIndexMax = nCount;
    }
}

状态

此行为是设计造成的。

详细信息

还会从 CWnd::OnCommand 中调用更新命令 UI 处理程序,以确保在路由之前未禁用该命令。 这就是为什么当菜单项被禁用时,即使它显示为可用(但实际不可用),命令处理程序也不会被调用。 菜单项在这种情况下不会被绘制以显示其状态。 这是Wincore.cpp文件中的相关代码:

    // Make sure command has not become disabled before routing.
CTestCmdUI state;
state.m_nID = nID;
OnCmdMsg(nID, CN_UPDATE_COMMAND_UI, &state, NULL);
if (!state.m_bEnabled)
{
    TRACE1("Warning: not executing disabled command %d\n", nID);
    return TRUE;
}

重现行为的步骤

按照以下步骤在 Visual C++ .NET 中重现此行为:

  1. 使用 AppWizard 创建基于 MFC 对话框的应用程序。

  2. 创建新的菜单资源,并将“文件和文件/退出”菜单项添加到其中。

  3. 将此菜单设置为对话框“属性”窗口中对话框的菜单。 为此,请在对话编辑器中打开对话框资源。 在 “属性 ”窗口中,单击 “选择菜单”。 新菜单资源的 ID 显示在“菜单属性编辑器”下拉列表中。

  4. 添加文件/退出菜单项的UPDATE_COMMAND_UI处理程序。 为此,请在菜单编辑器中右键单击 “文件/退出 ”,然后单击“ 添加事件处理程序”。 在事件处理程序向导中,将 UPDATE_COMMAND_UI 处理程序添加到项目 CDialog 派生类。 单击 “添加编辑” 以创建处理程序,然后将以下语句之一添加到生成的处理程序方法:

    pCmdUI->Enable(FALSE); //Not calling the handler, but does not show as disabled
    pCmdUI->SetCheck(TRUE); // Does not show check mark before the text.
    pCmdUI->SetRadio(TRUE); // Does not show dot before the text.
    pCmdUI->SetText("Close"); //Does not change the text.
    
  5. 生成并运行应用程序。