C++ – Higher color depth for MFC toolbar icons


I was wondering how to make a toolbar in MFC that used 24bit or 256 colour bitmaps rather than the horrible 16 colour ones.

Can anyone point me in the direction of some simple code?


Best Solution

The reason this happens is that the MFC CToolbar class uses an image list internally that is initialised to use 16 colours only. The solution is to create our own image list and tell the toolbar to use that instead. I know this will work for 256-colours, but I haven't tested it with higher bit-depths:

First, load a 256-colour bitmap from a resource:

HBITMAP hBitmap = (HBITMAP) ::LoadImage(AfxGetInstanceHandle(),
CBitmap bm;

Next, create a 256-colour image list and add our bitmap to it:

CImageList m_imagelist.Create(20, 20, ILC_COLOR8, 4, 4);
m_imagelist.Add(&bm, (CBitmap*) NULL);

Finally, we need to tell the toolbar to use the new image list:


It's also possible that the new MFC version in VS2008 may have solved this problem as I know that many of the UI elements have been updated. I haven't actually tried using it yet so I can't be certain.