新建dialog对话框程序。在资源视图中添加一个toolbar(代码中为IDR_TOOLBAR_MAIN)),在资源视图中添加一个bmp(代码中为IDB_BITMAP_Toobar_background)图片作为背景.
在.h文件中添加:
private:
CToolBar m_toolBar;
CReBar m_reBar;
在.cpp文件的OnInitDialog中添加以下代码:
if (m_toolBar.CreateEx(this,TBSTYLE_FLAT | TBSTYLE_TRANSPARENT))
{
//m_toolBar.LoadBitmap(m_nIDBitmap);
//m_toolBar.SetButtons(m_lpaIDToolBar, m_cIDToolBar);
m_toolBar.LoadToolBar(IDR_TOOLBAR_MAIN);
}
m_toolBar.ModifyStyle(0,TBSTYLE_TRANSPARENT);//设置工具栏背景色透明
m_toolBar.ShowWindow(SW_SHOW);
m_reBar.Create(this);
m_reBar.AddBar(&m_toolBar, RGB(255,0,0), RGB(0,255,0),_T(""), RBBS_GRIPPERALWAYS| RBBS_CHILDEDGE);
m_reBar.RedrawWindow();
REBARBANDINFO info;
CRect rectToolBar;
m_toolBar.GetItemRect(0, &rectToolBar);
ZeroMemory(&info, sizeof(info));
info.cbSize=sizeof(info);
info.fMask=RBBIM_CHILDSIZE | RBBIM_IDEALSIZE | RBBIM_SIZE|RBBIM_BACKGROUND;
info.fStyle = RBBS_GRIPPERALWAYS;
info.cxMinChild = rectToolBar.Width();
info.cyMinChild = rectToolBar.Height();
info.cx = info.cxIdeal = rectToolBar.Width() * 10;
info.hwndChild=(HWND)m_toolBar;
info.hbmBack = LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP_Toobar_background));//加载位图
m_reBar.GetReBarCtrl().SetBandInfo(0,&info);
CRect rcClientStart;
CRect rcClientNow;
GetClientRect(rcClientStart);
//将重绘工具栏和状态栏后剩余的客户区空间放到rcClientNow中
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST,
0, reposQuery, rcClientNow);
// Now move all the controls so they are in the same relative
// position within the remaining client area as they would be
// with no control bars.
CPoint ptOffset(rcClientNow.left - rcClientStart.left,
rcClientNow.top - rcClientStart.top);
CRect rcChild;
CWnd* pwndChild = GetWindow(GW_CHILD);
while (pwndChild)
{
pwndChild->GetWindowRect(rcChild);
ScreenToClient(rcChild);
rcChild.OffsetRect(ptOffset);
pwndChild->MoveWindow(rcChild, FALSE);
pwndChild = pwndChild->GetNextWindow();
}
// Adjust the dialog window dimensions
CRect rcWindow;
GetWindowRect(rcWindow);
rcWindow.right += rcClientStart.Width() - rcClientNow.Width();
rcWindow.bottom += rcClientStart.Height() - rcClientNow.Height();
MoveWindow(rcWindow, FALSE);
// And position the control bars
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
运行程序即可.