c++cwinapitreeviewcommon-controls

Color specific treeview item using WIN32 API (C/C++)


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

Solution

  • 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.