A multi-select tree control class

Sample Image

Environment: VC6 SP2

There have already been several implementations of a multi-select tree control posted to CodeGuru, but none of them have provided the functionality and/or behaviour I required.

The versions I have seen take the approach of handling the selection around the mouse-down, mouse-move and mouse-up messages, with variables holding state in-between. CMultiTree takes the approach of handling the whole multi-selection within the mouse-down handler. This is a closer approach to the underlying Windows treeview control.

CMultiTree attempts to mirror the selection behaviour of a CListCtrl (listview control) class. In fact the code was developed by putting a CListCtrl next to a CTreeCtrl and trying the combinations of mouse clicks/drags, and key presses. CMultiTree thus has full rubber-band selection support.

The sample application implements the CTreeCtrl in a CTreeView class, using a method described on a CodeGuru site. Unfortunately I can't find the original article to credit the author [hopefully the editor can do this on my behalf].

Usage

Multiple-selection in CMultiTree should be done in more or less the same way as in a CListCtrl, i.e. using SetItemState, GetItemState. I have taken the liberty of defining a TVIS_FOCUSED (the equivalent of LVIS_FOCUSED of the CListCtrl).

SetItemState / GetItemState:

These two methods have been over-scoped in the CMultiTree class to replace the underlying CTreeCtrl versions. Because they are NOT virtual, they must be called in the scope of the CMultiTree class (or derived class), otherwise they will not work, e.g.:


    /* if m_tree is a CMultiTree */
    CTreeCtrl *pTree = &m_tree;
    UINT nState = pTree->GetItemState(hItem, TVIS_FOCUSED|TVIS_SELECTED);

this will NOT call the CMultiTree version because of the non-virtual GetItemState.

Examine the header file for other methods, they are pretty self-explanatory.

Comments: mailto:comments@tinuviel.demon.co.uk
Info: http://www.tinuviel.demon.co.uk/source_code/sc_multitree.htm
Copyright (c) 1999 Richard Hazlewood.

Downloads

Download demo project - 34 Kb



Comments

  • Not working

    Posted by Liviu Dorobantu on 04/10/2014 08:54am

    Hello! I integrated your code for multiple selection on a CTreeCtrl taken from here:http://www.codeguru.com/cpp/controls/treeview/misc-advanced/article.php/c629/A-multiselect-tree-control-class.htm. The problem is that multi selection with Ctrl does not work (the program runs). I'm using Visual Studio 2005. If I run your program as downloaded from the link above, it works, but not if I take MltiTree.h and MltiTree.cpp and put them in my program. Do you have any ideea? Thanks!

    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 …

  • Live Event Date: October 29, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this eSeminar, Gene Kim will discuss these survey findings and will share woeful tales of artifact management gone wrong! Gene will also share examples of how …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds