Click here to Skip to main content
Click here to Skip to main content

Tree control templates

, 11 Jun 2001
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

Introduction

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

mc_tcTree.Initialize(true);

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

mc_tcTree.Initialize(false);

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

mc_tcTree.SetRedraw(FALSE);
    
mc_tcTree.ExpandItem();
    
 mc_tcTree.SetRedraw(TRUE);

Sample Code 4: Collapsing All Items

/////////////////////////
// Items collapse sample

mc_tcTree.SetRedraw(FALSE);
    
mc_tcTree.ExpandItem(TVI_ROOT, TVE_COLLAPSE);
    
mc_tcTree.SetRedraw(TRUE);

Sample Code 5: Status Preservation

/////////////////////////
// Status preservation sample

mc_tcTree.SaveStatus();

PopulateTree();

mc_tcTree.RestoreStatus();

That's all folks!

License

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.

Comments and Discussions

 
GeneralGood sample! Pinmembercen_jin_long7-Aug-08 7:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.8.140721.1 | Last Updated 12 Jun 2001
Article Copyright 2001 by Francisco José Sen del Prado
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid