Transparent Images in ImageLists for ListViews

Here is a photo of my program:

Whiteflag

As you can see, the icons are not transparent, but simply white. This is problematic because I encoded the list view to alternate colors, and white looks very ugly on gray.

Right now, I'm using a pink bitmap for the icons and using pink as a mask. Here is the code for my 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);

Here is the code for individually drawing a list (alternating colors)

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;
}

Quite frankly, I completely lost how to approach this. Does anyone have any idea?

+2
source share
2 answers

. ListView_SetBkImage, . , , NM_CUSTOMDRAW , CDRF_NEWFONT. , FillRect, CDDS_ITEMPREPAINT CDRF_DODEFAULT CDRF_NEWFONT, .

0

. , SetBkColor (RGB (...)), RGB (...) . 16x16 4b BMP . FillRect() clrTextBk. . CListCtrl, SetBkColor() ( ).

. , OnEraseBkgnd(). .

,

Olexiy

0

Source: https://habr.com/ru/post/1704547/


All Articles