我在處理一個(gè)對(duì)話框的菜單時(shí),想為兩個(gè)菜單命令添加單選標(biāo)記,,于是給兩個(gè)菜單都添加了UPDATE_COMAND_UI 事件,,在生成的函數(shù)里設(shè)置單選標(biāo)記,代碼如下:
- void CCaculatorDlg::OnUpdateToStd(CCmdUI *pCmdUI)
- {
- if (!m_bSwitch)
- {
- pCmdUI->SetCheck(1);
- }
- else
- {
- pCmdUI->SetCheck(0);
- }
- }
-
- void CCaculatorDlg::OnUpdateToSci(CCmdUI *pCmdUI)
- {
- if (m_bSwitch)
- {
- pCmdUI->SetCheck(1);
- }
- else
- {
- pCmdUI->SetCheck(0);
- }
- }
可是,,程序并沒(méi)有像預(yù)想的那樣出現(xiàn)單選標(biāo)記,。這種方法在單文檔或多文檔程序中很常用,根本不會(huì)出現(xiàn)問(wèn)題,,為什么在對(duì)話框程序中就不行了呢,?
折騰了一個(gè)多小時(shí),,終于在網(wǎng)上找到了問(wèn)題的所在和解決辦法,。引用原文:
Symptos:
Changing the state (enable/disable, check/uncheck, change text) of a menu item from its command user-interface (UI) handler does not work correctly if the menu is attached to a dialog box:
- void CTestDlg::OnUpdateFileExit(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(FALSE); //Not calling the command handler, but does not show as disabled.
- pCmdUI->SetCheck(TRUE); // Does not show check mark before the text.
- pCmdUI->SetRadio(TRUE); // Does not show dot before the text.
- pCmdUI->SetText("Close"); //Does not change the text.
- }
Cause:
When a drop-down menu is displayed, the WM_INITMENUPOPUP message is sent prior to displaying the menu items. The MFC
CFrameWnd::OnInitMenuPopup function iterates through the menu items and calls the update command UI handler for the item, if there is one. The appearance of each menu item is updated to reflect its state (enabled/disabled, checked/unchecked).
The update UI mechanism doesn't work for a dialog box-based application because
CDialog has no OnInitMenuPopup handler and it uses
CWnd's default handler, which does not call update command UI handlers for menu items.
Rusolution:
Use the following steps to resolve this problem:
1.Add an ON_WM_INITMENUPOPUP entry to the message map:
- BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
- //}}AFX_MSG_MAP
-
- ON_WM_INITMENUPOPUP()
- 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):
- void CTestDlg::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;
- }
- }
原來(lái)如此,按照解決方案做,,預(yù)期的效果真的出現(xiàn)了,!
|