Search code examples
c++mfciconstoolbars

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?

Thanks


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(),
        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.