DrawIconEx留下掩码工件

时间:2012-09-18 05:42:51

标签: c++ windows winapi icons

我正在使用IImageListSHGetFileInfo为任何指定路径提取巨型图标。完成后,我会使用HICONHBITMAP渲染为DrawIconEx,以便使用GDI + BitmapGraphics对象进行最终渲染。

现在,这一切都很有效,除了当我对位图进行最终渲染时,最左边的边缘总是有一个黑色的神器。对于我得到的几乎任何图标都是如此,并且始终是左边缘。

What I see in explorer.exe on left, what my drawn icon looks like right

暗线可能来自哪个想法?

我正在使用的代码大致是:

1。提取图标:

// Get the image list index of the icon
SHFILEINFO sfi;
if (!SHGetFileInfo(pszPath, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX)) return NULL;

// Get the jumbo image list
IImageList *piml;
if (FAILED(SHGetImageList(SHIL_JUMBO, IID_PPV_ARGS(&piml)))) return NULL;

// Extract an icon
HICON hicon;
piml->GetIcon(sfi.iIcon, ILD_SCALE|ILD_TRANSPARENT, &hicon);
return hicon;

2。生成位图

HDC hDC = GetDC(NULL);
HDC hMemDC = CreateCompatibleDC(hDC);
HBITMAP hMemBmp = CreateCompatibleBitmap(hDC, x, y);
HBITMAP hResultBmp = NULL;
HGDIOBJ hOrgBMP = SelectObject(hMemDC, hMemBmp);

HBRUSH hbr = CreateSolidBrush(bg);

RECT rr = { 0, 0, 256, 256 }; // jumbo icons
FillRect(hMemDC, &rr, hbr);
DeleteBrush(hbr);
DrawIconEx(hMemDC, 0, 0, hicon, size, size, 0, NULL, DI_NORMAL);

hResultBmp = hMemBmp;
hMemBmp = NULL;

SelectObject(hMemDC, hOrgBMP);
return hResultBitmap;

3。将GDI +位图渲染为“窗口位图”:

Bitmap *b = ::New Bitmap(hResultBitmap, NULL);

Graphics    graphics(hdc);
graphics.SetTextRenderingHint(TextRenderingHintClearTypeGridFit);

SolidBrush  bgbrush(Color(255, 255, 255, 255));
Rect r(0, 0, hwnd_w, hwnd_h);
graphics.FillRectangle(&bgbrush, r);

graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
Rect r(5, 5, 128, 128);
graphics.DrawImage(dpd->image_to_draw, r);

1 个答案:

答案 0 :(得分:4)

哇,昨晚我又玩了一次。这是ILD_SCALE中的IImageList::GetIcon

摆脱这一切,这一切都完美无缺。去图......

1。提取图标:

// Get the image list index of the icon
SHFILEINFO sfi;
if (!SHGetFileInfo(pszPath, 0, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX)) return NULL;

// Get the jumbo image list
IImageList *piml;
if (FAILED(SHGetImageList(SHIL_JUMBO, IID_PPV_ARGS(&piml)))) return NULL;

// Extract an icon
HICON hicon;
piml->GetIcon(sfi.iIcon, ILD_TRANSPARENT, &hicon);
return hicon;