Click here to Skip to main content
15,799,183 members
Articles / Desktop Programming / MFC

Simple Tree Control

Rate me:
Please Sign up or sign in to vote.
4.50/5 (23 votes)
28 Oct 20023 min read 247.3K   5.5K   32   45
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 a 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 readable 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:

C++
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:

C++
// 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".

C++
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:

C++
class BaseCtrlItem : public CSimpleTreeCtrl::TreeCtrlItem
{
public:
     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
     } ;

protected:
     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 are any useful 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 :-)

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, its 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.

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


Comments and Discussions

 
AnswerRe: How changes the color of the node on-line Pin
Ozgur Aydin Yuksel1-Nov-02 1:35
Ozgur Aydin Yuksel1-Nov-02 1:35 
GeneralRe: How changes the color of the node on-line Pin
asdmusic3-Nov-02 15:03
asdmusic3-Nov-02 15:03 
GeneralRe: How changes the color of the node on-line Pin
Ozgur Aydin Yuksel3-Nov-02 15:54
Ozgur Aydin Yuksel3-Nov-02 15:54 
Answerm_treeCtrl.RedrawWindow(); Pin
OlofAs6-Oct-04 0:42
OlofAs6-Oct-04 0:42 
GeneralCSimpleTreeCtrl::lClick () Pin
Ozgur Aydin Yuksel31-Oct-02 2:03
Ozgur Aydin Yuksel31-Oct-02 2:03 
GeneralAbout drawing Pin
Anonymous31-Oct-02 0:00
Anonymous31-Oct-02 0:00 
GeneralRe: About drawing Pin
Ozgur Aydin Yuksel31-Oct-02 1:56
Ozgur Aydin Yuksel31-Oct-02 1:56 
GeneralHelp on CTreeView Pin
Anthony Winters30-Oct-02 15:49
Anthony Winters30-Oct-02 15:49 
This does not pertain directly to your article but I thought you would know the answer to my problem. I am trying to alter the style of the CTreeCtrl contained in a CTreeView project, but I can't seem to find a way to do it. I want to make the +/- buttons next to the items visible and make the tree lines visible, for example. Can you help me find the solution to this?

Thank You!


Thanks!
GeneralRe: Help on CTreeView Pin
Ozgur Aydin Yuksel30-Oct-02 16:47
Ozgur Aydin Yuksel30-Oct-02 16:47 
GeneralExcellent... Pin
Nitron30-Oct-02 4:24
Nitron30-Oct-02 4:24 
GeneralReally simple & good Pin
Volki29-Oct-02 16:16
Volki29-Oct-02 16:16 
GeneralRe: Really simple & good Pin
dangdinhtien30-Oct-02 1:50
dangdinhtien30-Oct-02 1: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.