I am no expert in anything GDI. But I was given some code YEARS ago which has served me decently well. But, it's getting old ... and with new windows10 dark theme, it's showing it's flaws.
I am rendering a menu (in an explorer menu plugin). Here's the snippet of code used to generate the bitmap.
My goal, to convert this code to generate a bitmap with transparency of the icon preserved.
(the result HBITMAP ends up in pItem->m_hBitmap )
HICON hIcon;
if ( (iIndex >= 0) && (ExtractIconEx(iconDLLPath, iIndex, NULL, &hIcon, 1) != 0) )
{
HDC hdc = CreateIC(L"DISPLAY", NULL, NULL, NULL);
HDC hdcMem = CreateCompatibleDC(hdc);
// XP demands 12x12, otherwise use 16x16
int cx = GetSystemMetrics((m_bUseSmallerIcons) ? SM_CXMENUCHECK : SM_CXSMICON);
int cy = GetSystemMetrics((m_bUseSmallerIcons) ? SM_CYMENUCHECK : SM_CYSMICON);
pItem->m_hBitmap = CreateCompatibleBitmap(hdc, cx, cy);
HBITMAP hBmOld = (HBITMAP) SelectObject(hdcMem, pItem->m_hBitmap);
// DC: paint entire mem dc COLOR_MENU so icon looks transparent
// when painted into context menu having this background color
HBRUSH hBrush = CreateSolidBrush(GetSysColor(COLOR_MENU));
RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = cx;
rect.bottom = cy;
FillRect(hdcMem, &rect, hBrush);
DeleteObject(hBrush);
// Draw icon transparently, on top of the background color. Transparent
// areas will be the background color.
DrawIconEx(hdcMem, 0, 0, hIcon, cx, cy, 0, 0, DI_NORMAL);
// Cleanup
SelectObject(hdcMem, hBmOld);
DeleteDC(hdc);
DeleteDC(hdcMem);
DestroyIcon(hIcon);
}
I should remove the which draws the white background, but how do I put down a transparent background? Everything I've tried yields a black background.
* just removing the white "fill"
* SetBkMode(TRANSPARENT)
* using the theme code to get the menu color...
How do I go about making a proper bitmap with transparency?
DrawIconEx
is probably doing its job with transparency. But the bitmap is not transparent. I don't know exactly what is going on here, you could draw on PNG file with transparency, or paint the bitmap usingTransparentBlt
... – Barmak Shemirani