我的菜单是弹出式的二级菜单,我的第二级菜单上有四个项,我想实现任意时刻只有一个项前面打勾,如果我选择另一个则原先的那个没有勾,而被点击的项前面带勾。
怎么实现,用什么函数。

解决方案 »

  1.   

    你自己通过判断来设定菜单的状态
    在菜单的更新函数里 
    OnUpdateXXX(CCmdUI* pCmdUI)
    pCmdUI->SetCheck(1);
      

  2.   

    没有错
    SetCheck(1) 表示 :选中
    SetCheck()表示 :清除
      

  3.   

    可以使用一个通用的过程
    ON_COMMAND_RANGE
    ON_UPDATE_COMMAND_UI_RANGE
      

  4.   

    点哪个的时候就设上这个,SetCheck(1) ,其他的就都SetCheck()就可以了
      

  5.   

    定义四个BOOL变量例如m_bMenu1,m_bMenu2,m_bMenu3,m_bMenu4
    void CTestDlg::OnUpdateMenu1(CCmdUI* pCmdUI) 
    {
    pCmdUI->SetCheck(m_bMenu1);

    }void CTestDlg::OnMenu1() 
    {
    m_bMenu1 = !m_bMenu1;
    m_bMenu2 = FALSE;
    m_bMenu3 = FALSE;
    m_bMenu4 = FALSE;
    }
      

  6.   

    可是这个OnUpdateXXX(CCmdUI* )函数是只要我改变菜单的项就会响应的,或者打开第二级菜单我把我要的是要点击后才产生我要的效果的啊,是不是的自己写消息映射。
      

  7.   

    选中菜单ID,右键类向导,Message Maps,Messages中选中UPDATE_COMMAND_UI,Add Function即可
      

  8.   

    CMenu::CheckMenuRadioItem( UINT nIDFirst, UINT nIDLast, UINT nIDItem, UINT nFlags );或
    ::CheckMenuRadioItem(HMENU hmenu UINT nIDFirst, UINT nIDLast, UINT nIDItem, UINT nFlags )
    nIDFirst,nIDLast为这一组菜单项的ID范围
      

  9.   

    还要记住如果是dialog-based的程序,对话框上的菜单要想改变状态,还要在MESSAGE MAP中添加ON_WM_INITMENUPOPUP(),并加入如下代码:
    void CXXXDlg::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;
        }
    }后才可以实现改变菜单项的状态.
      

  10.   

    骇,问题解决了,我老想在OnUpdateXXX(CCmdUI* )在这函数里面改变状态标志,骇有是人的脑子会短路的。