如果菜单附加到 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. 生成并运行应用程序。