Click here to Skip to main content
15,882,113 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 401K   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

 
Generalsplitter pane Pin
DMW27-May-04 4:30
DMW27-May-04 4:30 
GeneralSome details if not bugs Pin
mssg23-Dec-03 23:03
mssg23-Dec-03 23:03 
QuestionIDB_HEADER??? Pin
Kartan11-Sep-03 18:55
Kartan11-Sep-03 18:55 
AnswerRe: IDB_HEADER??? Pin
Louka Dlagnekov21-Jan-04 18:04
Louka Dlagnekov21-Jan-04 18:04 
GeneralRe: IDB_HEADER??? Pin
Louka Dlagnekov21-Jan-04 18:12
Louka Dlagnekov21-Jan-04 18:12 
Generaldemo doesn't compile as-is Pin
Kartan11-Sep-03 15:54
Kartan11-Sep-03 15:54 
QuestionResond to TVN_ITEMEXPANDING? Pin
Andrew Sinagra9-Sep-03 14:18
Andrew Sinagra9-Sep-03 14:18 
AnswerRe: Resond to TVN_ITEMEXPANDING? Pin
ipashche10-Jul-04 15:28
ipashche10-Jul-04 15:28 
GeneralRe: Resond to TVN_ITEMEXPANDING? Pin
Nelno21-Dec-04 5:34
Nelno21-Dec-04 5:34 
GeneralBug in CTLFrame::OnNotify: Pin
Nick Nougat14-Jul-03 9:26
Nick Nougat14-Jul-03 9:26 
GeneralRe: Bug in CTLFrame::OnNotify: Pin
Eric Linu14-Mar-07 15:34
Eric Linu14-Mar-07 15:34 
GeneralBug in DeleteItem: Memory Leaks Pin
Nick Nougat12-Jul-03 8:42
Nick Nougat12-Jul-03 8:42 
GeneralHorizontal Scroll bug Pin
Parampreet Sidhu8-Jul-03 0:01
Parampreet Sidhu8-Jul-03 0:01 
GeneralFound a solution Pin
Parampreet Sidhu8-Jul-03 0:04
Parampreet Sidhu8-Jul-03 0:04 
GeneralBug in SetItemTex() Pin
paqui13-May-03 23:10
paqui13-May-03 23:10 
QuestionHow to display a vertical line on tree while sizing column headers Pin
Paul Vickery3-Oct-02 1:46
professionalPaul Vickery3-Oct-02 1:46 
AnswerRe: How to display a vertical line on tree while sizing column headers Pin
Oscar Londono13-Dec-05 11:55
Oscar Londono13-Dec-05 11:55 
GeneralSort bug Pin
Doug3162-Jul-02 11:13
Doug3162-Jul-02 11:13 
GeneralRe: Sort bug Pin
Gregoire14-Jul-05 9:03
Gregoire14-Jul-05 9:03 
QuestionHow to turn into OCX Pin
21-Jun-02 5:25
suss21-Jun-02 5:25 
GeneralSmall bug fix Pin
30-May-02 3:45
suss30-May-02 3:45 
GeneralRe: Small bug fix Pin
dours27-Jan-03 3:16
dours27-Jan-03 3:16 
Generalmultiple row selection Pin
ckdrew26-May-02 18:12
ckdrew26-May-02 18:12 
GeneralRe: multiple row selection Pin
flippydeflippydebop23-Mar-07 21:04
flippydeflippydebop23-Mar-07 21:04 
GeneralDeleteHeaders() Pin
dours24-May-02 0:47
dours24-May-02 0:47 

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.