viksoe.dk

TreeListView control

TreeListView control


This article was submitted .


This WTL TreeListView control is a combination of a TreeView control with ListView capabilities. It looks like a tree, but has column headers and displays tree sub-items.

The control is partly implemented from a MFC CTreeListView control by Gerolf Kühnel available at the Codeproject site. As a bonus I have added sub-item images, text coloring, font formatting and header drag'n'drop support.

To use it, place a regular TreeView control on your dialog. This control is not actually used; it is just a container. It's a bit of a waste of resources, but allows you to set the control properties in the resource editor.
Now add a member variable to your dialog implementation file...

CTreeListViewCtrl m_tree
In the OnInitDialog() event handler, add the following line:
  LRESULT OnInitDialog(UINT /*uMsg*/, 
                       WPARAM /*wParam*/, 
                       LPARAM /*lParam*/, 
                       BOOL& /*bHandled*/)
  {
    ...
    m_tree.SubclassWindow(GetDlgItem(IDC_TREE1));
    ...
  }

The control is built from a standard Windows TreeView control and a Header control. Both of these child controls are subclassed so additional functionality can be added.
There isn't a lot of API methods on the actual CTreeListViewCtrl class. Instead, you are allowed access to the two child controls behind the scenes.
By using the calls...

    CTreeViewCtrl ctrlTree = m_tree.GetTreeControl();
    CHeaderCtrl ctrlHeader = m_tree.GetHeaderControl();
you have the full power of the standard tree and header controls.
From there you can add your column headers...
    HDITEM col = { 0 };
    col.mask = HDI_FORMAT | HDI_TEXT | HDI_WIDTH;
    col.fmt = HDF_LEFT;
    col.cxy = 250;
    col.pszText = _T("header #1");
    ctrlHeader.InsertItem(0, &col);
    col.cxy = 150;
    col.pszText = _T("header #2");
    ctrlHeader.InsertItem(1, &col);
and tree items...
    HTREEITEM hRoot = ctrlTree.InsertItem(
         _T("Root item"), 
         0,0, TVI_ROOT, TVI_ROOT);
and finally change the sub-item properties...
    m_tree.SetSubItemText(hRoot, 
         1, _T("Sub item"));
Sub-items can also be added/changed using the SetSubItem() method. It uses its own TLVITEM item-structure, which works the same way as the ones in the original tree and list controls.

You still need to set the images and indent using the embedded tree control to modify data in column 0, while the caption of items in column 0 (and all other columns) should be changed using the SetSubItem() method.

You can change font and colours for each item.. Use the CLR_NONE to specify the default colour.

It is wise to add the TVS_DISABLEDRAGDROP style to the tree control for best result.

Source Code Dependencies

Internet Explorer 5 or better
Microsoft Visual C++ 6.0
Microsoft WTL 3.1 Library

Download Files

DownloadSource Code (10 Kb)

To the top