Iceman
Iceman

Reputation: 397

Loading icons with transparency to tray menu of application

I need the icons of application menu options had no background. I'm using this code to set icons to menu options:

HICON PowerIcon = (HICON)LoadImageA(hInstance, MAKEINTRESOURCEA(POWERBTN),IMAGE_ICON,24,24,LR_LOADTRANSPARENT);
HICON ResetIcon = (HICON)LoadImageA(hInstance, MAKEINTRESOURCEA(RESETBTN),IMAGE_ICON,24,24,LR_LOADTRANSPARENT);
//    HICON PowerIcon = LoadIconA(hInstance, MAKEINTRESOURCEA(POWERBTN));
//    HICON ResetIcon = LoadIconA(hInstance, MAKEINTRESOURCEA(RESETBTN));
ICONINFO PowerII, ResetII;
GetIconInfo(PowerIcon, &PowerII);
GetIconInfo(ResetIcon, &ResetII);
HBITMAP PB = PowerII.hbmColor;
HBITMAP RB = ResetII.hbmColor;
SetMenuItemBitmaps(PopupMenu, TRAY_EXIT, MF_BYCOMMAND, PB, PB);
SetMenuItemBitmaps(PopupMenu, TRAY_RESTART, MF_BYCOMMAND, RB, RB);

And the result is... with black background:

buttons

I don't need this background.

How to draw this icons in menu as is, without background?

Upvotes: 1

Views: 1029

Answers (1)

RbMm
RbMm

Reputation: 33706

menu transparent display DIB section bitmap rather than a compatible bitmap. best and most easy choice - use 32bpp bitmaps created via LoadImage( , IMAGE_BITMAP, .. LR_CREATEDIBSECTION). try for test bitmap (in menu) created with and without LR_CREATEDIBSECTION flag and view different.

if you have icon in resource instead of bitmap, in my test GetIconInfo return not DIB section bitmap and if not transparently displayed. possibly way - yourself create DIB section bitmap from icon resource, instead use LoadIcon + GetIconInfo.

HBITMAP CreateDIBfromIcon(PCWSTR lpName, int cxDesired, int cyDesired)
{
    union {
        PVOID pv;
        PBYTE pb;
        PBITMAPINFOHEADER pbih;
    };

    if (HRSRC hrs = FindResource((HMODULE)&__ImageBase, lpName, RT_GROUP_ICON))
    {
        if (pv = LoadResource((HMODULE)&__ImageBase, hrs))
        {
            if (int i = LookupIconIdFromDirectoryEx(pb, TRUE, cxDesired, cyDesired, 0))
            {
                if (hrs = FindResource((HMODULE)&__ImageBase, MAKEINTRESOURCE(i), RT_ICON))
                {
                    if (pv = LoadResource((HMODULE)&__ImageBase, hrs))
                    {
                        if (pbih->biBitCount == 32)
                        {
                            BITMAPINFOHEADER bih = *pbih;
                            bih.biHeight >>= 1;
                            PVOID pvBits;

                            if (HBITMAP hbmpItem = CreateDIBSection(0, (PBITMAPINFO)&bih, DIB_RGB_COLORS, &pvBits, 0, 0))
                            {
                                memcpy(pvBits, pb + pbih->biSize, 4*pbih->biWidth*pbih->biHeight);
                                return hbmpItem;
                            }
                        }
                    }
                }
            }
        }
    }

    return 0;
}

in your case need use CreateDIBfromIcon(MAKEINTRESOURCE(POWERBTN), 24, 24) and CreateDIBfromIcon(MAKEINTRESOURCE(RESETBTN), 24, 24)

Upvotes: 1

Related Questions