解决 不能通过CCmdUI *pCmdUI改变对话框菜单状态 的问题

来源:互联网 发布:linux sort命令 编辑:程序博客网 时间:2024/05/16 10:34
 

1.Add an ON_WM_INITMENUPOPUP entry to the message map:

Code:
  1. BEGIN_MESSAGE_MAP(CTestDlg, CDialog)   
  2.     //}}AFX_MSG_MAP   
  3.   
  4.     ON_WM_INITMENUPOPUP()   
  5. END_MESSAGE_MAP()  

2.Add a OnInitMenuPopup member function to your dialog box class and copy the following code (note that this code is taken largely from CFrameWnd::OnInitMenuPopup in WinFrm.cpp):

Code:
  1. void CTestDlg::OnInitMenuPopup(CMenu *pPopupMenu, UINT nIndex,BOOL bSysMenu)   
  2. {   
  3.     ASSERT(pPopupMenu != NULL);   
  4.     // Check the enabled state of various menu items.   
  5.   
  6.     CCmdUI state;   
  7.     state.m_pMenu = pPopupMenu;   
  8.     ASSERT(state.m_pOther == NULL);   
  9.     ASSERT(state.m_pParentMenu == NULL);   
  10.   
  11.     // Determine if menu is popup in top-level menu and set m_pOther to   
  12.     // it if so (m_pParentMenu == NULL indicates that it is secondary popup).   
  13.     HMENU hParentMenu;   
  14.     if (AfxGetThreadState()->m_hTrackingMenu == pPopupMenu->m_hMenu)   
  15.         state.m_pParentMenu = pPopupMenu;    // Parent == child for tracking popup.   
  16.     else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL)   
  17.     {   
  18.         CWnd* pParent = this;   
  19.            // Child windows don't have menus--need to go to the top!   
  20.         if (pParent != NULL &&   
  21.            (hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL)   
  22.         {   
  23.            int nIndexMax = ::GetMenuItemCount(hParentMenu);   
  24.            for (int nIndex = 0; nIndex < nIndexMax; nIndex++)   
  25.            {   
  26.             if (::GetSubMenu(hParentMenu, nIndex) == pPopupMenu->m_hMenu)   
  27.             {   
  28.                 // When popup is found, m_pParentMenu is containing menu.   
  29.                 state.m_pParentMenu = CMenu::FromHandle(hParentMenu);   
  30.                 break;   
  31.             }   
  32.            }   
  33.         }   
  34.     }   
  35.   
  36.     state.m_nIndexMax = pPopupMenu->GetMenuItemCount();   
  37.     for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;   
  38.       state.m_nIndex++)   
  39.     {   
  40.         state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);   
  41.         if (state.m_nID == 0)   
  42.            continue// Menu separator or invalid cmd - ignore it.   
  43.   
  44.         ASSERT(state.m_pOther == NULL);   
  45.         ASSERT(state.m_pMenu != NULL);   
  46.         if (state.m_nID == (UINT)-1)   
  47.         {   
  48.            // Possibly a popup menu, route to first item of that popup.   
  49.            state.m_pSubMenu = pPopupMenu->GetSubMenu(state.m_nIndex);   
  50.            if (state.m_pSubMenu == NULL ||   
  51.             (state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||   
  52.             state.m_nID == (UINT)-1)   
  53.            {   
  54.             continue;       // First item of popup can't be routed to.   
  55.            }   
  56.            state.DoUpdate(this, TRUE);   // Popups are never auto disabled.   
  57.         }   
  58.         else  
  59.         {   
  60.            // Normal menu item.   
  61.            // Auto enable/disable if frame window has m_bAutoMenuEnable   
  62.            // set and command is _not_ a system command.   
  63.            state.m_pSubMenu = NULL;   
  64.            state.DoUpdate(this, FALSE);   
  65.         }   
  66.   
  67.         // Adjust for menu deletions and additions.   
  68.         UINT nCount = pPopupMenu->GetMenuItemCount();   
  69.         if (nCount < state.m_nIndexMax)   
  70.         {   
  71.            state.m_nIndex -= (state.m_nIndexMax - nCount);   
  72.            while (state.m_nIndex < nCount &&   
  73.             pPopupMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)   
  74.            {   
  75.             state.m_nIndex++;   
  76.            }   
  77.         }   
  78.         state.m_nIndexMax = nCount;   
  79.     }   
  80. }