winapibitmaptransparencymenuitem

win32 menu item bitmap with transparent background


What I try to accomplish is really simple: adding an image to a menu item. I'm programming in C using the win32 API. The image / bitmap shows up but the background is white. What I want is to turn the white background into transparant.

I have been reading every information I could find including stackoverflow but the information seems inconsistent. Some say a bitmap cannot have any form of transparancy, whereas others say it can. See for example this question:

Simple way to show a bitmap next to a menu item in a CMenu with correct transparency

Both SetMenuItemBitmaps() and SetMenuItemInfo() give a white background. Above link says if the bitmap is in 32bpp with premultiplied alpha it should be shown correctly. So either it simply is not possible this way, or the bmp I use is in the wrong format. Can anyone give the definite answer to this problem. If it turns out it can't be done using SetMenuItemInfo(), what is then the easiest way of solving this problem? I try to avoid owner draw solutions as I feel it is a bit overkill. Besides, as far as I understand owner drawn solutions are hard to respect Windows theming.

menubitmap.rc:

#include "menubitmap.h"

ID_ICON             ICON    DISCARDABLE "menu1.ico"
ID_BITMAP_EXIT      BITMAP  DISCARDABLE "Exit-icn.bmp"

ID_MENU MENU DISCARDABLE 
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "E&xit",                       ID_FILE_EXIT
    END
END

menubitmap.c:

#include <windows.h>
#include "menubitmap.h"

const char g_szClassName[] = "myWindowClass";

LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
        case WM_CREATE:
        {
            HBITMAP btmp;
            MENUITEMINFOA miinfo;
            HMENU menu;

            btmp=LoadBitmap((HINSTANCE) GetModuleHandle (NULL), MAKEINTRESOURCE(ID_BITMAP_EXIT));
            menu=GetMenu(hwnd);
            miinfo.cbSize=sizeof(MENUITEMINFO);
            if(!GetMenuItemInfo(menu,ID_FILE_EXIT,FALSE,&miinfo)){
                    printf("getmenuiteminfo failed\r\n");
            }else{
                    miinfo.fMask |= MIIM_BITMAP;
                    miinfo.hbmpItem=btmp;
                    if(SetMenuItemInfo(menu,ID_FILE_EXIT,FALSE,&miinfo)){
                            printf("setmenuiteminfo");
                    }
            }

        }
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case ID_FILE_EXIT:
                    PostMessage(hwnd, WM_CLOSE, 0, 0);
                break;
            }
        break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, Message, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_ICON));
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = MAKEINTRESOURCE(ID_MENU);
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_ICON), IMAGE_ICON, 16, 16, 0);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "A Menu",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

Here you can find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aAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AA==

Solution

  • Alpha transparency in Windows' GDI is a minefield. It was added very late, and only a handful of API calls are truly capable of handling a dedicated alpha channel. LoadBitmap is not one of those that support (or at least do not break) alpha transparency. The alpha channel gets lost when you're loading the image from the application's resources.

    To fix that, you'll have to use LoadImage, passing in the correct flags. LR_CREATEDIBSECTION is the important one, as that preserves the alpha channel in the source bitmap.

    The fix is as simple as replacing

    LoadBitmap((HINSTANCE) GetModuleHandle (NULL), MAKEINTRESOURCE(ID_BITMAP_EXIT))
    

    with

    (HBITMAP)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_BITMAP_EXIT),
                       IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION)
    

    With that in place you'll see the menu icon displayed with per-pixel alpha transparency:

    Screenshot