接触到了CMFCToolBar就把他通透一下。
首先工具栏由ToolBar创建,
创建一个新的ToolBar,修改其ID。设有两个ID:IDR_LIGHT、IDR_CAMERA
将对应的bmp资源加入资源视图的bitmap中,其ID为IDB_BITMAP_TOOLBAR
在MainFrm.h文件中加入CMFCToolBar m_toolbar1;
在MainFrm.cpp的CMainFrame::OnCreate中创建:
if (!m_toolbar1.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC/*,CRect(0, 0, 1, 1), IDR_TOOLBAR1*/) ||
(!m_toolbar1.LoadToolBar(IDR_TOOLBAR1, 0, 0, TRUE, 0, 0, IDB_BITMAP_TOOLBAR)))
{
TRACE0("未能创建工具栏\n");
return -1; // 未能创建
}
m_toolbar1.SetWindowText(_T("MyToolbar"));
m_toolbar1.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockPane(&m_toolbar1);
CMFCToolBarComboBoxButton类是用于在工具栏中添加ComboBox控件的类。
你可以在
MSDN
中找到其定义:
首先,在MainFrm.h文件中创建变量:
public:
CMFCToolBarComboBoxButton *m_comboButton;
接下来在MainFrm.cpp的CMainFrame::OnCreate中添加:
//Combox
m_comboButton = new CMFCToolBarComboBoxButton(IDR_LIGHT,
GetCmdMgr()->GetCmdImage(IDR_LIGHT, FALSE),// 工具条位图中的图像序号
CBS_DROPDOWNLIST, //组合框类型
100);
m_comboButton->EnableWindow(TRUE);
m_comboButton->SetCenterVert();
m_comboButton->SetDropDownHeight(200);//设置下拉列表的高度
m_comboButton->SetFlatMode();
//添加内容
m_comboButton->AddSortedItem(_T("If Only"), 1);//1无效,写在前面的就为第一个
m_comboButton->AddSortedItem(_T("Once"),0);
m_comboButton->AddItem(_T("P.S.I Love You"));
m_comboButton->AddItem(_T("Love Me If You Dare"));
m_comboButton->SelectItem(0);//默认选中
//添加到工具栏
//m_toolbar1.InsertSeparator();//先添加分隔条
//m_wndToolBar.InsertButton(m_comboButton);
m_toolbar1.ReplaceButton(IDR_LIGHT, *m_comboButton);
这时控件还是灰色的。
接下来添加其消息响应:
ON_COMMAND(IDR_LIGHT, &CMainFrame::OnClickComboBox)
ON_CBN_SELCHANGE(IDR_LIGHT, &CMainFrame::OnSelChangeClick)
void CMainFrame::OnClickComboBox()
{}//这个处理函数,是为响应鼠标操作,处理函数什么也没有做,只是一个空函数,
//但是这个消息函数如果不加,组合框为灰色的
void CMainFrame::OnSelChangeClick()
{
CMFCToolBarComboBoxButton* pSrcCombo = CMFCToolBarComboBoxButton::GetByCmd(IDR_LIGHT, TRUE);
int index = m_comboButton->GetCurSel();//这种不可用
index = pSrcCombo->GetCurSel();
CString str;
str = pSrcCombo->GetItem(index);
MessageBox(_T("OnClickComboBox: ") + str);
}
//
CMFCToolBarEditBoxButton类是用于在工具栏中添加EditControl控件的类。
具体使用类似:
首先创建变量,
public:
CMFCToolBarEditBoxButton *m_editboButton;
然后在Oncreate中加:
//edit control
m_editboButton = new CMFCToolBarEditBoxButton(IDR_CAMERA, NULL, 128, 60);
m_editboButton->EnableWindow(TRUE);
m_editboButton->HaveHotBorder();//设置下拉列表的高度
m_editboButton->SetFlatMode();
m_editboButton->OnSize(65);//设置下拉列表宽度
//添加默认内容
m_editboButton->SetContents(_T("0.4"));
m_toolbar1.ReplaceButton(IDR_CAMERA, *m_editboButton);
这时控件还是灰色的。
接下来添加其消息响应:
ON_COMMAND(IDR_CAMERA, &CMainFrame::OnClickEdit)
ON_EN_CHANGE(IDR_CAMERA, &CMainFrame::OnEditChangeClick)
void CMainFrame::OnSelChangeClick()
{
CMFCToolBarEditBoxButton* pSrcCombo = CMFCToolBarEditBoxButton::GetByCmd(IDR_CAMERA);
CString t = m_editboButton->GetContentsAll(IDR_CAMERA); //都行了
t = pSrcCombo->GetContentsAll(IDR_CAMERA);
MessageBox(_T("OnClickedit: ") + t);
}
void CMainFrame::OnClickEdit()
{}//这个处理函数,是为响应鼠标操作,处理函数什么也没有做,只是一个空函数,
//但是这个消息函数如果不加,组合框为灰色的
最后说一点。
另外像CMFCToolBarMenuButton这样的类顾名思义是用于在工具栏中添加Menu控件。
学无止境。最后给出我直接的例子和效果图。
https://download.csdn.net/download/cao_jie_xin/12198577