我的工程是基于对话框的工程,menu属于主对话框类
我是按照这个帖子做的
http://community.csdn.net/Expert/topic/2703/2703248.xml?temp=.8762934
我的程序如下:
void CSSD1983Dlg::OnMenucom1() 
{
bFlag=1;
}void CSSD1983Dlg::OnMenucom2() 
{
bFlag=2;
}void CSSD1983Dlg::OnUpdateMenucom1(CCmdUI* pCmdUI) 
{
pCmdUI->SetCheck(bFlag==1);
}void CSSD1983Dlg::OnUpdateMenucom2(CCmdUI* pCmdUI) 
{
pCmdUI->SetCheck(1);
}
可是程序运行点击Menu以后,它们的checked属性一点变化都没有,怎么回事啊,是不是因为
用了基于对话框的工程就不能这样啊?
大侠赐教,急死了!

解决方案 »

  1.   

    在对话框中响应WM_KICKIDLE,同时响应ON_COMMAND_UI
      

  2.   

    Message Map中添加消息響應了嗎?
    ON_UPDATE_COMMAND_UI( id, memberFxn )
      

  3.   

    LRESULT CMainDlgDlg::OnKickIdle(WPARAM , LPARAM )
    {
    UpdateMenuItems(GetMenu());
    return 0;
    }void CMainDlgDlg::UpdateMenuItems(CMenu* pMenu)
    {
    if(NULL==pMenu)return;
    UINT nCount = pMenu->GetMenuItemCount();
    CCmdUI cmdUI;
    cmdUI.m_nIndexMax = nCount;
    for(UINT n=0;n<nCount;++n){
    UINT nID = pMenu->GetMenuItemID(n);
    if(nID==UINT(-1)){
    CMenu* pSubMenu = pMenu->GetSubMenu(n);
    UpdateMenuItems(pSubMenu);
    }else{
    if(nID){
    cmdUI.m_nIndex = n;
    cmdUI.m_nID = nID;
    cmdUI.m_pMenu = pMenu;
    cmdUI.DoUpdate(this, FALSE);
    }
    }
    }
    }
      

  4.   

    OnKickIdle是响应WM_KICKIDLE,在消息映射中加一行
    ON_MESSAGE(WM_KICKIDLE,OnKickIdle)
      

  5.   


    再问一句,WM_KICKIDLE消息在添加消息的window_handler里面好像没有啊?
    它是对话框的消息吗?为什么没有呢?从哪里知道这个消息的?
      

  6.   

    我在对话框程序中用了 MENU, 是在 OnInitDialog中用 SetMenu设的程序一直运行良好, 但像楼上说的重载 WM_KICKIDLE后, 在这一句出错, UINT nCount = pMenu->GetMenuItemCount();跟踪发现, ::IsMenu(pMenu->m_hMenu);返回 FALSE;但我传入的 pMenu确实是通过 GetMenu();得到的究竟是什么回事?
      

  7.   

    我用了oninitmenupopup(),没用这个函数,不过还是谢谢大家了
      

  8.   

    请添加一下代码到程序中:afx_msg void OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu);
    BEGIN_MESSAGE_MAP(CTest6Dlg, CDialog)
    //{{AFX_MSG_MAP(CTest6Dlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
              。
    ON_WM_INITMENUPOPUP()//加这句
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    void CTest6Dlg::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu) 
    {
    CDialog::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);

    // TODO: Add your message handler code here
    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;
        }

      

  9.   

    是比较奇怪,按理说pCmdUI->SetCheck()就可以解决了阿!void CMainFrame::OnUpdateMnuTaxonomyView(CCmdUI* pCmdUI) 
    {
    // TODO: Add your command update UI handler code here
    // pCmdUI->SetCheck(m_viewTaxonomy.IsWindowVisible());
    }