Click here to Skip to main content
15,891,513 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 354.7K   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

 
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 
Looks good and I like the approach -

Couple of fixes here -
Override SetBkColor to allow non-white background. Maybe a better way to handle background color (and text color) is by getting and setting the treeviews colors through TVM_GETBKCOLOR etc.

Fixed clicking on expand buttons so it doesn't affect the current selection.

Also just FYI - I got this working on 7.1 by copying out the relavent classes from ATL 7.5 (CDoubleBuffer, CMemoryDC and CLogFont)

<br />
  COLORREF SetBkColor(COLORREF bk)<br />
  {<br />
    m_rgbBackground = bk;<br />
    return CWindowImpl< CTreeCtrl, CTreeViewCtrl >::SetBkColor(bk);<br />
  }<br />
<br />
  void OnLButtonDown( UINT nFlags, CPoint point )<br />
  {<br />
    if ( m_bMultipleSelect )<br />
    {<br />
      // reset selected item if click on a single item<br />
      UINT flags = 0;<br />
      HTREEITEM hNewItem = HitTest( point, &flags );<br />
      if (flags & TVHT_ONITEMBUTTON)<br />
      {<br />
        SetMsgHandled( FALSE );<br />
        return;<br />
      }<br />
<br />
      m_bCtrlKey = ( ( GetKeyState( VK_CONTROL ) & 0x8000 ) != 0 );<br />
      m_bShiftKey = ( ( GetKeyState( VK_SHIFT ) & 0x8000 ) != 0 );<br />
      <br />
      if ( m_bCtrlKey || m_bShiftKey )<br />
        return;<br />
      <br />
      if ( hNewItem != NULL )<br />
      {<br />
        UINT nNewSelectState = ( GetItemState( hNewItem, TVIS_SELECTED ) & TVIS_SELECTED );<br />
        ResetSelected( GetRootItem() );<br />
        SetItemState( hNewItem, nNewSelectState, TVIS_SELECTED );<br />
        m_hFirstSelected = hNewItem;<br />
      }<br />
    }<br />
    <br />
    SetMsgHandled( FALSE );<br />
  }<br />
<br />
  void OnLButtonUp( UINT nFlags, CPoint point )<br />
  {<br />
    SetMsgHandled( FALSE );<br />
    <br />
    if ( !m_bMultipleSelect )<br />
      return;<br />
<br />
    UINT flags = 0;<br />
    HTREEITEM hNewItem = HitTest( point, &flags );<br />
    if (flags & TVHT_ONITEMBUTTON)<br />
      return;<br />
<br />
    HTREEITEM hOldItem = GetSelectedItem();<br />
    <br />
    if ( m_bCtrlKey )<br />
    {<br />
      if ( hNewItem != NULL )<br />
      {<br />
        UINT nNewSelectState = GetItemState( hNewItem, TVIS_SELECTED ) & TVIS_SELECTED ? 0 : TVIS_SELECTED;<br />
        UINT nOldSelectState = hOldItem == NULL ? 0 : GetItemState( hOldItem, TVIS_SELECTED );<br />
        <br />
        // select new item (to get focus)<br />
        SelectItem( hNewItem );<br />
        <br />
        // set highlight state for new item<br />
        if ( GetItemState( hNewItem, TVIS_SELECTED ) != nNewSelectState )<br />
          SetItemState( hNewItem, nNewSelectState, TVIS_SELECTED );<br />
        <br />
        // restore state of old selected item<br />
        if ( hOldItem != NULL && hOldItem != hNewItem && GetItemState( hOldItem, TVIS_SELECTED ) != nOldSelectState )<br />
          SetItemState( hOldItem, nOldSelectState, TVIS_SELECTED );<br />
        <br />
        m_hFirstSelected = NULL;<br />
      }<br />
    }<br />
    else if ( m_bShiftKey )<br />
    {<br />
      // initialise the reference item if this is the first shift selection<br />
      if ( m_hFirstSelected == NULL )<br />
        m_hFirstSelected = GetSelectedItem();<br />
        <br />
      // select new item (to get focus)<br />
      if ( hOldItem == hNewItem )<br />
        SelectItem( NULL ); // to prevent edit<br />
<br />
      if ( m_hFirstSelected != NULL )<br />
        SelectItems( m_hFirstSelected, hNewItem );<br />
    }<br />
    <br />
    m_bCtrlKey = FALSE;<br />
    m_bShiftKey = FALSE;<br />
  }<br />




Chris Davies

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.