SetMenuItemBitmaps unter Windows 10 Dark Mode



  • Ich finde keine Beiträge oder sample code wie man SetMenuItemBitmaps so verwendet dass es mit dem Dark Mode von Windows 10 kompatibel ist. Das Menü ist dunkel, weil es eine Shell Extension ist. Kennt zufällig jemand ein Open-Source-Programm, dessen Code mir weiter helfen könnte?

    HBITMAP MakeBitMapTransparent(HBITMAP hbmSrc)
    {
        HDC hdcSrc, hdcDst;
        HBITMAP hbmOld, hbmNew;
        BITMAP bm;
        COLORREF clrTP, clrBK;
    
        if ((hdcSrc = CreateCompatibleDC(NULL)) != NULL) {
            if ((hdcDst = CreateCompatibleDC(NULL)) != NULL) {
                int nRow, nCol;
                GetObject(hbmSrc, sizeof(bm), &bm);
                hbmOld = (HBITMAP)SelectObject(hdcSrc, hbmSrc);
                hbmNew = CreateBitmap(bm.bmWidth, bm.bmHeight, bm.bmPlanes, bm.bmBitsPixel, NULL);
                SelectObject(hdcDst, hbmNew);
                BitBlt(hdcDst, 0, 0, bm.bmWidth, bm.bmHeight, hdcSrc, 0, 0, SRCCOPY);
                clrTP = GetPixel(hdcDst, 0, 0);
                clrBK = GetSysColor(COLOR_MENU);
    
                for (nRow = 0; nRow < bm.bmHeight; nRow++)
                    for (nCol = 0; nCol < bm.bmWidth; nCol++)
                        if (GetPixel(hdcDst, nCol, nRow) == clrTP)
                            SetPixel(hdcDst, nCol, nRow, clrBK);
    
                DeleteDC(hdcDst);
            }
    
            DeleteDC(hdcSrc);
        }
    
        return hbmNew;
    }
    
    
    void SetIcon(HMENU menu, UINT command, Item* item)
    {
        if (!FileExist(item->IconFile))
            return;
    
        HICON iconSmall;
        HICON iconLarge = NULL;
        UINT res = ExtractIconEx(item->IconFile.c_str(), item->IconIndex, &iconLarge, &iconSmall, 1);
        assert(res);
        ICONINFO iconInfo;
        res = GetIconInfo(iconSmall, &iconInfo);
        assert(res);
        //HBITMAP bmp = MakeBitMapTransparent(iconInfo.hbmColor);
        HBITMAP bmp = iconInfo.hbmColor;
        SetMenuItemBitmaps(menu, command, MF_BITMAP | MF_BYCOMMAND, bmp, iconInfo.hbmColor);
        assert(res);
    }


  • Habe jetzt funktionierenden sample code gefunden.

    HBITMAP ConvertIconToBitmap(HICON hicon)
    {
        IWICImagingFactory* pFactory;
        IWICBitmap* pBitmap;
        IWICFormatConverter* pConverter;
        HBITMAP ret = NULL;
    
        if (SUCCEEDED(CoCreateInstance(CLSID_WICImagingFactory, NULL,
            CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (void**)&pFactory)))
        {
            if (SUCCEEDED(pFactory->CreateBitmapFromHICON(hicon, &pBitmap)))
            {
                if (SUCCEEDED(pFactory->CreateFormatConverter(&pConverter)))
                {
                    UINT cx, cy;
                    PBYTE pbBits;
                    HBITMAP hbmp;
    
                    if (SUCCEEDED(pConverter->Initialize(pBitmap, GUID_WICPixelFormat32bppPBGRA,
                        WICBitmapDitherTypeNone, NULL, 0.0f, WICBitmapPaletteTypeCustom)) && SUCCEEDED(
                            pConverter->GetSize(&cx, &cy)) && NULL != (hbmp = Create32BitHBITMAP(cx, cy, &pbBits)))
                    {
                        UINT cbStride = cx * sizeof(UINT32);
                        UINT cbBitmap = cy * cbStride;
    
                        if (SUCCEEDED(pConverter->CopyPixels(NULL, cbStride, cbBitmap, pbBits)))
                            ret = hbmp;
                        else
                            DeleteObject(hbmp);
                    }
    
                    pConverter->Release();
                }
    
                pBitmap->Release();
            }
    
            pFactory->Release();
        }
    
        return ret;
    }

Log in to reply