其他分享
首页 > 其他分享> > c – ListViews的ImageLists中的透明图像

c – ListViews的ImageLists中的透明图像

作者:互联网

这是我的程序图片:

如您所见,图标不透明,只是白色.这是有问题的,因为我已经将列表视图编码为替代颜色,并且白色在灰色上看起来非常难看.

现在,我使用bitmap with a pink background作为图标,并使用粉红色作为蒙版.这是我的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);

这是list-view的Custom Draw(交替颜色)的代码

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,transparency,listview,winapi,imagelist
来源: https://codeday.me/bug/20191007/1865335.html