Click here to Skip to main content
12,896,849 members (51,414 online)
Click here to Skip to main content
Add your own
alternative version

Stats

204.5K views
9.3K downloads
60 bookmarked
Posted 26 May 2002

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

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 <p>

License

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

Share

About the Author

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

You may also be interested in...

Comments and Discussions

 
QuestionLicense restrictions Pin
Jim Kotelly15-Jun-12 5:17
memberJim Kotelly15-Jun-12 5:17 
QuestionFull row select (HIGHLIGHTED) Pin
furtel2-Sep-11 2:45
memberfurtel2-Sep-11 2:45 
QuestionJapanese error SetItemColor change text to garbage characters Pin
yotamwimmer16-Aug-11 19:07
memberyotamwimmer16-Aug-11 19:07 
GeneralAnother way to do this without redrawing Pin
Mike Heskamp19-Nov-09 10:24
memberMike Heskamp19-Nov-09 10:24 
When working with this, it worked well but there are a few draw backs resulting from having to draw the text on top of MFC's text: the text will need to be shifted for larger windows fonts, and I have noticed an issue on a foreign system with the text not lining up properly. Here is another way to do this within the drawing frame work in place by using custom drawing:

Override OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult):

In the header, add
afx_msg void OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult);


In Message Map, add
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)


Then in the OnCustomDraw() function, you can tell it to loop through the items with each call and access each item individually setting the font color.

This microsoft article is helpful with explaining the process of handling prepainting, postpainting, etc. and it tells what results to pass back to control the behavior: http://msdn.microsoft.com/en-us/library/bb761817(VS.85).aspx#list_tree[^]

To change an items color, cast the NMHDR structure:
LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;


Handle each painting case as described in the article and change color in CDDS_ITEMPREPAINT. To loop through each item with subsequent OnCustomDraw() calls, return CDRF_NOTIFYSUBITEMDRAW in the CDDS_PREPAINT stage:

  switch(lplvcd->nmcd.dwDrawStage)
  {
    case CDDS_PREPAINT:
      *pResult = CDRF_NOTIFYSUBITEMDRAW;
      break;   
 
    case CDDS_ITEMPREPAINT:
    {
      // Get the tree item to do whatever process needed to determine color
      HTREEITEM hTreeItem = (HTREEITEM)lplvcd->nmcd.dwItemSpec;
 
      // Set the item color
      lplvcd->clrText = RGB(225,0,0);
 
      // Color was changed, so send new font notification
      *pResult = CDRF_NEWFONT;
      break;
    }
 
    case CDDS_ITEMPOSTPAINT:
      *pResult = CDRF_DODEFAULT;
      break;
 
    default:
      *pResult = CDRF_DODEFAULT;
      break;
  }


Hope this helps...

mike
GeneralRe: Another way to do this without redrawing Pin
zhuweiping21-Dec-09 16:30
memberzhuweiping21-Dec-09 16:30 
GeneralActually, I need the Colorful TreeView in my project. Pin
snookermx8-Sep-09 17:43
membersnookermx8-Sep-09 17:43 
GeneralCTreeCtrlEx in a CTreeView Pin
mimosa21-Jun-09 13:32
membermimosa21-Jun-09 13:32 
GeneralCTreeCtrlEx license restrictions Pin
clintM22-Apr-09 7:45
memberclintM22-Apr-09 7:45 
GeneralUpdating the GUI Pin
kydyl9-Oct-07 21:00
memberkydyl9-Oct-07 21:00 
GeneralOverloaded DeleteItem() and DeleteAllItems() Pin
Christoph Conrad29-Aug-07 5:21
memberChristoph Conrad29-Aug-07 5:21 
GeneralRe: Overloaded DeleteItem() and DeleteAllItems() Pin
Gukki25-May-08 22:46
memberGukki25-May-08 22:46 
GeneralHorizontal scrollbar missing Pin
Andreas Gut11-Nov-05 2:39
memberAndreas Gut11-Nov-05 2:39 
GeneralFunctionality Enhancement Request - Custom Highlight Color Pin
tytan7-Jun-05 19:59
membertytan7-Jun-05 19:59 
Questionhow to i change selected item bk and fg color Pin
Jiten D. Gandhi16-Mar-05 20:31
memberJiten D. Gandhi16-Mar-05 20:31 
GeneralSetting Text Background Color Pin
mister trunks5-Nov-03 10:31
sussmister trunks5-Nov-03 10:31 
GeneralRe: Setting Text Background Color Pin
Samuele27-Nov-03 3:06
memberSamuele27-Nov-03 3:06 
GeneralRe: Setting Text Background Color Pin
mister trunks9-Dec-03 5:34
membermister trunks9-Dec-03 5:34 
AnswerRe: Setting Text Background Color Pin
stokos6-Mar-06 23:57
memberstokos6-Mar-06 23:57 
GeneralRe: Setting Text Background Color Pin
Samuele8-Mar-06 2:07
memberSamuele8-Mar-06 2:07 
AnswerRe: Setting Text Background Color Pin
stokos9-Mar-06 1:59
memberstokos9-Mar-06 1:59 
GeneralRe: Setting Text Background Color Pin
Samuele9-Mar-06 4:05
memberSamuele9-Mar-06 4:05 
GeneralIME Issue in Tree Control...!! Pin
boonshajayan25-Aug-03 22:33
memberboonshajayan25-Aug-03 22:33 
QuestionHow to display a turecolor image or icon in the treectrl? Pin
Jack_Cai24-Apr-03 3:32
memberJack_Cai24-Apr-03 3:32 
GeneralBug when changing item height Pin
Alex Hazanov16-Apr-03 1:45
memberAlex Hazanov16-Apr-03 1:45 
Generalhelp Pin
xuchuangeng30-Dec-02 0:58
memberxuchuangeng30-Dec-02 0:58 

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.

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