Click here to Skip to main content
15,881,898 members
Articles / Desktop Programming / WTL

TreeCtrl - A WTL Tree Control with Windows Vista Style Item Selection

Rate me:
Please Sign up or sign in to vote.
4.98/5 (24 votes)
9 Apr 2006CPOL 353.6K   3.8K   75   21
A WTL tree control that supports Windows Vista style selection and multiple selection

Image 1

Introduction

After quite a few requests, I thought I'd post this tree control that uses a similar selection drawing style to the list control I posted here: ListCtrl - A WTL list control with Windows Vista style item selection. For an added bonus, I've also included support for multiple-selection.

How to Use CTreeCtrl

Simply use the tree control as you would the normal CTreeViewCtrl, however there are a couple of functions required to activate and retrieve multiple selections:

C++
void ShowThemed( 
    BOOL bShowThemed = TRUE
)

Description

Draw item selection themed.

Parameters

  • bShowThemed - TRUE = Draw themed selection; FALSE = Classic mode
C++
void SetMultipleSelect( 
       BOOL bMultipleSelect
)

Description

Turns on/off multiple selection.

Parameters

  • bMultipleSelect - TRUE = Turn on multiple select
C++
void GetSelectedItems( 
    CSimpleArray < HTREEITEM >& aSelectedItems
)

Description

Retrieves a list of selected tree items.

Parameters

  • aSelectedItems - Reference to an array of HTREEITEM

Finally

Any comments or suggestions are welcome.

History

  • 16th March, 2006: 1.0
    • First release
  • 20th March, 2006: 1.1
    • Small bug fix for VS2005
  • 5th April, 2006: 1.2
    • Corrected problems with multiple select and checkboxes - many thanks to Phil C.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
Alan has been developing applications for a very long time (~10 years), but he's not bitter about this at all. My main area of expertise is C++. He lives in Sweden with his beautiful wife, daughter and son and enjoys climbing mountains and kayaking in his spare time (which isn't much).

Comments and Discussions

 
Questionwhy i can not download the sample code? Pin
asdf112212-Nov-12 22:47
asdf112212-Nov-12 22:47 
QuestionSetWindowTheme Pin
Koh Chia31-Aug-12 0:11
professionalKoh Chia31-Aug-12 0:11 
Question不能用啊??? Pin
zhangjingfei21-Jan-10 0:52
zhangjingfei21-Jan-10 0:52 
AnswerRe: 不能用啊??? Pin
Koh Chia31-Aug-12 0:01
professionalKoh Chia31-Aug-12 0:01 
GeneralOnLButtonUp is not called correctly Pin
JinXu24-Apr-08 5:15
JinXu24-Apr-08 5:15 
Generalfatal error RC1015: cannot open include file 'atlres.h'. Pin
aditya raut18-Sep-07 15:58
aditya raut18-Sep-07 15:58 
GeneralRe: fatal error RC1015: cannot open include file 'atlres.h'. Pin
asight17-Oct-13 23:43
asight17-Oct-13 23:43 
QuestionVC6 Pin
toddma26-Jun-07 9:54
toddma26-Jun-07 9:54 
AnswerRe: VC6 Pin
toddma26-Jun-07 9:56
toddma26-Jun-07 9:56 
GeneralMake the tree items editable Pin
shaohao5-Aug-06 5:52
shaohao5-Aug-06 5:52 
GeneralFixes for keeping selection when clicking expand and collapse and setting background color Pin
J Chris Davies19-Apr-06 7:44
J Chris Davies19-Apr-06 7:44 
QuestionIt's very nice , but have MFC version ? Pin
XiaoQing Li17-Apr-06 23:36
XiaoQing Li17-Apr-06 23:36 
GeneralDisplay problem Pin
Black.Stone13-Apr-06 20:35
Black.Stone13-Apr-06 20:35 
GeneralLooks great, but ... Pin
_oti3-Apr-06 14:34
_oti3-Apr-06 14:34 
Start the program then click on the first item. The selection disappears.

Get the selection back on the first item, then Shift+Click further down. The section incorporates everything from the second clicked item to the bottom of the tree.

Select the first three items using Ctrl+Click, then Shift+Click the somewhere further down. The first item is not de-selected.

Use the arrow keys to move up and down the tree. At the top and bottom, the selection disappears if you hold the key down. The same happens if you use the left and right arrow keys to expand or collapse a node.

Put checkboxes into the tree using the dialog editor. Pressing the space bar will only flip one checkbox out of the selection, and the selection goes away.

I think that the custom painting is great, and I want to use it, but the multiple selection functionality needs a lot of work. I did a bit to address a few of these, but it's going to take more, I think.

Here's the diff:
135,144c135,136
< 		HTREEITEM hItem = GetNextVisibleItem( GetRootItem() );
< 		
< 		SetRedraw( FALSE );
< 		
< 		// clear selection upto the first item
< 		for ( ; hItem != NULL && hItem != hItemFrom && hItem != hItemTo; hItem = GetNextVisibleItem( hItem ) )
< 			SetItemState( hItem, 0, TVIS_SELECTED );
< 
< 		// is item visible?
< 		if ( hItem != NULL )
---
> 		HTREEITEM hItem = GetRootItem();
> 		for ( ; hItem != NULL; hItem = GetNextVisibleItem( hItem ) )
146,148c138,142
< 			SelectItem( hItemTo );
< 
< 			if ( hItem == hItemTo )
---
> 			if ( hItem == hItemFrom )
> 			{
> 				break;
> 			}
> 			else if ( hItem == hItemTo )
152a146
> 				break;
153,155c148,149
< 
< 			// go through remaining visible items
< 			for ( BOOL bSelectItem = TRUE; hItem != NULL; hItem = GetNextVisibleItem( hItem ) )
---
> 		}
> 		if ( hItem == NULL )
157,161c151
< 				SetItemState( hItem, bSelectItem ? TVIS_SELECTED : 0, TVIS_SELECTED );
< 
< 				// do we need to start removing items from selection?
< 				if ( hItem == hItemTo ) 
< 					bSelectItem = FALSE;
---
> 			return FALSE;
163a153,162
> 		
> 		SetRedraw( FALSE );
> 		
> 		// clear whole selection
> 		ResetSelected( GetRootItem() );
> 
> 		// is item visible?
> 		for ( hItem = hItemFrom; hItem != hItemTo; hItem = GetNextVisibleItem( hItem ) )
> 		{
> 			SetItemState( hItem, TVIS_SELECTED, TVIS_SELECTED );
164a164
> 		SetItemState( hItemTo, TVIS_SELECTED, TVIS_SELECTED );
242c243,245
< 		if ( ( nChar == VK_UP || nChar == VK_DOWN ) && GetKeyState( VK_SHIFT ) & 0x8000 )
---
> 		if ( m_bMultipleSelect && ( nChar == VK_UP || nChar == VK_DOWN ) )
> 		{
> 			if ( GetKeyState( VK_SHIFT ) & 0x8000 )
265c268,290
< 		else if ( nChar >= VK_SPACE )
---
> 			else	// no shift key
> 			{
> 				HTREEITEM hSel = GetSelectedItem();
> 				m_hFirstSelected = NULL;
> 				ResetSelected( GetRootItem() );
> 				SelectItem( hSel );
> 			}
> 		}
> 		else if ( nChar == VK_SPACE && (GetStyle() & TVS_CHECKBOXES) == TVS_CHECKBOXES )
> 		{
> 			HTREEITEM hSel = GetSelectedItem();
> 			BOOL bChecked = GetCheckState(hSel);
> 			CSimpleArray<HTREEITEM> aSel;
> 			GetSelectedItems(aSel);
> 			for (int ctr = 0; ctr < aSel.GetSize(); ctr++)
> 			{
> 				if (aSel[ctr] != hSel)
> 				{
> 					SetCheckState(aSel[ctr], !bChecked);
> 				}
> 			}
> 		}
> 		else if ( isalnum(nChar) )
300c325,326
< 			if ( HitTest( point, NULL ) != NULL )
---
> 			HTREEITEM hNewItem = HitTest( point, NULL );
> 			if ( hNewItem != NULL )
302a328
> 				UINT nItemSelState = GetItemState(hNewItem, TVIS_SELECTED) & TVIS_SELECTED;
303c330,331
< 				m_hFirstSelected = NULL;
---
> 				SetItemState(hNewItem, nItemSelState, TVIS_SELECTED);
> 				m_hFirstSelected = hNewItem;

This still doesn't address what happens to the selection if you click and drag another item, or if you set or clear the checkbox if another item is selected.

Phil.

--
All things considered, you can't really consider all things ...
GeneralRe: Looks great, but ... Pin
AlanW9-Apr-06 22:43
AlanW9-Apr-06 22:43 
Generalgreat work but I find a bug! Pin
lihezhao17-Mar-06 5:11
lihezhao17-Mar-06 5:11 
GeneralRe: great work but I find a bug! Pin
AlanW17-Mar-06 5:32
AlanW17-Mar-06 5:32 
GeneralATLApp Error Pin
Sreekanth Muralidharan17-Mar-06 2:48
Sreekanth Muralidharan17-Mar-06 2:48 
GeneralRe: ATLApp Error Pin
Urquan Master17-Mar-06 6:45
Urquan Master17-Mar-06 6:45 
GeneralRe: ATLApp Error Pin
Urquan Master17-Mar-06 7:13
Urquan Master17-Mar-06 7:13 
GeneralRe: ATLApp Error Pin
AlanW17-Mar-06 9:26
AlanW17-Mar-06 9:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.