Click here to Skip to main content
15,881,866 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

is there a way to display items' text in CTreeCtrl with different colors.
I mean part of item's text with red color and other part with blue?

Or is it possible to embed and display custom control in CTreeCtrl?

Thanks.
Posted
Updated 22-Jul-22 13:04pm

You can modify the colours of a tree control by using the custom drawing features[^] to adjust the colours and backgrounds. I am not sure if this would allow you to use multiple colours in a single item, but a bit of testing should help figure it out.
 
Share this answer
 
Comments
Yevgen Lisovenko 23-Feb-12 9:07am    
Unfortunately, the task is to use differnt colors for single item
Richard MacCutchan 23-Feb-12 9:20am    
As I said, I am not sure about this but it may still be possible. You will need to investigate further for yourself.
You can do it with custom draw if you want to draw the start of the item text with different color than the rest of the item text (or some other variation). Example, to draw the start part of the item in a different color than the rest of item:

1. in CDDS_ITEMPREPAINT set the color for the whole thing and let Windows draw the item.
2. in CDDS_ITEMPOSTPAINT get the DC and select the color you want for the start of the item and use ::DrawText to draw just the start text of the item.

C++
    case CDDS_ITEMPREPAINT: {
        HTREEITEM hItem = reinterpret_cast<HTREEITEM>(pNMCD->dwItemSpec);
        // let Windows draw the item text in gray here and in post item paint
        // will draw only the start text in regular text color
        pNMTVCD->clrText = ::GetSysColor(COLOR_GRAYTEXT);
        *pResult = CDRF_NOTIFYPOSTPAINT;
    }
    break;

case CDDS_ITEMPOSTPAINT: {
    HTREEITEM hItem = reinterpret_cast<HTREEITEM>(pNMCD->dwItemSpec);
    CDC*  pDC = CDC::FromHandle(pNMCD->hdc);
    const int iDpi = 96;  //TODO: figure out dpi

    CRect rcItem;
    GetItemRect(
        hItem,
        rcItem,
        TRUE);  // item text area rectangle (excluding images)
    if (rcItem.IsRectEmpty())
        break;

     if (myItem) {
        CRect rcText(rcItem);
        rcText.DeflateRect(
            CSize(
                ::GetSystemMetricsForDpi(SM_CXEDGE, iDpi),
                ::GetSystemMetricsForDpi(SM_CYEDGE, iDpi)));
        CFont *pFont = GetFont();
        ASSERT(pFont);
        if (pFont) {
            const CString sName("my text start");
            const int nSavedDC = pDC->SaveDC();
            CFont *pFontOld = pDC->SelectObject(pFont);
            pDC->SetTextColor(
                ::GetSysColor(COLOR_WINDOWTEXT));
            pDC->DrawText(
                sName,
                -1,
                rcText,
                DT_NOPREFIX
                    | DT_SINGLELINE
                    | DT_VCENTER);
            pDC->SelectObject(pFontOld);
            pDC->RestoreDC(nSavedDC);
        }
    }
 break;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900