Click here to Skip to main content
Click here to Skip to main content
Go to top

A TreeList Control

, 1 Dec 1999
Rate this:
Please Sign up or sign in to vote.
A tree control / list control hybrid
  • Download demo project - 60 Kb
  • Download source files - 21 Kb
  • Sample Image - treelist.jpg

    <!-- Article Starts --> 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:
      ....
      CTLFrame::RegisterClass();
      ....
      
    3. Layout a user-defined control inside the dialog into which the control is supposed to be intserted. 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:
      ....
      m_wndMyTreeList.SubclassDlgItem(IDC_TREE_LIST, this);
      
      // IDC_TREE_LIST is the ID of the user-defined control you 
      // inserted into the dialog</p>
      ....
      

    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

    Share

    About the Author


    Comments and Discussions

     
    Generalwhy i translate this ctrl to OCX(MFC activex)projecthave some error PinmemberLGEKS2-Jun-11 0:41 
    GeneralMy vote of 4 PinmemberMANISH RASTOGI2-Mar-11 19:18 
    GeneralNot getting notifications under VS2010 PinmemberSaintNeil9-Jun-10 12:09 
    Generaldemo is not working buddy !!!!!!!!!!!!! PinmemberS.V.RAJKUMAR17-Sep-09 23:15 
    GeneralEditlable Pinmembermarkman10115-Jul-09 0:03 
    GeneralBug in sort algorithm PinmemberFreak3026-Jan-09 23:17 
    Questionhow to insert a root element after or before an existing one PinmemberMember 217264122-Sep-08 4:24 
    GeneralUseless code Pinmemberkilt4-May-08 7:23 
    GeneralRe: Useless code PinmemberMichael O'Rourke1-Apr-09 5:02 
    GeneralI found a new bug and fixed it. Pinmembernewmetic10-Oct-07 20:55 
    GeneralBug fix for CNewTreeListCtrl::OnPaint() Pinmemberflippydeflippydebop23-Mar-07 20:53 
    GeneralRe: Bug fix for CNewTreeListCtrl::OnPaint() [modified] PinmemberKindy Chen23-Sep-07 23:14 
    GeneralMultiple Row Selection Pinmemberflippydeflippydebop23-Mar-07 20:49 
    Questionclass field type: "LANTIVTREELISTCTRL" ??? Pinmembergerms131312-Mar-07 7:02 
    GeneralSunken frame Pinmemberdgodfrey21-Feb-07 6:43 
    QuestionGood!! ... in C#? Pinmemberd00_ape11-Jul-06 20:33 
    GeneralCatching messages (e.g. TVN_GETDISPINFO) PinmemberBenGEAR16-Mar-06 2:04 
    GeneralCopyright issues to use this control Pinmemberpadmawar6-Feb-06 10:41 
    GeneralWhy a Assert failure when I use the CHeaderCtrl instead of CNewHeaderCtrl Pinmemberzhou_wz25-Aug-05 17:16 
    GeneralRe: Why a Assert failure when I use the CHeaderCtrl instead of CNewHeaderCtrl PinmemberDavid Cader7-Nov-05 8:32 
    GeneralEditLabel Pinmembermungflesh3-Aug-05 23:10 
    QuestionHow to get .NET assembly of this? PinmemberBusyMan 200530-Apr-05 2:19 
    GeneralBug in CNewTreeListCtrl::SetItemText() Pinmemberrimoraj20-Jan-05 21:41 
    Generalsplitter pane PinmemberDMW27-May-04 4:30 
    GeneralSome details if not bugs Pinmembermssg23-Dec-03 23:03 
    QuestionIDB_HEADER??? PinmemberKartan11-Sep-03 18:55 
    AnswerRe: IDB_HEADER??? PinmemberLouka Dlagnekov21-Jan-04 18:04 
    GeneralRe: IDB_HEADER??? PinmemberLouka Dlagnekov21-Jan-04 18:12 
    Generaldemo doesn't compile as-is PinmemberKartan11-Sep-03 15:54 
    QuestionResond to TVN_ITEMEXPANDING? PinmemberAndrew Sinagra9-Sep-03 14:18 
    AnswerRe: Resond to TVN_ITEMEXPANDING? Pinsussirenap10-Jul-04 15:28 
    GeneralRe: Resond to TVN_ITEMEXPANDING? PinmemberNelno21-Dec-04 5:34 
    GeneralBug in CTLFrame::OnNotify: PinsussNick Nougat14-Jul-03 9:26 
    GeneralRe: Bug in CTLFrame::OnNotify: PinmemberEric Linu14-Mar-07 15:34 
    GeneralBug in DeleteItem: Memory Leaks PinsussNick Nougat12-Jul-03 8:42 
    GeneralHorizontal Scroll bug PinmemberParampreet Sidhu8-Jul-03 0:01 
    GeneralFound a solution PinmemberParampreet Sidhu8-Jul-03 0:04 
    GeneralBug in SetItemTex() Pinmemberpaqui13-May-03 23:10 
    QuestionHow to display a vertical line on tree while sizing column headers PinmemberPaul S. Vickery3-Oct-02 1:46 
    AnswerRe: How to display a vertical line on tree while sizing column headers PinmemberOscar Londoño13-Dec-05 11:55 
    GeneralSort bug PinmemberDoug3162-Jul-02 11:13 
    GeneralRe: Sort bug PinmemberGregoire14-Jul-05 9:03 
    QuestionHow to turn into OCX PinmemberJerry Gadd21-Jun-02 5:25 
    GeneralSmall bug fix PinmemberSilent Bob30-May-02 3:45 
    GeneralRe: Small bug fix Pinmemberdours27-Jan-03 3:16 
    Generalmultiple row selection Pinmemberckdrew26-May-02 18:12 
    GeneralRe: multiple row selection Pinmemberflippydeflippydebop23-Mar-07 21:04 
    GeneralDeleteHeaders() Pinmemberdours24-May-02 0:47 
    GeneralLast Item Pinmemberdours24-May-02 0:28 
    GeneralCTLFrame::OnNotify Pinmemberdours23-May-02 5:35 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
    Web02 | 2.8.140916.1 | Last Updated 2 Dec 1999
    Article Copyright 1999 by David Lantsman
    Everything else Copyright © CodeProject, 1999-2014
    Terms of Service
    Layout: fixed | fluid