TreeList : Multi column tree control

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. It looks like this:

structure.gif (1528 bytes)

  • 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

....

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.

Download Sample/Code(TreeList.zip 60K).



Comments

  • Very interesting

    Posted by leessming on 02/12/2010 11:52pm

    I need the treelist with multicolumn for displaying data of soundings.but I can not load down it.

    Reply
  • fd9f2!f3d8h==c

    Posted by vipygs on 06/03/2009 12:33am

    fd9f2!f3d8h==

    Reply
  • Why don't these sample projects ever compile?

    Posted by SteveS on 06/05/2006 02:08pm

    To get this to compile in VS.NET 2003 do the following: Remove the function CLantivUIDoc* CTreeListView::GetDocument() Remove these lines from TLView.rc #include "l.heb\afxres.rc" // Standard components #include "l.heb\afxprint.rc" // printing/print preview resources

    Reply
  • The correct download url

    Posted by Air2 on 01/25/2005 02:51am

    The url is wrong, I think it should be: http://www.codeguru.com/code/legacy/treeview/Treelist.zip

    Reply
  • Fix for DeleteItem()

    Posted by cjoslin on 12/13/2004 06:08pm

    Hi, Couple of memory leaks for CNewTreeListCtrlDeleteItem(HTREEITEM); Here is the fix: BOOL CNewTreeListCtrl::DeleteItem( HTREEITEM hItem ) { CTLItem *pItem = (CTLItem *)CTreeCtrl::GetItemData(hItem); if(!pItem) return FALSE; delete pItem; // Added (CJ) pItem = NULL; // Added (CJ) m_nItems--; return CTreeCtrl::DeleteItem(hItem); }

    Reply
  • I can't download the demo/code

    Posted by ayana on 05/29/2004 04:57pm

    can you post, I can't download the code

    • few files are missing in the zip

      Posted by ejlee on 08/06/2004 01:33am

      rc file asks a directory l.heb ... Please help.

      Reply
    • Working link

      Posted by maniac91 on 08/04/2004 05:45pm

      Try this then : http://www.codeguru.com/code/legacy/treeview/Treelist.zip

      Reply
    Reply
  • Some problems with XP style

    Posted by Legacy on 09/23/2003 12:00am

    Originally posted by: Ivan Ivanov

    Header delimiters disappear with XP style.
    Any ideas how to solve this problem?

    Reply
  • Dialog app, bitmap IDB_HEADER, m_tree.m_cImageList

    Posted by Legacy on 09/06/2003 12:00am

    Originally posted by: Michal

    Hi folks,

    Could I get some working dialog based example? I tried to use this control, but I have som problems with image that uses IDB_HEADER bitmap.

    In TLFrame.cpp this code doesn't work correctly.

    m_tree.m_cImageList.Create(IDB_HEADER, 16, 10, 0);
    m_tree.m_wndHeader.SetImageList(&m_tree.m_cImageList);

    Create return 0, and I dont know why.

    Please help.
    Thanks,

    Michal

    Reply
  • Help - Compile error while trying to use in my project

    Posted by Legacy on 07/13/2003 12:00am

    Originally posted by: David Jorge

    when i try to compile my project with the TreeList control i get the folowing errors:

    Compiling...
    NewHeaderCtrl.cpp
    E:\OpenCASCADE5.0\ShoeHeel\TreeList\NewHeaderCtrl.cpp(119) : error C2039: 'DrawText' : is not a member of 'CDC'
    e:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(636) : see declaration of 'CDC'
    E:\OpenCASCADE5.0\ShoeHeel\TreeList\NewHeaderCtrl.cpp(166) : error C2039: 'DrawText' : is not a member of 'CDC'
    e:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(636) : see declaration of 'CDC'
    NewTreeListCtrl.cpp
    E:\OpenCASCADE5.0\ShoeHeel\TreeList\NewTreeListCtrl.cpp(276) : error C2039: 'DrawText' : is not a member of 'CDC'
    e:\program files\microsoft visual studio\vc98\mfc\include\afxwin.h(636) : see declaration of 'CDC'

    Can anyone helpme ?
    what's the problem with DrawText?

    Reply
  • retrieve text from treelist''''''HELP

    Posted by Legacy on 06/18/2003 12:00am

    Originally posted by: boomer

    how would I retrieve the data contained in the treelistview from a vb6 app?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Email is the most common communication vehicle used by organizations of all shapes and sizes. Among the billions of email messages sent every day are sensitive information, critical requests, and other essential business data. IT staff bear the burden of ensuring the confidentiality, integrity, and availability of the information contained within the communication. This white paper explores the email security landscape, an assessment of the threats organizations face,  and the building blocks of an effective …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds