默认的VC单文档或者多文档的工具条是一个个的图标,下面这段代码可以让你得到一个上边是图标下边是文字的工具条。
代码:
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar/n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar/n");
return -1; // fail to create
}
/*这里插入代码*/
// TODO: Delete these three lines if you don't want the toolbar to be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
/*插入以下代码*/
m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
CBRS_GRIPPER | CBRS_BORDER_3D | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
// Add text to each button
for(int i = 0; i < m_wndToolBar.GetCount(); i++)
{
UINT id = m_wndToolBar.GetItemID(i);
CString s;
if(!s.LoadString(id))
continue;
int j = s.Find(_T('/n'));
if(j < 0)
continue;
s = s.Right(s.GetLength() - j - 1);
m_wndToolBar.SetButtonText(i, s);
}
CRect rect;
m_wndToolBar.GetItemRect(0, &rect);
m_wndToolBar.SetSizes(rect.Size(),CSize(16, 15));
思路:设置工具条的Style,并获取每个ID所对应的字符串,从中提取ToolTip的内容作为ButtonText的内容,然后重新安排工具条的大小。