Click here to Skip to main content
13,350,708 members (69,456 online)
Click here to Skip to main content
Add your own
alternative version


36 bookmarked
Posted 5 Jun 2001

Tree control templates

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 8:05
membercen_jin_long7-Aug-08 8:05 
JokeNice work. thank you! Pin
Andy Byron22-Jan-06 17:53
memberAndy Byron22-Jan-06 17:53 
GeneralGreat Code ! Pin
Warren Stevens30-Jul-05 10:34
memberWarren Stevens30-Jul-05 10:34 
I have to agree with the comment from "taratata1" below - worked right out of the box, I only had to add two or three lines of code. Nice!

This article is certainly a prime example of how code should be written for easy re-use. Well done.

GeneralGreate Article Pin
taratata117-Mar-04 12:07
membertaratata117-Mar-04 12:07 
GeneralStress Test problem Pin
alan934-Nov-02 7:17
memberalan934-Nov-02 7:17 
GeneralThank you Pin
Aaron Hudon15-Aug-02 12:46
memberAaron Hudon15-Aug-02 12:46 
Questionhow to change content of nodes Pin
Venkata Ramana9-Aug-01 19:54
memberVenkata Ramana9-Aug-01 19:54 
GeneralJust what I needed Pin
Geert Delmeiren11-Jun-01 23:22
memberGeert Delmeiren11-Jun-01 23: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.

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