Click here to Skip to main content
Licence 
First Posted 26 May 2002
Views 171,835
Bookmarked 54 times

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

By | 26 May 2002 | Article
Allows to display color items and change the font style

Sample Image - ColorTreeCtrl.jpg

Introduction

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.

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

Acknowledgements

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

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

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

About the Author

lion

Software Developer (Senior)

Germany Germany

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionFull row select (HIGHLIGHTED) Pinmemberfurtel2:45 2 Sep '11  
QuestionJapanese error SetItemColor change text to garbage characters Pinmemberyotamwimmer19:07 16 Aug '11  
GeneralAnother way to do this without redrawing PinmemberMike Heskamp10:24 19 Nov '09  
GeneralRe: Another way to do this without redrawing Pinmemberzhuweiping16:30 21 Dec '09  
GeneralActually, I need the Colorful TreeView in my project. Pinmembersnookermx17:43 8 Sep '09  
GeneralCTreeCtrlEx in a CTreeView Pinmembermimosa13:32 21 Jun '09  
GeneralCTreeCtrlEx license restrictions PinmemberclintM7:45 22 Apr '09  
GeneralUpdating the GUI Pinmemberkydyl21:00 9 Oct '07  
GeneralOverloaded DeleteItem() and DeleteAllItems() PinmemberChristoph Conrad5:21 29 Aug '07  
GeneralRe: Overloaded DeleteItem() and DeleteAllItems() PinmemberGukki22:46 25 May '08  
Christoph Conrad wrote:
BOOL CTreeCtrlEx::DeleteItem(HTREEITEM hItem)
{
m_mapColorFont.RemoveKey(hItem);
return ((CTreeCtrl*) this)->DeleteItem( hItem );
}
 
BOOL CTreeCtrlEx::DeleteAllItems()
{
m_mapColorFont.RemoveAll();
return ((CTreeCtrl*) this)->DeleteAllItems();
}

 
Thanks for the bugfix. You can use an easier notation:
 
BOOL CTreeCtrlEx::DeleteItem(HTREEITEM hItem)
{
  m_mapColorFont.RemoveKey(hItem);
  return CTreeCtrl::DeleteItem( hItem );
}
 
BOOL CTreeCtrlEx::DeleteAllItems()
{
  m_mapColorFont.RemoveAll();
  return CTreeCtrl::DeleteAllItems();
}

GeneralHorizontal scrollbar missing PinmemberAndreas Gut2:39 11 Nov '05  
GeneralFunctionality Enhancement Request - Custom Highlight Color Pinmembertytan19:59 7 Jun '05  
Questionhow to i change selected item bk and fg color PinmemberJiten D. Gandhi20:31 16 Mar '05  
GeneralSetting Text Background Color Pinsussmister trunks10:31 5 Nov '03  
GeneralRe: Setting Text Background Color PinmemberSamuele3:06 27 Nov '03  
GeneralRe: Setting Text Background Color Pinmembermister trunks5:34 9 Dec '03  
AnswerRe: Setting Text Background Color Pinmemberstokos23:57 6 Mar '06  
GeneralRe: Setting Text Background Color PinmemberSamuele2:07 8 Mar '06  
AnswerRe: Setting Text Background Color Pinmemberstokos1:59 9 Mar '06  
GeneralRe: Setting Text Background Color PinmemberSamuele4:05 9 Mar '06  
GeneralIME Issue in Tree Control...!! Pinmemberboonshajayan22:33 25 Aug '03  
QuestionHow to display a turecolor image or icon in the treectrl? PinmemberJack_Cai3:32 24 Apr '03  
GeneralBug when changing item height PinmemberAlex Hazanov1:45 16 Apr '03  
Generalhelp Pinmemberxuchuangeng0:58 30 Dec '02  
Generalanother one, PinmemberMikelangelo21:19 3 Jun '02  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 27 May 2002
Article Copyright 2002 by lion
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid