Click here to Skip to main content
12,296,023 members (61,037 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked

Tree control templates

, 11 Jun 2001 CPOL
Rate this:
Please Sign up or sign in to vote.
Three tree control templates to easily operate with CTreeCtrl derived objects

Sample Image - TreeControlTemplates.gif


These templates allow CTreeCtrl derived classes programmers to add three basic functionalities to their classes:

  • Iteration of all the items in the control
  • Persistence of the status of the control (when you reload it, it tries to return to its previous state)
  • Expand / Collapse all the items in the tree

Sample Code 1: Deep Iteration

// Deep iteration sample


HTREEITEM hItem= mc_tcTree.GetNext();

while (hItem != NULL)
    TRACE(mc_tcTree.GetItemText(hItem) + "\n");

    hItem= mc_tcTree.GetNext();

Sample Code 2: Width Iteration

// Width iteration sample


HTREEITEM hItem= mc_tcTree.GetNext();

while (hItem != NULL)
    TRACE(mc_tcTree.GetItemText(hItem) + "\n");

    hItem= mc_tcTree.GetNext();

Sample Code 3: Expanding All Items

// Items expand sample


Sample Code 4: Collapsing All Items

// Items collapse sample

mc_tcTree.ExpandItem(TVI_ROOT, TVE_COLLAPSE);

Sample Code 5: Status Preservation

// Status preservation sample




That's all folks!


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


About the Author

Francisco José Sen del Prado
Software Developer (Senior)
Spain Spain
I started programming in Basic language when I was 14 years old. The second programming language I learned was Z80 assembler. I've been programming with C++ for the last 20 years. I attended the 2012 C++ Now event in Aspen and it was the most exciting experience I've lived in my professional life. I met a lot of brilliant C++ minds and I have very good memories from that trip.

You may also be interested in...

Comments and Discussions

GeneralGood sample! Pin
cen_jin_long7-Aug-08 7:05
membercen_jin_long7-Aug-08 7:05 
JokeNice work. thank you! Pin
Andy Byron22-Jan-06 16:53
memberAndy Byron22-Jan-06 16:53 
GeneralGreat Code ! Pin
Warren Stevens30-Jul-05 9:34
memberWarren Stevens30-Jul-05 9:34 
GeneralGreate Article Pin
taratata117-Mar-04 11:07
membertaratata117-Mar-04 11:07 
GeneralStress Test problem Pin
alan934-Nov-02 6:17
memberalan934-Nov-02 6:17 
I am using this very nicely done control to display a 4mb file with 67,000 records.

It Loads, displays, and works perfect.


When exiting the dialog there is about a 3 to 4 second delay before the dialog disappears as if the the control( with all nodes expanded ) needs time to unload before allowing the dialog to exit.


Any thoughts on this?
GeneralThank you Pin
Aaron Hudon15-Aug-02 11:46
memberAaron Hudon15-Aug-02 11:46 
Questionhow to change content of nodes Pin
Venkata Ramana9-Aug-01 18:54
memberVenkata Ramana9-Aug-01 18:54 
GeneralJust what I needed Pin
Geert Delmeiren11-Jun-01 22:22
memberGeert Delmeiren11-Jun-01 22:22 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 12 Jun 2001
Article Copyright 2001 by Francisco José Sen del Prado
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid