Click here to Skip to main content
15,860,859 members
Articles / Desktop Programming / MFC

CTreeCtrlEx - Setting Color and Font Attribute for Individual Items in Tree Control

Rate me:
Please Sign up or sign in to vote.
4.68/5 (24 votes)
26 May 20021 min read 244.4K   11.1K   63   29
Allows to display color items and change the font style

Sample Image - ColorTreeCtrl.jpg

Introduction

This is an example of 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.

Features

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

Usage

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

C++
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));
}

Acknowledgements

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

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
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

 
AnswerRe: Setting Text Background Color Pin
stokos9-Mar-06 1:59
stokos9-Mar-06 1:59 
GeneralRe: Setting Text Background Color Pin
Samuele9-Mar-06 4:05
Samuele9-Mar-06 4:05 
GeneralIME Issue in Tree Control...!! Pin
boonshajayan25-Aug-03 22:33
boonshajayan25-Aug-03 22:33 
QuestionHow to display a turecolor image or icon in the treectrl? Pin
Jack_Cai24-Apr-03 3:32
Jack_Cai24-Apr-03 3:32 
GeneralBug when changing item height Pin
Alex Hazanov16-Apr-03 1:45
Alex Hazanov16-Apr-03 1:45 
Generalhelp Pin
xuchuangeng30-Dec-02 0:58
xuchuangeng30-Dec-02 0:58 
Generalanother one, Pin
Mikelangelo3-Jun-02 21:19
Mikelangelo3-Jun-02 21:19 
GeneralRe: another one, Pin
MrKii9-Dec-05 23:41
MrKii9-Dec-05 23:41 
hey! it's only 189$ !!
Generalyet another treectrlex Pin
26-May-02 23:21
suss26-May-02 23:21 

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

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