Adding true transparent 32x32 buttons from bitmap to flat toolbar

My application creates toolbar and adds custom buttons to it. I have the choice to add 16x16, 24x24 or 32x32 buttons. According to MSDN:

To create a transparent toolbar, all you need to do is add TBSTYLE_FLAT or TBSTYLE_TRANSPARENT to the window style parameter of CreateWindowEx.

It's not clear If I should use some specific color for transparency, or I have to use 32 bit bitmaps where alpha=255 indicates transparency. How would the Toolbar control know about the transparency color? Specifying styles TBSTYLE_FLAT or TBSTYLE_TRANSPARENT works for 16x16, 24x24 bitmaps where RGB(0xC0, 0xC0, 0xC0) is transparency color. but in 32x32 neither RGB(0xC0, 0xC0, 0xC0) nor 32 bit bitmap with alpha=255 worked. I googled and spent one full day for this, but failed to find a clear solution. Some offered using GB(0xF0, 0xF0, 0xF0) in 24 bit bitmaps, which is the background color of buttons. This is not True transparency, I switched to high-contrast theme, and noticed that in 16x16 and 24x24 buttons, toolbar transparency (color #C0C0C0) works fine, but for 32x32 buttons It didn't work. Can anybody Help me? Thanks in advance mr.abzadeh

EDIT: I was using TB_ADDBITMAP to add bitmap to toolbar. This only showed 1/4 of 32bpp image with no transparency. I switched to TB_SETIMAGELIST, and everything was ok.

Upvotes: 0

Views: 435

Answers (1)

The problem exists when I use TB_ADDBITMAP as this:

TBADDBITMAP tb;
tb.hInst = GetModuleHandleW(nullptr);
tb.nID = IDR_TOOLBAR32;
unsigned uCount = 10;
SendMessageW(hwndTB, TB_ADDBITMAP, uCount, (LPARAM)&tb);

by switching to ImageList, Everything works fine. My new code is this:

const int cpWidth = 32;
const int iCount = 10;
const int idResource = IDR_TOOLBAR32;
HINSTANCE hInstance = GetModuleHandleW(nullptr);
HIMAGELIST hImageList = ImageList_Create(cpWidth, cpWidth,
    ILC_COLOR32 | ILC_MASK,
    iCount, 0
);
if (!hImageList) return -1;
HBITMAP hBitmapImage = (HBITMAP)LoadImageW(hInstance,
    MAKEINTRESOURCEW(idResource),
    IMAGE_BITMAP, iCount * cpWidth, cpWidth,
    LR_COPYFROMRESOURCE | LR_SHARED
);
if (!hBitmapImage) return -1;
ImageList_Add(hImageList, hBitmapImage, NULL);
SendMessageW(hwndTB, TB_SETIMAGELIST, (WPARAM)0, (LPARAM)hImageList);

Upvotes: 2

Related Questions