Click here to Skip to main content
15,867,488 members
Articles / Desktop Programming / MFC

A TreeList Control

Rate me:
Please Sign up or sign in to vote.
3.86/5 (18 votes)
1 Dec 19993 min read 400.1K   11.3K   80   89
A tree control / list control hybrid

Sample Image - treelist.jpg

The Treelist control is a combination Tree/List control derived from CTreeCtrl.

Here's the description of the classes that are used:

  • CTLFrame - derived from CWnd, this class is the frame class for the treelist control. It is used to include the header control, the tree itself, and the horizontal scroll bar.
  • CNewHeaderCtrl - derived from CHeaderCtrl. Used as the header in the TreeList.
  • CNewTreeListCtrl - derived from CTreeCtrl, used as the main tree in the TreeList.
  • CTLItem - represents each item inside the tree.
  • SSortType - structure that's used to indicate whether the sort of the tree is in an ascending order or descending, and which column is being sorted.

How to Insert TreeList into Your Project?

  1. Insert the following files into your project:
    • TLFrame.cpp, TLFrame.h
    • NewTreeListCtrl.cpp, NewTreeListCtrl.h
    • NewHeaderCtrl.cpp, NewHeaderCtrl.h
  2. Include the file "TLFrame.h" in the app file (where the InitInstance function is) and insert the following line in the InitInstance function:
    C++
    ....
    CTLFrame::RegisterClass();
    ....
  3. Layout a user-defined control inside the dialog into which the control is supposed to be inserted. In the class field type: "LANTIVTREELISTCTRL"
  4. Include "TLFrame.h" in the dialog's header file, and add a member variable: CTLFrame m_wndMyTreeList;
  5. In your OnInitDialog() or OnCreate() functions, subclass the control:
    C++
    ....
    m_wndMyTreeList.SubclassDlgItem(IDC_TREE_LIST, this);
    
    // IDC_TREE_LIST is the ID of the user-defined control you 
    // inserted into the dialog
    ....

That's it !

Using the TreeListCtrl

The use of the control is simple. It's a tree, so treat it as one (HTREEITEM, etc.). In addition, there are functions like InsertColumn, SetItemText, SetItemColor, SetItemBold, GetItemText.

Implementation

Here's my approach to implementing the TreeList. First of all, I had to create a frame window, that would include the 3 objects: header, tree, horz scroll bar. That's what CTLFrame is for. In addition, this CWnd derived class helps during the scroll: the header is clipped after it's repositioned, so there's a feeling of scrolling.

The class CNewHeaderCtrl was created only to put the 3D triangles in it. I decided to include sorting since it's needed in 9/10 cases, and it's a shame every programmer needs to insert it by himself. Finally, CNewTreeListCtrl is the more complicated part. Every item inside the tree, has a DWORD data associated with it, that stores a pointer to a CTLItem class. CTLItem stores the information about each item- its columns' strings, whether the item is bold, the item's color, and the item data that the user wishes to associate with the item. All the functions that deal with the items, like InsertItem, DeleteItem, SetItemText, etc. were overridden in order to use the CTLItem technique.

I had some difficulties with the scrolling part, since there are many different conditions for the scroll bars: vertical scroll is shown and then the horz becomes shorter, it's hidden so the horz should become larger again; what happens if the users changed some column's size... and problems like that. I believe I handle each of these cases so I think there won't be any problems with this part.

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

Comments and Discussions

 
Generalvariable m_RTL Pin
8-Jun-01 4:00
suss8-Jun-01 4:00 
GeneralRe: variable m_RTL Pin
17-Jul-01 6:47
suss17-Jul-01 6:47 
GeneralSelChange Pin
Ron van Gelderen2-Jan-01 3:37
Ron van Gelderen2-Jan-01 3:37 
GeneralRe: SelChange Pin
Eric Linu14-Mar-07 16:20
Eric Linu14-Mar-07 16:20 
GeneralOnContextMenu not reflecting correct parameters... Pin
10-Dec-00 10:18
suss10-Dec-00 10:18 
QuestionDoes this work with VC++ 5? Pin
19-Nov-00 16:33
suss19-Nov-00 16:33 
QuestionGreat control, how about a grid? Pin
15-Nov-00 5:23
suss15-Nov-00 5:23 
GeneralBug in Numerical Sorting Pin
fantozzi8-Sep-00 10:52
fantozzi8-Sep-00 10:52 
GeneralBug in Numerical Sorting Pin
fantozzi8-Sep-00 10:51
fantozzi8-Sep-00 10:51 
GeneralIntegrate in Taborder of Dialog Pin
Sailer Alexander18-Aug-00 1:48
sussSailer Alexander18-Aug-00 1:48 
GeneralRe: Integrate in Taborder of Dialog Pin
Alexander Sailer20-Aug-00 20:44
Alexander Sailer20-Aug-00 20:44 
GeneralUsing State Images Pin
Thomas Matelich1-Aug-00 8:24
Thomas Matelich1-Aug-00 8:24 
GeneralRe: Using State Images Pin
Thomas Matelich1-Aug-00 10:22
Thomas Matelich1-Aug-00 10:22 
GeneralHorizontal scroll bar repaint problem Pin
Paul D'hertoghe29-May-00 4:32
professionalPaul D'hertoghe29-May-00 4:32 

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.