1

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!

Henry
  • 727
  • 1
  • 6
  • 25
  • I'm not sure, but `NMLISTVIEW::lParam` seems like the custom data you want. – Liviu May 30 '13 at 09:16
  • Already tried that, doesn't seem to work. I'm not sure how I have to get the information out of the lParam later on : lplvcd->clrTextBk = (COLORREF)lParam; -> is creating random colors – Henry May 30 '13 at 09:25
  • How about `NMCUSTOMDRAW::lItemlParam`, but, then again, I don't know how to set it. – Liviu May 30 '13 at 09:33
  • NMCUSTOMDRAW::lItemlParam keeps returning 0 :/ – Henry May 30 '13 at 10:18
  • 4
    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` :) – Jonathan Potter May 30 '13 at 11:42
  • Thanks, exactly what I was looking for! Is there any way I can flag your answer as accepted? :) – Henry May 30 '13 at 12:38
  • Sure, I'll post it as an answer, I just wasn't sure that was actually the question you were asking :) – Jonathan Potter May 30 '13 at 12:49

2 Answers2

2

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 :)

Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
  • How do you use the `NMCUSTOMDRAW::lItemlParam` member ? I mean how do you retrieve the current `NMCUSTOMDRAW` object ? – PinkTurtle Dec 08 '15 at 17:51
  • **EDIT** All right it's `LPNMTVCUSTOMDRAW lpNMCustomDraw = (LPNMTVCUSTOMDRAW)lParam` and `lpNMCustomDraw->nmcd.lItemlParam` for tree view items. – PinkTurtle Dec 08 '15 at 17:57
0

I think the (exact) answer lies here: Using Custom Draw.

Liviu
  • 1,859
  • 2
  • 22
  • 48
  • Well, I was using this article as a template and it explains how to use customdraw but it doesn't show how to pass the argument via SendMessage or am I missing something? – Henry May 30 '13 at 10:13