62 How do I Create >16 Colour Toolbars?
I did this as an exercise in self-flagellation and to prove to myself that it
could be done. I tried it with 24 bit colour instead of 256 colours, because 256
colour bitmaps are palletised and a bit of a pain to work with as a result.
In the class header, lets define some members:
CToolBar * m_pDataBar;
CImageList m_DataHotImages;
CImageList m_CallHotImages;
I made two 24-bit colour bitmaps 32 pixels deep by as many as were needed to
get the number of buttons I needed (5 are used here), then loaded them into imagelists.
I made the bitmaps with fuschia as the background colour, so the image list
would automatically be masked without me having to specify separate mask
bitmaps. That saves some time. The buttons are quite large at 32 pixels, but
that allowed me to rip off icons for the images, which provides an almost
limitless supply of artwork :-).
CBitmap cbmDataHot;
CBitmap cbmDataCold;
cbmDataHot.LoadBitmap (IDB_DATA_HOT_LARGE_24BIT) ;
cbmDataCold.LoadBitmap (IDB_DATA_COLD_LARGE_24BIT) ;
HIMAGELIST hHotImages = ImageList_Create (32, 32, ILC_COLOR24 | ILC_MASK, 0,
5);
HIMAGELIST hColdImages = ImageList_Create (32, 32, ILC_COLOR24 | ILC_MASK, 0,
5);
// Note that fuschia (255,0,255) is the bitmap background colour
if (hHotImages)
{
m_DataHotImages.Attach (hHotImages);
m_DataHotImages.Add (&cbmDataHot, RGB(255, 0, 255));
}
if (hColdImages)
{
m_DataColdImages.Attach (hColdImages);
m_DataColdImages.Add (&cbmDataCold, RGB(255, 0, 255));
}
I made the toolbar and assigned the commands like this:
const UINT auCmdIds [] = {ID_COMMAND1,
ID_COMMAND2,
ID_COMMAND3,
ID_COMMAND4,
ID_COMMAND5};
m_pDataBar = new CToolBar;
if (!m_pDataBar->Create (this,
WS_CHILD | WS_VISIBLE |
CBRS_TOP | CBRS_TOOLTIPS,
ID_CALL_TOOLBAR))
{
return FALSE; // fail to create
}
m_pDataBar->ModifyStyle (0, TBSTYLE_FLAT);
m_pDataBar->SetButtons (auCmdIds,sizeof(auCmdIds)/sizeof(auCmdIds[0]));
m_pDataBar->GetToolBarCtrl().SetHotImageList (&m_DataHotImages);
m_pDataBar->GetToolBarCtrl().SetImageList (&m_DataColdImages);
and it seems to work fine. Windows even does a passable job of coping when
you change the colour depth on the fly.
The biggest problem in all this is that VC will import a hi-colour bitmap quite happily, but the resource editor refuses to allow you to edit it. That's _got_ to get fixed sometime soon, because it's basically crap. It bugs me no end having to swap backwards and forwards between VC and
Paintshop Pro, but that's how I did it.
Update
Microsoft have added an article on 256-colour toolbars to their website, at
http://msdn.microsoft.com/msdnmag/issues/01/07/c/c0107.asp