Click here to Skip to main content
12,305,299 members (72,244 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked

Simple Tree Control

, 28 Oct 2002
Rate this:
Please Sign up or sign in to vote.
An article on using CTreeCtrl in a simpler way (I believe).

Why a new tree control

First of all, there are lots of custom tree controls available so what's the need for this? In one of my projects I happened to use several tree controls to show data objects at different levels and I had to deal with user actions. Each time, my tree control has to respond in different way depending on the selected item. Here I present what I have (I re-wrote some of my code to use this custom control and I believe that it is more readeble and easy to understand now)

How it works

CSimpleTreeCtrl "simply" overrides the MFC CTreeCtrl class and provides some access functions to deal with it. CSimpleTreeCtrl::TreeCtrlItem is used to add new items in the tree control. You should write your own class (to use it in a better way) and override virtual functions to be notified by windows messages such as item selection, right click etc.

CSimpleTreeCtrl provides the following virtual functions:

virtual void          selChanged ( TreeCtrlItem *item ) {} ;
virtual void          lClick ( TreeCtrlItem *item ) {} ;
virtual void          lDblClick ( TreeCtrlItem *item ) {} ;
virtual void          rClick ( TreeCtrlItem *item ) {} ;
// will be called after three control is created
virtual void          postCreate ( void ) {} ;

As you noticed base class has nothing to do with these functions. It simply handles windows messages and calls the virtual function (I just got bored having to register my message handling functions). The base class also will let CTreeCtrl do its own stuff, so whenever you receive a lClick() you will also get a selChanged() if this click causes the current selection to change. The only difference is with rClick(): since in most cases I want right click to change the selected item, I handled this case in my message handler, so it will also send a selChanged() as well (if a selection exists). You may change this behaviour if you want.

For me the most useful part is CSimpleTreeCtrl::TreeCtrlItem. This class provides the following virtual functions:

// will be called on selection
virtual void          onSelected   ( void ) {} ;
// will be called for old selected item when there is a new selection (before 
// onSelected() for new item)
virtual void          onUnSelected ( void ) {} ;
// will be called with position in screen coordinates if user right clicks on item
virtual void          onRClick    ( CPoint pos ) {} ;
// will be called if user left double clicks on item
virtual void          onLDblClick ( void ) {} ;
// will be called after item inserted in tree
virtual void          postInsert  ( void ) {} ;

It also provides two additional functions which you may find necessary. Thanks to Michael Dunn and his article "Neat Stuff to do in List Controls Using Custom Draw"

void          setTextColor   ( COLORREF color ) ;
void          setBkColor     ( COLORREF color ) ;

The default color for item text is black ( RGB(0, 0, 0) ) and item background is white ( RGB(255, 255, 255) ).

You can write your own class handling only required virtual functions as shown below.

class BaseCtrlItem : public CSimpleTreeCtrl::TreeCtrlItem
     BaseCtrlItem ( CString name, CMyDataClass *myData )
          : CSimpleTreeCtrl::TreeCtrlItem ( name )
          m_myData = myData ;
     } ;

     virtual ~BaseCtrlItem () {} ;

     virtual void     postInsert ( void )
          //You can insert sub items to the tree control if necessary
     } ;

     virtual void     onRClick ( CPoint pos )
          //Show popup menu according to your data pointer
     } ;

     CMyDataClass *m_myData ;

If an item is removed from the tree (using CSimpleTreeCtrl or CTreeCtrl functions) related the CSimpleTreeCtrl::TreeCtrlItem pointer will be removed as well, so you do not need to worry about deleting the item pointers.

There is some more to add to this class but I prefer to keep it as simple as possible. Also, it handles most of the requirements in my project. I would, however, like to hear if there is any usefull features to add while keeping it simple.

The demo application creates a new tree control and adds some items in different colors. It also handles the onRClick() function in items. You can just add SimpleTreeCtrl.h and SimpleTreeCtrl.cpp to your project and enjoy it. The header file should be clear enough for documentation, if it is not, just let me know Smile | :)

Known Problems:

  1. CSimpleTreeCtrl does not hide the base implementation of CTreeCtrl so users may use the base class functionality as well. However this may cause some problems. Whenever a CSimpleTreeCtrl::TreeCtrlItem item is added to the tree, it's pointer is added as item data in CTreeCtrl. If you use HTREEITEM to add new items and set item data by yourself, CSimpleTreeCtrl will assume that user data is a CSimpleTreeCtrl::TreeCtrlItem or derived pointer. If there is no user data, there should not be any problem

  2. CSimpleTreeCtrl::postCreate() will be called when tree control receives an OnCreate() message. I noticed that in some cases (according to how you use the tree) that control does not get this message. But I did not check it in detail. If you've got any problem send me the details.


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

Ozgur Aydin Yuksel
Web Developer
Romania Romania
Aeronautical Engineer working on Software Development,

Graduated from Istanbul Technical University, Istanbul, Turkey at 1996.

Mainly works in simulation projects for defense, vehicle simulations, computer graphics, cockpit design and all related tools. Deals with MFC and other GUI tools whenever necessary.

You may also be interested in...

Comments and Discussions

Generaltree control color text only Pin
ashu_om18-Aug-09 23:43
memberashu_om18-Aug-09 23:43 
Generaltree control color text only Pin
ashu_om18-Aug-09 23:43
memberashu_om18-Aug-09 23:43 
Jokenice Pin
Member 45624273-Aug-09 17:43
memberMember 45624273-Aug-09 17:43 
Generalmerhaba Pin
NCECE11-Oct-05 4:04
sussNCECE11-Oct-05 4:04 
GeneralHide the selection (the blue color) Pin
d00_ape28-Feb-05 1:52
memberd00_ape28-Feb-05 1:52 
GeneralTree Experts Pin
pank0071-Jan-05 6:08
memberpank0071-Jan-05 6:08 
GeneralLet's go further Pin
Repsej20-Oct-04 19:54
memberRepsej20-Oct-04 19:54 
GeneralRe: Let's go further Pin
Repsej20-Oct-04 20:21
memberRepsej20-Oct-04 20:21 
GeneralRe: Let's go further Pin
Ozgur Aydin Yuksel21-Oct-04 9:39
memberOzgur Aydin Yuksel21-Oct-04 9:39 
Good question... However I think it is a little bit out of the scope of "Simple Tree Ctrl". What you're suggestion goes to write wrapper classes around the MFC objects which actually I tought at the beginning. But again, it was totally out of my mind when I wrote this control. And I did not write any other similar classes for other MFC controls.

So if you like to give a try I also recommend to check sigslot template headers which is available on internet somwhere ( I'm sure a quick search might give nice results). Checking some about QT (if not done yet) also will give really nice ideas I am sure...

"Computers emit waves that affect the brain, causing ... Superstitious beliefs ("the computer hates me") and magical thinking ("the program only works when I wear my hat backward"). "
from "How to Think Like a Computer Scientist"
GeneralGetting unique info from CTreeCtrl Pin
PsychoPsam16-Jun-04 18:51
memberPsychoPsam16-Jun-04 18:51 
GeneralCTreeCtrl::SetCheck Pin
edgar32514-Jun-04 7:36
memberedgar32514-Jun-04 7:36 
GeneralRe: CTreeCtrl::SetCheck Pin
Anonymous30-Jul-04 22:08
sussAnonymous30-Jul-04 22:08 
GeneralRe: CTreeCtrl::SetCheck Pin
Anonymous30-Jul-04 22:10
sussAnonymous30-Jul-04 22:10 
GeneralRe: CTreeCtrl::SetCheck Pin
m4pp120-Dec-04 9:17
memberm4pp120-Dec-04 9:17 
GeneralRe: CTreeCtrl::SetCheck Pin
ETA3-Jun-07 23:29
memberETA3-Jun-07 23:29 
Questionwith images ? Pin
cross bones style12-May-04 2:45
membercross bones style12-May-04 2:45 
Generalcoo Pin
haoshenghan19-Apr-04 14:49
memberhaoshenghan19-Apr-04 14:49 
QuestionHow to call a Dialog member function Pin
C_A14-Apr-04 18:33
memberC_A14-Apr-04 18:33 
GeneralCTreeCtrl under _UNICODE setting Pin
charcoalc3-Mar-04 15:54
membercharcoalc3-Mar-04 15:54 
GeneralCTreectrl::Setcheck() does not work! Pin
Anonymous21-Jul-03 18:27
sussAnonymous21-Jul-03 18:27 
GeneralRe: CTreectrl::Setcheck() does not work! Pin
Anonymous30-Jul-03 5:29
sussAnonymous30-Jul-03 5:29 
GeneralRe: CTreectrl::Setcheck() does not work! Pin
anonymous17-Feb-04 0:52
sussanonymous17-Feb-04 0:52 
GeneralThanks Pin
richard sancenot20-Aug-04 0:37
memberrichard sancenot20-Aug-04 0:37 
GeneralRe: CTreectrl::Setcheck() does not work! Pin
Anonymous22-Sep-04 8:18
sussAnonymous22-Sep-04 8:18 
GeneralRe: CTreectrl::Setcheck() does not work! Pin
llllskywalker17-Jan-05 12:32
sussllllskywalker17-Jan-05 12:32 
GeneralTreeCtrlItem not deleted Pin
Roongrit29-Apr-03 21:26
memberRoongrit29-Apr-03 21:26 
GeneralRe: TreeCtrlItem not deleted Pin
Ozgur Aydin Yuksel29-Apr-03 21:46
memberOzgur Aydin Yuksel29-Apr-03 21:46 
Generaldynamical stuff in the tree Pin
T1TAN22-Apr-03 8:12
memberT1TAN22-Apr-03 8:12 
GeneralRe: dynamical stuff in the tree Pin
Ozgur Aydin Yuksel29-Apr-03 21:53
memberOzgur Aydin Yuksel29-Apr-03 21:53 
GeneralRe: dynamical stuff in the tree Pin
T1TAN3-May-03 6:12
memberT1TAN3-May-03 6:12 
GeneralTreeItem Coordinates Pin
naveensg14-Apr-03 1:49
membernaveensg14-Apr-03 1:49 
Generalthanks! this is really what i need.. Pin
kaisze14-Feb-03 16:55
memberkaisze14-Feb-03 16:55 
QuestionHow changes the color of the node on-line Pin
asdmusic31-Oct-02 14:43
memberasdmusic31-Oct-02 14:43 
AnswerRe: How changes the color of the node on-line Pin
Ozgur Aydin Yuksel1-Nov-02 0:35
memberOzgur Aydin Yuksel1-Nov-02 0:35 
GeneralRe: How changes the color of the node on-line Pin
asdmusic3-Nov-02 14:03
memberasdmusic3-Nov-02 14:03 
GeneralRe: How changes the color of the node on-line Pin
Ozgur Aydin Yuksel3-Nov-02 14:54
memberOzgur Aydin Yuksel3-Nov-02 14:54 
Answerm_treeCtrl.RedrawWindow(); Pin
OlofAs5-Oct-04 23:42
memberOlofAs5-Oct-04 23:42 
GeneralCSimpleTreeCtrl::lClick () Pin
Ozgur Aydin Yuksel31-Oct-02 1:03
memberOzgur Aydin Yuksel31-Oct-02 1:03 
GeneralAbout drawing Pin
Anonymous30-Oct-02 23:00
sussAnonymous30-Oct-02 23:00 
GeneralRe: About drawing Pin
Ozgur Aydin Yuksel31-Oct-02 0:56
memberOzgur Aydin Yuksel31-Oct-02 0:56 
GeneralHelp on CTreeView Pin
AnthonyWinters30-Oct-02 14:49
memberAnthonyWinters30-Oct-02 14:49 
GeneralRe: Help on CTreeView Pin
Ozgur Aydin Yuksel30-Oct-02 15:47
memberOzgur Aydin Yuksel30-Oct-02 15:47 
GeneralExcellent... Pin
Nitron30-Oct-02 3:24
memberNitron30-Oct-02 3:24 
GeneralReally simple & good Pin
Volki29-Oct-02 15:16
memberVolki29-Oct-02 15:16 
GeneralRe: Really simple & good Pin
Fat dog30-Oct-02 0:50
memberFat dog30-Oct-02 0:50 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160530.1 | Last Updated 29 Oct 2002
Article Copyright 2002 by Ozgur Aydin Yuksel
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid