Higher color depth for MFC toolbar icons?

时间秒杀一切 提交于 2019-11-27 15:02:07

问题


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


回答1:


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.




回答2:


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); 


来源:https://stackoverflow.com/questions/261559/higher-color-depth-for-mfc-toolbar-icons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!