Mfc 如何显示具有透明背景的菜单位图

Mfc 如何显示具有透明背景的菜单位图,mfc,cmenu,Mfc,Cmenu,我正在使用以下代码: m_bmpSwap.LoadBitmap(IDB_BITMAP2); pMnuPopup->SetMenuItemBitmaps(0, MF_BYPOSITION, &m_bmpSwap, &m_bmpSwap); 它看起来像: 这只是一个测试图像: 我如何让我的图像看起来像是有一个透明的背景 它是24位图像 我看到了,但我想不出来 我调整为以192/192/192为背景的8位图像,加载方式如下: HBITMAP hBmp; hBmp = (HBI

我正在使用以下代码:

m_bmpSwap.LoadBitmap(IDB_BITMAP2);
pMnuPopup->SetMenuItemBitmaps(0, MF_BYPOSITION, &m_bmpSwap, &m_bmpSwap);
它看起来像:

这只是一个测试图像:

我如何让我的图像看起来像是有一个透明的背景

它是24位图像

我看到了,但我想不出来

我调整为以192/192/192为背景的8位图像,加载方式如下:

HBITMAP hBmp;

hBmp = (HBITMAP)::LoadImage(AfxGetResourceHandle(),
    MAKEINTRESOURCE(IDB_BITMAP2),
    IMAGE_BITMAP,
    0, 0, // cx,cy
    LR_CREATEDIBSECTION | LR_LOADMAP3DCOLORS);
m_bmpSwap.Attach(hBmp);

pMnuPopup->SetMenuItemBitmaps(0, MF_BYPOSITION, &m_bmpSwap, &m_bmpSwap);
HBITMAP hBmp = (HBITMAP)::LoadImage(AfxGetResourceHandle(),
    MAKEINTRESOURCE(IDB_BITMAP1),
    IMAGE_BITMAP,
    0, 0, // cx,cy
    LR_LOADTRANSPARENT | LR_LOADMAP3DCOLORS);   
HBITMAP hBmp2 = ReplaceColor(hBmp, RGB(71, 71, 71), GetSysColor(COLOR_MENU), NULL);
DeleteObject(hBmp);

m_bmpSwap.Attach(hBmp2);
pMnuPopup->SetMenuItemBitmaps(0, MF_BYPOSITION, &m_bmpSwap, &m_bmpSwap);
如果我不使用百叶窗,这似乎更好:

但当我重新打开百叶窗并再次展示时:

我自己也是色盲,但我能看出背景实际上与对话框背景相匹配,而不是菜单颜色背景

这是我能做的最好的了吗

如何将24位或32位图像作为菜单位图?

添加LR_LOADTRANSPARENT标志以及LR_LoadMap3DColor

这将适用于未使用Windows blind测试的8位或4位图像

也可以手动更改背景色

void swap_color(HBITMAP hbmp)
{
    if(!hbmp)
        return;
    HDC hdc = ::GetDC(HWND_DESKTOP);
    BITMAP bm;
    GetObject(hbmp, sizeof(bm), &bm);
    BITMAPINFO bi = { 0 };
    bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bi.bmiHeader.biWidth = bm.bmWidth;
    bi.bmiHeader.biHeight = bm.bmHeight;
    bi.bmiHeader.biPlanes = 1;
    bi.bmiHeader.biBitCount = 32;

    std::vector<uint32_t> pixels(bm.bmWidth * bm.bmHeight);
    GetDIBits(hdc, hbmp, 0, bm.bmHeight, &pixels[0], &bi, DIB_RGB_COLORS);

    //assume that the color at (0,0) is the background color
    uint32_t color_old = pixels[0];

    //this is the new background color
    uint32_t bk = GetSysColor(COLOR_MENU);

    //swap RGB with BGR
    uint32_t color_new = RGB(GetBValue(bk), GetGValue(bk), GetRValue(bk));

    for (auto &pixel : pixels)
        if(pixel == color_old)
            pixel = color_new;

    SetDIBits(hdc, hbmp, 0, bm.bmHeight, &pixels[0], &bi, DIB_RGB_COLORS);
    ::ReleaseDC(HWND_DESKTOP, hdc);
}
我找到了这个。我在这里复制生成的代码:

#define COLORREF2RGB(Color) (Color & 0xff00) | ((Color >> 16) & 0xff) \
                                 | ((Color << 16) & 0xff0000)

//-------------------------------------------------------------------------------
// ReplaceColor
//
// Author    : Dimitri Rochette drochette@coldcat.fr
// Specials Thanks to Joe Woodbury for his comments and code corrections
//
// Includes  : Only <windows.h>

//
// hBmp         : Source Bitmap
// cOldColor : Color to replace in hBmp
// cNewColor : Color used for replacement
// hBmpDC    : DC of hBmp ( default NULL ) could be NULL if hBmp is not selected
//
// Retcode   : HBITMAP of the modified bitmap or NULL for errors
//
//-------------------------------------------------------------------------------
HBITMAP ReplaceColor(HBITMAP hBmp,COLORREF cOldColor,COLORREF cNewColor,HDC hBmpDC)
{
    HBITMAP RetBmp=NULL;
    if (hBmp)
    {
        HDC BufferDC=CreateCompatibleDC(NULL);    // DC for Source Bitmap
        if (BufferDC)
        {
            HBITMAP hTmpBitmap = (HBITMAP) NULL;
            if (hBmpDC)
                if (hBmp == (HBITMAP)GetCurrentObject(hBmpDC, OBJ_BITMAP))
            {
                hTmpBitmap = CreateBitmap(1, 1, 1, 1, NULL);
                SelectObject(hBmpDC, hTmpBitmap);
            }

            HGDIOBJ PreviousBufferObject=SelectObject(BufferDC,hBmp);
            // here BufferDC contains the bitmap

            HDC DirectDC=CreateCompatibleDC(NULL); // DC for working
            if (DirectDC)
            {
                // Get bitmap size
                BITMAP bm;
                GetObject(hBmp, sizeof(bm), &bm);

                // create a BITMAPINFO with minimal initilisation 
                // for the CreateDIBSection
                BITMAPINFO RGB32BitsBITMAPINFO; 
                ZeroMemory(&RGB32BitsBITMAPINFO,sizeof(BITMAPINFO));
                RGB32BitsBITMAPINFO.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
                RGB32BitsBITMAPINFO.bmiHeader.biWidth=bm.bmWidth;
                RGB32BitsBITMAPINFO.bmiHeader.biHeight=bm.bmHeight;
                RGB32BitsBITMAPINFO.bmiHeader.biPlanes=1;
                RGB32BitsBITMAPINFO.bmiHeader.biBitCount=32;

                // pointer used for direct Bitmap pixels access
                UINT * ptPixels;    

                HBITMAP DirectBitmap = CreateDIBSection(DirectDC, 
                                       (BITMAPINFO *)&RGB32BitsBITMAPINFO, 
                                       DIB_RGB_COLORS,
                                       (void **)&ptPixels, 
                                       NULL, 0);
                if (DirectBitmap)
                {
                    // here DirectBitmap!=NULL so ptPixels!=NULL no need to test
                    HGDIOBJ PreviousObject=SelectObject(DirectDC, DirectBitmap);
                    BitBlt(DirectDC,0,0,
                                   bm.bmWidth,bm.bmHeight,
                                   BufferDC,0,0,SRCCOPY);

                       // here the DirectDC contains the bitmap

                    // Convert COLORREF to RGB (Invert RED and BLUE)
                    cOldColor=COLORREF2RGB(cOldColor);
                    cNewColor=COLORREF2RGB(cNewColor);

                    // After all the inits we can do the job : Replace Color
                    for (int i=((bm.bmWidth*bm.bmHeight)-1);i>=0;i--)
                    {
                        if (ptPixels[i]==cOldColor) ptPixels[i]=cNewColor;
                    }
                    // little clean up
                    // Don't delete the result of SelectObject because it's 
                    // our modified bitmap (DirectBitmap)
                       SelectObject(DirectDC,PreviousObject);

                    // finish
                    RetBmp=DirectBitmap;
                }
                // clean up
                DeleteDC(DirectDC);
            }            
            if (hTmpBitmap)
            {
                SelectObject(hBmpDC, hBmp);
                DeleteObject(hTmpBitmap);
            }
            SelectObject(BufferDC,PreviousBufferObject);
            // BufferDC is now useless
            DeleteDC(BufferDC);
        }
    }
    return RetBmp;
}
结果是:


谢谢,但似乎不起作用。例如,如果我有一个24位的图像,背景为白色,矩形为红色,在我的菜单中,我以一个白色方块结束。根据这两个标志,它声明:如果加载的位图颜色深度大于8bpp,则不要使用此选项。如果我将同一图像的白色背景降低到8位,则渲染效果会更好,但是,使用对话框颜色作为背景,而不是菜单颜色。真的,背景应以GetSysColorCOLOR_菜单结束。@AndrewTruckle CBitmap具有HBITMAP运算符,它在请求时返回位图句柄。使用调试器,您可以单步切换_colorbmp,从而切换_colorbmp.m_hobject您可以将LR_LOADTRANSPARENT | LR_LOADMAP3DCOLORS与16幅4位彩色图像一起使用color@BarmakShemirani我刚刚将图像的颜色深度从8降低到4,结果是一样的。它们都是用对话框窗口背景而不是GetSysColorCOLOR_菜单背景绘制的。我不确定。它可以在我的电脑上使用8位和4位图像。这可能是Windows盲板的具体问题。@BarmakShemirani看到我的答案了。听起来你好像在什么地方打错了。如果找不到,请单独制作MVCE。我将此答案留在这里,但我在关闭Windows百叶窗时遇到了问题。
HBITMAP hBmp = (HBITMAP)::LoadImage(AfxGetResourceHandle(),
    MAKEINTRESOURCE(IDB_BITMAP1),
    IMAGE_BITMAP,
    0, 0, // cx,cy
    LR_LOADTRANSPARENT | LR_LOADMAP3DCOLORS);   
HBITMAP hBmp2 = ReplaceColor(hBmp, RGB(71, 71, 71), GetSysColor(COLOR_MENU), NULL);
DeleteObject(hBmp);

m_bmpSwap.Attach(hBmp2);
pMnuPopup->SetMenuItemBitmaps(0, MF_BYPOSITION, &m_bmpSwap, &m_bmpSwap);