MFC 工具栏图标的颜色深度更高?

2022-01-12 00:00:00 icons c++ mfc toolbars

我想知道如何在 MFC 中制作一个使用 24 位或 256 色位图而不是可怕的 16 色位图的工具栏.

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?

谢谢

推荐答案

发生这种情况的原因是 MFC CToolbar 类在内部使用了一个图像列表,该列表被初始化为仅使用 16 种颜色.解决方案是创建我们自己的图像列表并告诉工具栏使用它.我知道这适用于 256 色,但我还没有用更高的位深度测试它:

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:

首先,从资源中加载 256 色位图:

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

接下来,创建一个 256 色图像列表并将我们的位图添加到其中:

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

VS2008 中的新 MFC 版本也可能解决了这个问题,因为我知道许多 UI 元素已经更新.我还没有真正尝试过使用它,所以我不能确定.

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.

相关文章