c++winapitreeviewcustom-draw

Winapi : Change Color of a TreeView using CustomDraw


I am currently trying to change the background color of a TreeView item. Therefore, I am using this message to create the item:

    SendMessage(ListView, LVM_INSERTITEM, 0, (LPARAM)&lvI);

Additionally, I am handling the custom draw message like this :

        case WM_NOTIFY:
        {
            LPNMLISTVIEW pnm = (LPNMLISTVIEW)lParam;
            if (pnm->hdr.code == NM_CUSTOMDRAW)
            {
                LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;
                switch (lplvcd->nmcd.dwDrawStage)
                {
                    case CDDS_PREPAINT :
                        return CDRF_NOTIFYITEMDRAW;
                    case CDDS_ITEMPREPAINT:
                        lplvcd->clrTextBk = ???;

                        return CDRF_NEWFONT;
                }
            }

This works fine and I can set the color to whatever I'd like to, However, I have not found a way to pass the color as a parameter right when I send the message, yet. After all, custom draw is useless for me when I can only assign a constant or random color.

Thanks for any kind of help!


Solution

  • When you add the item to the list, you can pass your own data by setting the LVIF_PARAM flag and filling out the lParam member of the LVITEM structure. This then gets passed back to you as NMCUSTOMDRAW::lItemlParam.

    Don't get confused by the lParam that that comes with the WM_NOTIFY message itself, that's a different lParam :)