如何在CMFCToolBar工具栏中加入组合框等控件,且先看在线MSDN上怎么说的:
To add a combo box button to a toolbar, follow these steps:
1. Reserve a dummy resource ID for the button in the parent toolbar resource.
2. Construct a
3. In the message handler that processes the
具体过程如下:
1.在工具栏资源编辑器中加入id为IDR_COM的空白工具栏,
2.在头文件中定义组合框
CMFCToolBarComboBoxButto
3.BEGIN_MESSAGE_MAP(CMainFrame, CFrameWndEx)中添加消息响应
在头文件中声明消息函数:
在CMainFram中定义处理函数:
LRESULT CMainFrame::OnToolbarReset( WPARAM wp, LPARAM lp )
{
UINT iIndex = (UINT)wp;
switch(iIndex)
{
case IDR_TOOLBAR1:
{
int iIndex = m_wndToolBarCombox.CommandToIndex(ID_COMBOX1);
m_wndToolBarCombox.SetButtonInfo(iIndex, ID_COMBOX1, TBBS_SEPARATOR, 160 );
//得到组合框的位置
CRect rect;
m_wndToolBarCombox.GetItemRect(iIndex, &rect);
//设置组合框的下拉高度
rect.bottom += 100;
//创建组合框,四个参数依次为窗口风格、组合框位置、父窗口、ID号
//m_combox1.Create(CBS_DROPDOWN|WS_VISIBLE|WS_TABSTOP|CBS_AUTOHSCROLL,rect, &m_wndToolBarCombox, ID_COMBOX1);
CMFCToolBarComboBoxButto
GetCmdMgr ()->GetCmdImage
WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | CBS_DROPDOWN,
160);
m_combox1->EnableWindow(true);
m_combox1->SetCenterVert();
m_combox1->SetDropDownHeight(200);
m_combox1->SetFlatMode();
m_combox1->AddItem(_T("OPTION1"));
m_combox1->AddItem(_T("OPTION2"));
m_combox1->SelectItem(0);
m_wndToolBarCombox.ReplaceButton(ID_COMBOX1, *m_combox1);
delete m_combox1;
}
break;
}
return 0L;
}
2、添加组合框的事件消息响应函数
消息映射:
ON_COMMAND(IDR_COM, &CMainFrame::OnClickComboBox)
消息声明:
afx_msg void OnSelChangeClick();
消息处理函数:
void CMainFrame::OnSelChangeClick()
{
}
void CMainFrame::OnClickComboBox()
{
}
(注意:一定要GetByCmd;
OnClickComboBox没有做任何处理,但是如果去掉的话,组合框将编程灰色,无法使用)。
注:更新工具栏:
1.工具栏的右侧向下的小箭头->添加或删除按钮->标准->重置工具栏
2.运行regedit打开注册表->HKEY_CURRENT_USER->Software->工程名,找到工程名将其从注册表中删之。