A multi-select tree control class | CodeGuru

A multi-select tree control class

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Jul 25, 1999
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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.

Advertisement

Downloads

Download demo project – 34 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.