前面介绍 了在工具栏上创建ComboBox组合框控件,及程序主框架来响应消息.
因为在实际中业务需要时,工具栏上的某些按纽需要显示或灰化,比如操作时间控件时,很多时候在它前面或
旁边加个Button 吧.我们就拿这个问题来解释?
程序框架还是依照前面的代码,在工具栏上增加2个按纽. 然后在框架中利用向导加ON_UPDATE_COMMAND_UI(...)函数,利用pCmdUI->Enable()函数即可.
在此程序的ComboBox中,用下拉项来控制工具条按纽,只需在框架.h中声明一个变量 int m_nMode;
即可.
下面是实现代码:
// CMainFrame message handlers
void CMainFrame::OnSelchangeCombo()
{
int nSel;
nSel = m_wndToolBar.m_comboBox.GetCurSel();
CString ss;
switch(nSel)
{
case 0:
m_nMode = 1;
ss.Format("m_nMode=%d",m_nMode);
AfxMessageBox(ss);
break;
case 1:
m_nMode = 2;
ss.Format("m_nMode=%d",m_nMode);
AfxMessageBox(ss);
break;
case 2:
m_nMode = 3;
ss.Format("m_nMode=%d",m_nMode);
AfxMessageBox(ss);
break;
case 3:
m_nMode = 4;
ss.Format("m_nMode=%d",m_nMode);
AfxMessageBox(ss);
break;
default:
break;
}
}
void CMainFrame::OnUpdateButton1(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if ((m_nMode == 1) ||(m_nMode == 3) )
{
pCmdUI->Enable(FALSE);
}
else
{
pCmdUI->Enable(TRUE);
}
}
void CMainFrame::OnUpdateButton2(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if ((m_nMode == 2) ||(m_nMode == 4) )
{
pCmdUI->Enable(FALSE);
}
else
{
pCmdUI->Enable(TRUE);
}
}