此处仅仅作为记录,是学习和工作的笔记。
以下是创建自定义的工具栏的方法:
第一步:在资源中添加一个工具栏IDR_TOOLBAR1,并在框架类的头文件中定义CToolBar newToolBar;
第二步:在框架类的OnCreate函数中添加如下代码。
/* 创建一个自定义工具栏 */
if (!newToolBar.CreateEx(this, TBSTYLE_FLAT,
WS_CHILD|WS_VISIBLE|CBRS_TOP|CBRS_GRIPPER|CBRS_TOOLTIPS|CBRS_SIZE_DYNAMIC)
|| !newToolBar.LoadToolBar(IDR_TOOLBAR1))
//加载绑定一个工具栏资源
{
MessageBox(_T("Creat ToolBar Failed!"));
return -1;
}
以下是在单文档的自定义工具栏上创建的控件:
//在自定义的工具栏上添加控件
m_staticWhiteLace.Create(_T("白边:"), WS_CHILD|WS_VISIBLE|SS_CENTER,
CRect(1,10,65,32), &newToolBar, ID_STATIC_WHITE_LACE);
m_staticWhiteLace.SetFont(&m_font, TRUE);
m_editWhiteLace.Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER,
CRect(50, 10, 108, 32), &newToolBar, ID_EDIT_WHITE_LACE);
m_editWhiteLace.SetFont(&m_font, TRUE);
m_editWhiteLace.SetWindowText(_T("200.00"));
m_staticMoveValue.Create(_T("移动值:"), WS_CHILD|WS_VISIBLE|SS_CENTER,
CRect(110,10,170,32), &newToolBar, ID_STATIC_MOVE_VALUE);
m_staticMoveValue.SetFont(&m_font, TRUE);
m_editMoveValue.Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER,
CRect(160, 10, 200, 32), &newToolBar, ID_EDIT_MOVE_VALUE);
m_editMoveValue.SetFont(&m_font, TRUE);
m_editMoveValue.SetWindowText(_T("0"));
m_btnIncrease.Create(_T("+"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
CRect(210,10,230,32), &newToolBar, ID_BUTTON_INCREASE);
m_editInAndDe.Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER,
CRect(240, 10, 280, 32), &newToolBar, ID_EDIT_INCREASE_AND_DECREASE);
m_editInAndDe.SetFont(&m_font, TRUE);
m_editInAndDe.SetWindowText(_T("0.00"));
m_btnDecrease.Create(_T("-"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
CRect(290,10,310,32), &newToolBar, ID_BUTTON_DECREASE);
m_comboPrinSpeed.Create(
WS_CHILD|WS_VISIBLE|WS_VSCROLL|CBS_DROPDOWN,
CRect(320,10,400,80), &newToolBar, ID_COMBOBOX_PRINT_SPEED);
m_comboPrinSpeed.SetFont(&m_font, TRUE);
m_comboPrinSpeed.InsertString(0, _T("低速打印"));
m_comboPrinSpeed.InsertString(1, _T("中速打印"));
m_comboPrinSpeed.InsertString(2, _T("高速打印"));
m_comboPrinSpeed.SetCurSel(1);
m_comboPrinDirt.Create(
WS_CHILD|WS_VISIBLE|WS_VSCROLL|CBS_DROPDOWN,
CRect(410,10,490,80), &newToolBar, ID_COMBOBOX_PRINT_DIRT);
m_comboPrinDirt.SetFont(&m_font, TRUE);
m_comboPrinDirt.InsertString(0, _T("单向打印"));
m_comboPrinDirt.InsertString(1, _T("双向打印"));
m_comboPrinDirt.SetCurSel(0);
m_btnColorCheck.Create(_T("彩条"), WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX,
CRect(500,10,564,32), &newToolBar, 3);
m_btnColorCheck.SetFont(&m_font, TRUE);
// TODO: 在此添加命令处理程序代码
int nCurSel = m_comboPrinSpeed.GetCurSel();
CString strWhiteLace;
int len = m_editWhiteLace.LineLength();
m_editWhiteLace.GetLine(0, strWhiteLace.GetBuffer(len), len);
strWhiteLace.ReleaseBuffer(len);
//如果想把编辑框的文本转化为int型,只需调用函数atoi,如下所示:
int num = _ttoi(strWhiteLace);
//获取下拉框打印速度的数据
CString strPrintSpeed;
m_comboPrinSpeed.GetLBText(m_comboPrinSpeed.GetCurSel(),strPrintSpeed);
//获取下拉框打印方向的数据
CString strPrintDirt;
m_comboPrinDirt.GetLBText(m_comboPrinDirt.GetCurSel(),strPrintDirt);
//获取复选框的选中结果
int nCheck = 0;//0-表示未选中,1-表示选中
nCheck = m_btnColorCheck.GetCheck();
CString strCheck;
if (0 == nCheck)
{
strCheck = _T("未选中彩条");
}
else if (1 == nCheck)
{
strCheck = _T("已选中彩条");
}
MessageBox(strPrintSpeed + _T(" ") + strPrintDirt + _T(" ") + strWhiteLace + _T(" ") + strCheck);
以下是运行效果: