但如果工具栏位于对话框内,则无法响应ON_UPDATE_COMMAND_UI,这是由于CDialog没有对OnIdle的处理,从而无法实现对 DoUpdate的调用。网上有文章介绍一种方法,是在Dialog类内实现WM_INITMENUPOPUP的消息映射,但这种办法只能解决对话框内菜 单项的响应。这里介绍另外一种方法。
步骤如下:
1,创建CCmdUI的子类CCustomizeToolCmdUI;本来用CToolCmdUI即可,但此类MFC并没有暴露出来,同时定义在bartool.cpp内;代码实现则可以直接拷贝CToolCmdUI。
2,创建一个CToolBar的子类CCustomizeToolBar;
3, CCustomizeToolBar内重载CToolBar的OnUpdateCmdUI(CCmdTarget* pTarget, BOOL bDisableIfNoHndler); CToolBar中此函数的第一参数类型是CMainFrame*用于表示工具栏的父窗口并将被用于消息的分 发,重载后直接用CCmdTarget,当然CDialog也可以(因为对话框类也派生于CCmdTarget)。
4,CCustomizeToolBar内重载CToolBar的void _GetButton(int nIndex, TBBUTTON* pButton) const;
5,在对话框类内重载ContinueModal,此函数在DoModal内被调用,用于判断是否需要修改状态。在此函数内,实现对OnUpdateCmdUI的调用,如下:m_dlgToolbar.OnUpdateCmdUI(this, 1);
6,CCustomizeToolBar内两个重载的函数可以拷贝CToolBar的实现代码,并作小量修改,代码如下:
{
CCustomizeToolCmdUI state;
state.m_pOther = this;
state.m_nIndexMax = (UINT)DefWindowProc(TB_BUTTONCOUNT, 0, 0);
for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax; state.m_nIndex++)
{
// get buttons state
TBBUTTON button;
_GetButton(state.m_nIndex, &button);
state.m_nID = button.idCommand;
// ignore separators
if (!(button.fsStyle & TBSTYLE_SEP))
{
// allow reflections
if (CWnd::OnCmdMsg(0,
MAKELONG(CN_UPDATE_COMMAND_UI&0xff, WM_COMMAND+WM_REFLECT_BASE),
&state, NULL))
continue;
// allow the toolbar itself to have update handlers
if (CWnd::OnCmdMsg(state.m_nID, CN_UPDATE_COMMAND_UI, &state, NULL))
continue;
// allow the owner to process the update
state.DoUpdate(pTarget, bDisableIfNoHndler);
}
}
// update the dialog controls added to the toolbar
UpdateDialogControls(pTarget, bDisableIfNoHndler);
}
void CCustomizeToolBar::_GetButton( int nIndex, TBBUTTON * pButton) const
{
CToolBar* pBar = (CToolBar*)this;
//VERIFY(pBar->DefWindowProc(TB_GETBUTTON, nIndex, (LPARAM)pButton));
pBar->SendMessage(TB_GETBUTTON, nIndex, (LPARAM)pButton);
// TBSTATE_ENABLED == TBBS_DISABLED so invert it
pButton->fsState ^= TBSTATE_ENABLED;
}
不过以上的解决办法仍然有缺陷,即ContinueModal是用于模式对话框的,我不太清楚非模式是否会有调用;如果答案是否,则只能为对话框映射别的消息了。
NOTE:这篇ms是对的,但是我试了之后,发现还是不行。看来要看源码,弄清楚ON_UPDATE_COMMAND_UI的来龙去脉,自己写了。