從命令用戶界面處理函數(shù)(Command UI handler)改變菜單狀態(tài)(啟用/禁用,選擇/取消選擇,更改文字)在由對話框處理時沒有正常工作。
void CTestDlg::OnUpdateFileExit(CCmdUI* pCmdUI
{
pCmdUI->SetCheck(FLAG); // FLAG記錄菜單項的狀態(tài)
}
原因
在下拉菜單顯示的時候, WM_INITMENUPOPUP消息被先發(fā)送以顯示菜單項。MFC CFrameWnd::OnInitMenuPopup 函數(shù)遍歷菜單項并為每個菜單項調(diào)用更新命令處理函數(shù)(如果有的話).菜單的外觀被更新以反映它的狀態(tài)(啟用/禁用,選擇/取消選擇)
更新用戶界面機制在基于對話框的應(yīng)用程序中不能工作,因為CDialog沒有OnInitMenuPopup 處理函數(shù),而使用CWnd's 默認(rèn)處理函數(shù),該函數(shù)沒有為菜單項調(diào)用更新命令處理函數(shù)。
解決
適用下列步驟解決此問題
1.在消息映射中添加ON_WM_INITMENUPOPUP 項:
BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
//{{AFX_MSG_MAP(CTestDlg)
........................
........................
//}}AFX_MSG_MAP
ON_WM_INITMENUPOPUP()
END_MESSAGE_MAP()
2.在你的對話框類中添加OnInitMenuPopup成員函數(shù)且復(fù)制下列代碼到該函數(shù)(注意:代碼基本上是從CFrameWnd::OnInitMenuPopup(在WinFrm.cpp中)復(fù)制過來的):
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;
}
}