作为一个更大项目的一部分,我试图从现有的 HICON 创建一个 HICON,首先将该图标绘制为 HBITMAP,然后将其转换回 HICON。(这将允许我修改图标,例如,通过在其上绘制文本。)
不幸的是,该图标显示为黑色方块。这是我的代码:
bool createIcon()
{
bool ret = false;
// get context of window
HDC context = GetDC(window_);
if (context != NULL)
{
// create a buffer context
HDC buffer = CreateCompatibleDC(context);
if (buffer != NULL)
{
PatBlt(buffer, 0, 0, iconSize_, iconSize_, WHITENESS);
// create a bitmap for our use
HBITMAP bitmap = CreateCompatibleBitmap(context, iconSize_, iconSize_);
if (bitmap != NULL)
{
HBITMAP oldBitmap;
// select the bitmap with the buffer
oldBitmap = reinterpret_cast<HBITMAP>(SelectObject(buffer, bitmap));
if (oldBitmap != NULL)
{
// draw the icon
if (DrawIcon(buffer, 0, 0, icon_))
{
if (icon_ != NULL)
DestroyIcon(icon_);
ImageList_RemoveAll(imageList_);
if (ImageList_Add(imageList_, bitmap, NULL) != -1)
{
icon_ = ImageList_GetIcon(imageList_, 0, ILD_NORMAL);
if (icon_ != NULL)
ret = true;
}
}
SelectObject(buffer, oldBitmap);
}
DeleteObject(bitmap);
}
DeleteDC(buffer);
}
DeleteDC(context);
}
return ret;
}
有任何想法吗?