Click here to Skip to main content
11,408,835 members (61,392 online)
Click here to Skip to main content

CTreeCtrlEx - Setting color and font attribute for individual items in tree control

, 26 May 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
Allows to display color items and change the font style

Sample Image - ColorTreeCtrl.jpg


This is an example how to enable colors in Tree Controls. Actually I saw a similar Article from a friend, but it was based on CTreeView, not on CTreeCtrl which I actually needed.


Below are some of the many features that CTreeCtrlEx has.

  • Change the item color.
  • Change the item font.
  • Make the item bold.
  • Get the item color.
  • Get the item font.
  • Get if item is bold.
  • And more in future.


It is so easy, you only need to include the class CTreeCtrlEx in your project, and create control member variable from that class. The function name and the parameters you can find in the source zip file. Here is some sample code from the demo app. Here m_cTree is a member of type CTreeCtrlEx

HTREEITEM hOrder[1024], hContent[1024];
CString sStr;

for(int i = 0; i < 255; i++) 
    sStr.Format("Parent%d", i);
    hOrder[i] = m_cTree.InsertItem(sStr);
    for (int j = 0; j < 5; j++) 
        sStr.Format("Child%d", j);
        hContent[j] = m_cTree.InsertItem(sStr, hOrder[i]);
        m_cTree.SetItemColor(hContent[j], RGB(i, 255, 0));
    m_cTree.SetItemColor(hOrder[i], RGB(255, i, 0));


The CTreeCtrlEx is inspired by code, ideas, and submissions from the following:

Setting color and font attribute for individual items - By Zafir Anjum


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


About the Author

Software Developer (Senior)
Germany Germany
Lion is a software engineer start working with C/C++ in 1995, after that he move to the internet developing with PERL, PHP, ASP, JSP, MySql. now he since 12 years is working on Object Oriented, Visual C++ (he could say that he speak VC++ better than his nativ language), SDK, MFC, COM, COM+, ATL, ADO, ODBC, Internet technologies, DataBase (MS-SQLServer, ORACLE), named pipe, threading... you name it.
And now he is very interesting in .NET technology, as well in Android development and maybe later in Windows Phone 7.
Lion is originally from Bulgaria, he is working right now in Germany since 2001, developing a hospital/Labor software.

Comments and Discussions

QuestionLicense restrictions PinmemberJim Kotelly15-Jun-12 6:17 
QuestionFull row select (HIGHLIGHTED) Pinmemberfurtel2-Sep-11 3:45 
QuestionJapanese error SetItemColor change text to garbage characters Pinmemberyotamwimmer16-Aug-11 20:07 
GeneralAnother way to do this without redrawing PinmemberMike Heskamp19-Nov-09 11:24 
GeneralRe: Another way to do this without redrawing Pinmemberzhuweiping21-Dec-09 17:30 
GeneralActually, I need the Colorful TreeView in my project. Pinmembersnookermx8-Sep-09 18:43 
GeneralCTreeCtrlEx in a CTreeView Pinmembermimosa21-Jun-09 14:32 
GeneralCTreeCtrlEx license restrictions PinmemberclintM22-Apr-09 8:45 
GeneralUpdating the GUI Pinmemberkydyl9-Oct-07 22:00 
GeneralOverloaded DeleteItem() and DeleteAllItems() PinmemberChristoph Conrad29-Aug-07 6:21 
GeneralRe: Overloaded DeleteItem() and DeleteAllItems() PinmemberGukki25-May-08 23:46 
GeneralHorizontal scrollbar missing PinmemberAndreas Gut11-Nov-05 3:39 
GeneralFunctionality Enhancement Request - Custom Highlight Color Pinmembertytan7-Jun-05 20:59 
Questionhow to i change selected item bk and fg color PinmemberJiten D. Gandhi16-Mar-05 21:31 
GeneralSetting Text Background Color Pinsussmister trunks5-Nov-03 11:31 
GeneralRe: Setting Text Background Color PinmemberSamuele27-Nov-03 4:06 
GeneralRe: Setting Text Background Color Pinmembermister trunks9-Dec-03 6:34 
AnswerRe: Setting Text Background Color Pinmemberstokos7-Mar-06 0:57 
GeneralRe: Setting Text Background Color PinmemberSamuele8-Mar-06 3:07 
AnswerRe: Setting Text Background Color Pinmemberstokos9-Mar-06 2:59 
GeneralRe: Setting Text Background Color PinmemberSamuele9-Mar-06 5:05 
GeneralIME Issue in Tree Control...!! Pinmemberboonshajayan25-Aug-03 23:33 
QuestionHow to display a turecolor image or icon in the treectrl? PinmemberJack_Cai24-Apr-03 4:32 
GeneralBug when changing item height PinmemberAlex Hazanov16-Apr-03 2:45 
If I change the Items height in the tree the colored text is misplaced!

This lines fixes that:
int height = GetItemHeight();
memDC.TextOut(rect.left + 2, + (height - 16)/2+ 1, sItem);

Instead of:
memDC.TextOut(rect.left + 2, + 1, sItem);

Generalhelp Pinmemberxuchuangeng30-Dec-02 1:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.5 | Last Updated 27 May 2002
Article Copyright 2002 by lion
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid