0

I wanted to create a tree view where specific items have a different back & text colors. I did found the following solution in the internet: Win32 Custom Draw Treeview Control, but here they color each item according to its level. This is close to what I want, but I want to color just a specific treeview item regardless of its level, lets say by its TVITEM handle or its HTREEITEM.

Is it possible to do such a thing using the NM_CUSTOMDRAW message? If not, how can I do such a thing?

Edit: I've been trying to use the item's lParam in order to identify the treeview item, but the items remain invisible for some reason. Here is my function which is supposed to handle the custom draw:

/*
This function will custom draw a tree view
Input: (LRESULT*) res = To store the result (by reference, to be stored)
       (HWND) window = The handled window
       (LPNMTVCUSTOMDRAW) item = The item to draw
       (TVITEM) tvItem = The tv item that should be custom drawn
Output: (BOOL) TRUE if should use the stored value, otherwise FALSE
*/
BOOL customDrawTreeView(LRESULT* res, HWND window, LPNMTVCUSTOMDRAW item, TVITEM tvItem)
{
    switch (item->nmcd.dwDrawStage)
    {
    case CDDS_PREPAINT:
        *res = CDRF_NOTIFYITEMDRAW;
        return TRUE;
        break;
    case CDDS_ITEMPREPAINT:
        if (tvItem.lParam == item->nmcd.lItemlParam)
        {
            item->clrTextBk = 0x383838;
            item->clrText = RGB(255, 255, 255);
            *res = CDRF_SKIPDEFAULT;
            return TRUE;
        }

        break;
    }

    return FALSE;
}
aviad1
  • 354
  • 1
  • 10

1 Answers1

1

Yes. You can attach information to the tree item using the TVITEM::lParam member. This value is sent with the NM_CUSTOMDRAW message in the nmcd.lItemlParam member of the NMTVCUSTOMDRAW struct.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
  • This looks like the perfect solution but I appear to have another problem: The CDDS_ITEMPREPAINT case in my switch statement appears to never be reached. Do you have any idea why would that happen? – aviad1 Jul 06 '20 at 23:47
  • The CDDS_PREPAINT case is always the one that's reached, even tho the tree view **does** contain items – aviad1 Jul 06 '20 at 23:48
  • Managed to figure it out but it still does not do what I want. The tree view remains empty for some reason, even tho my code is reached and the ```clrTextBk``` & ```clrText``` are set, the items remain invisible. Can you please look at the edit of my question? – aviad1 Jul 08 '20 at 23:21
  • Managed to figure it out (I had to use the SetWindowLongPtr function in both switch cases). Thanks a lot for your help. – aviad1 Jul 09 '20 at 19:38