如果该菜单附加到一个对话框,在 Visual c + + 中则不能从其命令用户界面处理程序更改的菜单项的状态


症状:

   从其命令用户界面 (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 成员函数添加到您的对话框类,并将下面的代码 (请注意,此代码很大程度上取自 CFrameWnd::OnInitMenuPopup WinFrm.cpp 中) 复制:

 

void CTestDlg::OnInitMenuPopup(CMenu *pPopupMenu, UINT nIndex,BOOL bSysMenu)
{
    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;
    }
}

 更多信息:

  // 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. 通过使用应用程序向导创建 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.生成并运行该应用程序。


 


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值