預設的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的內容,然後重新安排工具條的大小。