MFC基于对话框菜单勾选无效解决方案
(2019-07-12 12:27:39)
标签:
mfc对话框菜单勾选勾选失效杂谈 |
分类: CPlusPlus基础 |
在单文档、多文档的程序实现菜单勾选只要实现对应菜单ID的ON_COMMAND、ON_UPDATE_COMMAND_UI即可。但是在对话中中实现这个你将会发现响应ON_COMMAND、ON_UPDATE_COMMAND_UI菜单依旧没有被勾选。主要原因是单文档和多文档长须默认依旧响应
WM_INITMENUPOPUP
这个消息,对应函数:OnInitMenuPopup,但是在对话框上没有实现这个函数。所以在勾选时没有勾上。
下面给出如何在对话框中实现勾选的解决方案:
1、响应
WM_INITMENUPOPUP 这个消息,对应函数:OnInitMenuPopup
void C****TestDlg::OnInitMenuPopup(CMenu*
pPopupMenu, UINT nIndex, BOOL bSysMenu)
{
CDialogEx::OnInitMenuPopup(pPopupMenu, nIndex,
bSysMenu);
//
TODO: Add your message handler code here
if
(bSysMenu)
return;
// don't support system menu
ENSURE_VALID(pPopupMenu);
//
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 =
pParent->GetMenu()->GetSafeHmenu())
!= NULL)
{
int
nIndexMax = ::GetMenuItemCount(hParentMenu);
for
(int nItemIndex = 0; nItemIndex < nIndexMax;
nItemIndex )
{
if
(::GetSubMenu(hParentMenu, nItemIndex) ==
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, 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(this, state.m_nID
< 0xF000);
}
//
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;
}
}
2、响应ON_COMMAND、ON_UPDATE_COMMAND_UI这个消息并进行实现:
void
C***TestDlg::OnMenuShow()
{
// TODO: Add your
command handler code here
if(m_IsShowFilter)
{
m_IsShow =
false;
}
else
{
m_IsShow =
true;
}
}
void
C*****TestDlg::OnUpdateMenuShow(CCmdUI *pCmdUI)
{
// TODO: Add your
command update UI handler code here
pCmdUI->SetCheck(m_IsShow?1:0);
}
前一篇:C++访问指定网页