views:

2138

answers:

3

Hi all,

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?

Thanks

+6  A: 

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(),
    MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_BITMAP,
    0,0, LR_CREATEDIBSECTION | LR_LOADMAP3DCOLORS);
CBitmap bm;
bm.Attach(hBitmap);

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:

m_toolbar.GetToolBarCtrl().SetImageList(&m_imagelist);

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.

Stu Mackellar
I can't seem to get this code working. With a little tinkering I have managed to get it running but I get completely blank icons. Any ideas?
Konrad
Have you created a bitmap resource of a suitable size in your project and called CImageList::Create with the appropriate parameters?
Stu Mackellar
Yes, I took a previously 16colour bit map and converted it using visual studio. As per the Create, I followed your instructions exactly with the exception : CImageList m_imagelist;m_imagelist.Create(20, 20, ILC_COLOR8, 4, 4);...
Konrad
Is your bitmap 20 pixels high? Are your icons 20 pixels wide? Are you sure the bitmap is 256-colour and has the right resource ID? The technique described here definitely works, because I use it. You can't simply cut and paste code and expect it to work, it needs to be tweaked to work with your app.
Stu Mackellar
Sure, I understand that. My icons are 16,15 and I have changed that (sorry forgot to mention) and I am know that the bitmap is 256 colours (8bits.) The bit that is getting me is how to make it work with the declaration :
Konrad
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE| CBRS_TOP) || m_wndToolBar.LoadToolBar(IDR_MAINFRAME))...
Konrad
Apologies, did some tinkering and got it working. Many thanks for your input.
Konrad
Surely that last bit of code is the boilerplate that MFC adds for the standard toolbar? Sorry, I assumed that you had a toolbar in your frame window already!
Stu Mackellar
loading the bitmap is easier if you just use CBitmap.LoadBitmap() call, or am I missing something?
gbjbaanb
@gbjbaanb There is no overload of LoadBitmap that takes an HBITMAP - only ones that take a resource ID or a resource name.
Stu Mackellar
Thanks for this, it got me out of a sticky situation today.
Adam Pierce
This worked for me using ILC_COLOR32 in place of ILC_COLOR8 for a 32 bit color bitmap! Nice. ;)
Mordachai
A: 

The solution worked Flawless, you only need to fix it a little:

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

This fails for 24 bit.