void CGfxPopupMenu::EnableMenuItems(CMenu * pMenu, CWnd * pParent) { ASSERT(pMenu); ASSERT(pParent); int nItem = pMenu->GetMenuItemCount(); CCmdUI state; state.m_pMenu = pMenu; state.m_nIndex = nItem-1; state.m_nIndexMax = nItem; while ((--nItem)>=0) { UINT itemId = pMenu->GetMenuItemID(nItem); if (itemId == (UINT) -1) { CMenu *pops = pMenu->GetSubMenu(nItem); if (pops) EnableMenuItems(pops, pParent); } else { if (itemId != 0) { state.m_nID = itemId; pParent->OnCmdMsg(itemId, CN_UPDATE_COMMAND_UI, &state, NULL); state.DoUpdate(pParent, true); } } state.m_nIndex = nItem-1; } }
// 参看:http://www.cnblogs.com/hcfalan/archive/2010/11/24/1886139.html BOOL CDlgImageViewer::ContinueModal() { if(m_ToolBar.IsWindowVisible()) { CFrameWnd* pParent = (CFrameWnd*)m_ToolBar.GetParent(); if(pParent) m_ToolBar.OnUpdateCmdUI(pParent, (WPARAM)TRUE); } CMenu* pMainMenu = GetMenu(); CCmdUI cmdUI; if (pMainMenu != NULL) { for (int n = 0; n < pMainMenu->GetMenuItemCount(); ++n) { CMenu* pSubMenu = pMainMenu->GetSubMenu(n); cmdUI.m_nIndexMax = pSubMenu->GetMenuItemCount(); for (int i = 0; i < cmdUI.m_nIndexMax; ++i) { cmdUI.m_nIndex = i; cmdUI.m_nID = pSubMenu->GetMenuItemID(i); cmdUI.m_pMenu = pSubMenu; cmdUI.DoUpdate(this, FALSE); } } } return CDialogEx::ContinueModal(); }
void CModelessMain::OnInitMenuPopup(CMenu* pPopupMenu, UINT /*nIndex*/, BOOL bSysMenu) { if (!bSysMenu) { ENSURE(pPopupMenu != NULL); // check the enabled state of various menu items CCmdUI state; state.m_pMenu = pPopupMenu; ASSERT(state.m_pOther == NULL); 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, FALSE); // popups are never auto disabled } else { // normal menu item // Auto enable/disable if command is _not_ a system command state.m_pSubMenu = NULL; state.DoUpdate(this, state.m_nID < 0xF000); } } } }
void CDocSelector::OnUpdateCmdUI( CFrameWnd* pTarget, BOOL bDisableIfNoHndler ) { CCmdUI state; state.m_pOther = this; state.m_nIndexMax = 1; // there's only one thing to update state.m_nID = AFX_IDW_STATUS_BAR; // allow the statusbar itself to have update handlers if ( CControlBar::OnCmdMsg( state.m_nID, CN_UPDATE_COMMAND_UI, &state, NULL ) ) return; // allow target (owner) to handle the remaining updates state.DoUpdate( pTarget, FALSE ); }
void CProgressBar::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler) // I'm not entirely sure how much of this is necessary, but it doesn't seem to // do any harm, and things didn't work properly until I put it in... { CCmdUI state; state.m_pOther = this; state.m_nIndexMax = (UINT32)m_nCount; for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax; state.m_nIndex++) { state.m_nID = 0; state.DoUpdate(pTarget, bDisableIfNoHndler); } // update the dialog controls added to the status bar (of which there are none) UpdateDialogControls(pTarget, bDisableIfNoHndler); }
//Стандартный вызов ON_UPDATE_COMMAND_UI запрещает элементы меню, но не делает их grayed. //Чтобы все таки запрещенные элементы меню выглядели как grayed, нужно вызывать эту функцию //Note: эта функция не перерисовывает меню. //Возвращает true - если произошли изменения и нужно перерисовать меню, иначе false bool CMainFrame::_UpdateTopLevelMainMenu(void) { CMenu* pMenu = this->GetMenu(); std::vector<UINT> old_menu_state; //remember old states of all menu items { int menu_items_count = pMenu->GetMenuItemCount(); for(int i = 0; i < menu_items_count; ++i) old_menu_state.push_back(pMenu->GetMenuState(pMenu->GetMenuItemID(i),MF_BYCOMMAND)); } //Perform update CCmdUI ui; ui.m_nIndexMax = pMenu->GetMenuItemCount(); ui.m_pMenu = pMenu; for (ui.m_nIndex = 0; ui.m_nIndex < ui.m_nIndexMax; ui.m_nIndex++) { ui.m_nID = pMenu->GetMenuItemID(ui.m_nIndex); ui.DoUpdate(this, m_bAutoMenuEnable); } //Check: do we need to redraw menu? { int menu_items_count = pMenu->GetMenuItemCount(); for(int i = 0; i < menu_items_count; ++i) { UINT old_s = old_menu_state[i]; UINT new_s = pMenu->GetMenuState(pMenu->GetMenuItemID(i),MF_BYCOMMAND); if (old_s!=new_s) return true; //caller should redraw main menu } } //nothing changed: redraw is not needed return false; }
void CIMoteTerminal::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; } }
//This method should be called when a pop-up menu is about to become active void CDiagnostContextMenuManager::OnInitMenuPopup(CMenu* pMenu, UINT nIndex, BOOL bSysMenu) { ASSERT(m_pWnd); if (bSysMenu) return; // don't support system menu ASSERT(pMenu != NULL); // check the enabled state of various menu items CCmdUI state; state.m_pMenu = pMenu; 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 == pMenu->m_hMenu) state.m_pParentMenu = pMenu; // parent == child for tracking popup else if ((hParentMenu = ::GetMenu(m_pWnd->m_hWnd)) != NULL) { CWnd* pParent = m_pWnd->GetTopLevelParent(); // 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) == pMenu->m_hMenu) { // when popup is found, m_pParentMenu is containing menu state.m_pParentMenu = CMenu::FromHandle(hParentMenu); break; } } } } state.m_nIndexMax = pMenu->GetMenuItemCount(); for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax; state.m_nIndex++) { state.m_nID = pMenu->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 = pMenu->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(m_pWnd, FALSE); // 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(m_pWnd, TRUE); } // adjust for menu deletions and additions UINT nCount = pMenu->GetMenuItemCount(); if (nCount < state.m_nIndexMax) { state.m_nIndex -= (state.m_nIndexMax - nCount); while (state.m_nIndex < nCount && pMenu->GetMenuItemID(state.m_nIndex) == state.m_nID) { state.m_nIndex++; } } state.m_nIndexMax = nCount; }//for }
void CISenTekAssistantDlg::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu) { ASSERT(pPopupMenu != NULL); CCmdUI state; state.m_pMenu = pPopupMenu; ASSERT(state.m_pOther == NULL); ASSERT(state.m_pParentMenu == NULL); HMENU hParentMenu; if (AfxGetThreadState()->m_hTrackingMenu == pPopupMenu->m_hMenu) state.m_pParentMenu = pPopupMenu; else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL) { CWnd* pParent = this; 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) { 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; ASSERT(state.m_pOther == NULL); ASSERT(state.m_pMenu != NULL); if (state.m_nID == (UINT)-1) { 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; } state.DoUpdate(this, TRUE); } else { state.m_pSubMenu = NULL; state.DoUpdate(this, FALSE); } 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; } }
void FileScrapDropDlg::OnInitMenuPopup(CMenu* pPopupMenu, xpr_uint_t nIndex, xpr_bool_t bSysMenu) { ASSERT(pPopupMenu != XPR_NULL); //super::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu); // for multi-language BCMenu *pBCPopupMenu = dynamic_cast<BCMenu *>(pPopupMenu); if (pBCPopupMenu != XPR_NULL) { xpr_uint_t sId; xpr_sint_t sCount = pBCPopupMenu->GetMenuItemCount(); const xpr_tchar_t *sStringId; const xpr_tchar_t *sString; xpr::string sMenuText; CommandStringTable &sCommandStringTable = CommandStringTable::instance(); xpr_sint_t i; for (i = 0; i < sCount; ++i) { sId = pBCPopupMenu->GetMenuItemID(i); // apply string table if (sId != 0) // if sId is 0, it's separator. { if (sId == -1) { // if sId(xpr_uint_t) is -1, it's sub-menu. pBCPopupMenu->GetMenuText(i, sMenuText, MF_BYPOSITION); sString = gApp.loadString(sMenuText); pBCPopupMenu->SetMenuText(i, (xpr_tchar_t *)sString, MF_BYPOSITION); } else { sStringId = sCommandStringTable.loadString(sId); if (sStringId != XPR_NULL) { sString = gApp.loadString(sStringId); pBCPopupMenu->SetMenuText(sId, (xpr_tchar_t *)sString, MF_BYCOMMAND); } } } } } // Check the enabled state of various menu items. CCmdUI sState; sState.m_pMenu = pPopupMenu; ASSERT(sState.m_pOther == XPR_NULL); ASSERT(sState.m_pParentMenu == XPR_NULL); // Determine if menu is popup in top-level menu and set m_pOther to // it if so (m_pParentMenu == XPR_NULL indicates that it is secondary popup). HMENU sParentMenu; if (AfxGetThreadState()->m_hTrackingMenu == pPopupMenu->m_hMenu) sState.m_pParentMenu = pPopupMenu; // Parent == child for tracking popup. else if ((sParentMenu = ::GetMenu(m_hWnd)) != XPR_NULL) { CWnd *sParentWnd = this; // Child windows don't have menus--need to go to the top! if (sParentWnd != XPR_NULL && (sParentMenu = ::GetMenu(sParentWnd->m_hWnd)) != XPR_NULL) { xpr_sint_t sIndex; xpr_sint_t sIndexMax = ::GetMenuItemCount(sParentMenu); for (sIndex = 0; sIndex < sIndexMax; ++sIndex) { if (::GetSubMenu(sParentMenu, sIndex) == pPopupMenu->m_hMenu) { // When popup is found, m_pParentMenu is containing menu. sState.m_pParentMenu = CMenu::FromHandle(sParentMenu); break; } } } } sState.m_nIndexMax = pPopupMenu->GetMenuItemCount(); for (sState.m_nIndex = 0; sState.m_nIndex < sState.m_nIndexMax; ++sState.m_nIndex) { sState.m_nID = pPopupMenu->GetMenuItemID(sState.m_nIndex); if (sState.m_nID == 0) continue; // Menu separator or invalid cmd - ignore it. ASSERT(sState.m_pOther == XPR_NULL); ASSERT(sState.m_pMenu != XPR_NULL); if (sState.m_nID == (xpr_uint_t)-1) { // Possibly a popup menu, route to first item of that popup. sState.m_pSubMenu = pPopupMenu->GetSubMenu(sState.m_nIndex); if (sState.m_pSubMenu == XPR_NULL || (sState.m_nID = sState.m_pSubMenu->GetMenuItemID(0)) == 0 || sState.m_nID == (xpr_uint_t)-1) { continue; // First item of popup can't be routed to. } sState.DoUpdate(this, XPR_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. sState.m_pSubMenu = XPR_NULL; sState.DoUpdate(this, XPR_FALSE); } // Adjust for menu deletions and additions. xpr_uint_t sCount = pPopupMenu->GetMenuItemCount(); if (sCount < sState.m_nIndexMax) { sState.m_nIndex -= (sState.m_nIndexMax - sCount); while (sState.m_nIndex < sCount && pPopupMenu->GetMenuItemID(sState.m_nIndex) == sState.m_nID) { sState.m_nIndex++; } } sState.m_nIndexMax = sCount; } }