XIZAO2011-The Garden of XiangDragon

Research on Service Computing,WebServices Search Engine and so on

统计

01科研资訊

02 Deep Learning & Wind Energy

Blog Online

Email access

English Net

International conference

Magazine

News and entertainment

Online Tools

Other Links

Polytechnic

Research SRC Online

System Developing

小说

最新评论

动态设置菜单项为灰色(VC实现)

          在网上有很多关于寻求关于“如何实现动态设置菜单为灰色(或激活态)”的问题,看了很多回贴,但能很好实现这一功能的回贴还是不多,特别是基于对话框模式下。经过查找资料,找到了一比较简单的方法基本可以解决上面的问题。这里主要响应两个消息:ON_WM_INITMENUPOPUP和ON_UPDATE_COMMAND_UI。相应的函数是:OnInitMenuPopup(CMenu *pPopupMenu, UINT nIndex,BOOL bSysMenu)和OnUpdateNew(CCmdUI* pCmdUI)在第二个函数中要添加的代码比较简单,要难很多,下面是实现第个函的完整代码,希望对你有用:
void CCSADlg::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;
    }

}

在自己类的中加上上面的代码后,基本上能实现动态设置菜单项为灰色。

示例源程序(如有问题请在下面留言) 

posted on 2007-05-20 18:44 XIZAO2011-The Garden of XiangDragon 阅读(2638) 评论(0)  编辑 收藏 引用 所属分类: 专业技术类

只有注册用户登录后才能发表评论。