Click here to Skip to main content
Licence 
First Posted 1 Dec 1999
Views 277,223
Bookmarked 72 times

A TreeList Control

By | 1 Dec 1999 | Article
A tree control / list control hybrid
  • Download demo project - 60 Kb
  • Download source files - 21 Kb
  • 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:
      ....
      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

    About the Author

    David Lantsman

    Web Developer

    Israel Israel

    Member

    School Timetable Scheduling Software

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    Generalwhy i translate this ctrl to OCX(MFC activex)projecthave some error PinmemberLGEKS0:41 2 Jun '11  
    GeneralMy vote of 4 PinmemberMANISH RASTOGI19:18 2 Mar '11  
    GeneralNot getting notifications under VS2010 PinmemberSaintNeil12:09 9 Jun '10  
    Generaldemo is not working buddy !!!!!!!!!!!!! PinmemberS.V.RAJKUMAR23:15 17 Sep '09  
    GeneralEditlable Pinmembermarkman1010:03 15 Jul '09  
    GeneralBug in sort algorithm PinmemberFreak3023:17 26 Jan '09  
    Questionhow to insert a root element after or before an existing one PinmemberMember 21726414:24 22 Sep '08  
    GeneralUseless code Pinmemberkilt7:23 4 May '08  
    GeneralRe: Useless code PinmemberMichael O'Rourke5:02 1 Apr '09  
    Since when?
     
    There is CListCtrl and CTreeCtrl, but no combination of the two that I've ever seen in MFC.
    GeneralI found a new bug and fixed it. Pinmembernewmetic20:55 10 Oct '07  
    GeneralBug fix for CNewTreeListCtrl::OnPaint() Pinmemberflippydeflippydebop20:53 23 Mar '07  
    GeneralRe: Bug fix for CNewTreeListCtrl::OnPaint() [modified] PinmemberKindy Chen23:14 23 Sep '07  
    GeneralMultiple Row Selection Pinmemberflippydeflippydebop20:49 23 Mar '07  
    Questionclass field type: "LANTIVTREELISTCTRL" ??? Pinmembergerms13137:02 12 Mar '07  
    GeneralSunken frame Pinmemberdgodfrey6:43 21 Feb '07  
    QuestionGood!! ... in C#? Pinmemberd00_ape20:33 11 Jul '06  
    GeneralCatching messages (e.g. TVN_GETDISPINFO) PinmemberBenGEAR2:04 16 Mar '06  
    GeneralCopyright issues to use this control Pinmemberpadmawar10:41 6 Feb '06  
    GeneralWhy a Assert failure when I use the CHeaderCtrl instead of CNewHeaderCtrl Pinmemberzhou_wz17:16 25 Aug '05  
    GeneralRe: Why a Assert failure when I use the CHeaderCtrl instead of CNewHeaderCtrl PinmemberDavid Cader8:32 7 Nov '05  
    GeneralEditLabel Pinmembermungflesh23:10 3 Aug '05  
    QuestionHow to get .NET assembly of this? PinmemberBusyMan 20052:19 30 Apr '05  
    GeneralBug in CNewTreeListCtrl::SetItemText() Pinmemberrimoraj21:41 20 Jan '05  
    Generalsplitter pane PinmemberDMW4:30 27 May '04  
    GeneralSome details if not bugs Pinmembermssg23:03 23 Dec '03  

    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.

    Permalink | Advertise | Privacy | Mobile
    Web02 | 2.5.120604.1 | Last Updated 2 Dec 1999
    Article Copyright 1999 by David Lantsman
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid