Introduction
The proposed class is based on Daniel Godson's: Generating inactive/disabled images for toolbar. I have fixed some errors in the original code. But there are still some shortcomings left:
- the function to load bitmap from the file and display it is not implemented
- the size of buttons on the toolbar is 24�24, I haven't give an interface to change it.
Using the code
Just 2 places you need to edit for your own use.
First is,
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
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;
}
int *num,i,j=m_wndToolBar.GetToolBarCtrl().GetButtonCount();
num=new int[j+1];
memset(num,0,(j+1)*sizeof(int));
num[0]=j;
for(i=0;i<j;i++)
{
if(ID_SEPARATOR == m_wndToolBar.GetItemID( i ))
num[i+1]=1;
}
m_wndToolBar.SetFullColorImage(IDR_MAINFRAME_24,
RGB(255, 0, 255),num);
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1;
}
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
delete []num;
return 0;
}
Second is, change the background bitmap here, by resource ID. So you must add the bitmap to your workplace first.
void CMainFrame::OnChangebk()
{
if(whichbk)
m_wndToolBar.UpdateBk(IDR_MAINFRAME_24,
RGB(255, 0, 255) , IDB_BITMAP1);
else
m_wndToolBar.UpdateBk(IDR_MAINFRAME_24 ,
RGB(255, 0, 255) , IDB_BITMAP2);
whichbk=!whichbk;
}
Acknowledgements
- Thanks for xxxyyyzzz's translation!
- Thanks for DavidRipple's suggestion!