这是我的程序的图片:
如您所见,图标不是透明的,只是白色的。这是有问题的,因为我已经将 ListView 编码为交替颜色,而白色在灰色上看起来非常难看。
现在,我正在使用 bitmap with a pink background对于图标,并使用粉红色作为 mask 。这是我的 HIMAGELIST 的代码:
hImageList = ImageList_Create(16, 16, ILC_COLOR32 | ILC_MASK, ICON_COUNT, 0);
if (hImageList != NULL)
{
HBITMAP hBitmap = LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_ICONS));
if (hBitmap != NULL)
{
ImageList_AddMasked(hImageList, hBitmap, RGB(0xFF, 0, 0xFF)); // pink mask
DeleteObject(hBitmap);
}
ImageList_SetBkColor(hImageList, CLR_NONE);
}
ListView_SetImageList(hWnd, hImageList, LVSIL_SMALL);
这是 ListView 的自定义绘制(交替颜色)的代码
LRESULT WhiteFlagUI::PaintListView(__in HWND hwndListView, __in LPARAM lParam)
{
LPNMLVCUSTOMDRAW lpListDraw = reinterpret_cast<LPNMLVCUSTOMDRAW>(lParam);
switch (lpListDraw->nmcd.dwDrawStage)
{
case CDDS_PREPAINT:
return (CDRF_NOTIFYPOSTPAINT | CDRF_NOTIFYITEMDRAW | CDRF_NOTIFYSUBITEMDRAW);
break;
case (CDDS_PREPAINT | CDDS_ITEM):
{
RECT rect;
if (ListView_GetSubItemRect(hwndListView, lpListDraw->nmcd.dwItemSpec, lpListDraw->iSubItem, LVIR_BOUNDS, &rect))
{
COLORREF color;
// determine color
if (lpListDraw->nmcd.uItemState & CDIS_SELECTED)
color = RGB(157, 173, 215);
else if (lpListDraw->nmcd.dwItemSpec % 2)
color = RGB(240, 240, 240);
else
color = RGB(255, 255, 255);
// paint
HBRUSH hBrush = CreateSolidBrush(color);
if (hBrush != NULL)
{
FillRect(lpListDraw->nmcd.hdc, &rect, hBrush);
DeleteObject(hBrush);
}
// return color info
lpListDraw->clrTextBk = color;
return CDRF_NEWFONT;
}
}
break;
}
return CDRF_DODEFAULT;
}
坦率地说,我完全不知道如何处理这个问题。有人有什么想法吗?
最佳答案
我发现了一些解决这个问题的技巧。如果您使用 ListView_SetBkImage 将背景图像设置为空白的白色位图,它将强制图标透明绘制。不幸的是,这样做会导致 NM_CUSTOMDRAW 忽略使用 CDRF_NEWFONT 设置的背景颜色。要绕过它,请调用 FillRect 以填充 CDDS_ITEMPREPAINT 中项目的背景并返回 CDRF_DODEFAULT 或 CDRF_NEWFONT(如果您也更改前景颜色)。
关于c++ - ImageLists for ListViews 中的透明图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6893351/