Quantcast
Channel: Active questions tagged listview - Stack Overflow
Viewing all articles
Browse latest Browse all 611

Why is my ListView custom drawing not applying row background colors in Win32 API?

$
0
0

I'm trying to apply custom row background colors to a ListView in a dialog-based Win32 application using WM_NOTIFY and NM_CUSTOMDRAW.However, the colors are not being applied correctly, and the ListView appears with its default colors.

Here's what I’ve tried so far:

  • Set LVS_OWNERDRAWFIXED in ListView_SetExtendedListViewStyle().

  • Handled WM_NOTIFY with CDDS_PREPAINT and CDDS_ITEMPREPAINT.

  • Used InvalidateRect() to force a refresh.

  • Ensured ListView items are added before drawing logic:

    case WM_NOTIFY:    if (((LPNMHDR)lParam)->hwndFrom == featherListView) {        LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;        switch (lplvcd->nmcd.dwDrawStage) {        case CDDS_PREPAINT:            return CDRF_NOTIFYITEMDRAW;        case CDDS_ITEMPREPAINT:            return CDRF_NOTIFYSUBITEMDRAW;        case CDDS_SUBITEM | CDDS_ITEMPREPAINT:        {            int row = lplvcd->nmcd.dwItemSpec;            if (row == 0) {                lplvcd->clrTextBk = RGB(50, 100, 200);                lplvcd->clrText = RGB(255, 255, 255);            }            else if (row % 2 == 0) {                lplvcd->clrTextBk = RGB(220, 230, 250);            }            else {                lplvcd->clrTextBk = RGB(240, 240, 255);            }            return CDRF_NEWFONT;        }        }    }    break;

Expected behavior:

The ListView rows should have alternating background colors (blue for header, light blue for even rows, very light blue for odd rows).

Actual behavior:

The ListView still appears with the default colors, and no row background changes are visible.

Question:

Am I missing any required flags or message handling?

Should I handle CDDS_SUBITEMPREPAINT differently?

Are there common pitfalls when using NM_CUSTOMDRAW with ListView?


Viewing all articles
Browse latest Browse all 611

Trending Articles