Link to home
Start Free TrialLog in
Avatar of yossikally
yossikally

asked on

Wanted: A function to traverse a populated tree control

Any of you folks has got such a function?

It is a CTreeCtrl that is a member of my own 'CMyTree' class.

It looks like a simple cookbook thing - traversing the tree, perhaps for printing out its contents.
Avatar of migel
migel

Avatar of yossikally

ASKER

I know this article, and it's not what I meant.  It shows you how to extract the path to one specific node, and I want to traverse the whole tree.

Maybe the article can be adapted to do what I want, and eventually I may do it, but I wanted to save me some work.
ASKER CERTIFIED SOLUTION
Avatar of migel
migel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zzynx
Maybe this can help you:

HTREEITEM CTreeCtrlEx::GetNextItemEx(HTREEITEM hItemPara)
{
  HTREEITEM hItem = GetChildItem(hItemPara);
  if (hItem) return hItem;

  hItem = GetNextSiblingItem(hItemPara);
  if (hItem) return hItem;

  HTREEITEM hParent = hItemPara;
  while (!hItem)
  {
    hParent = GetParentItem(hParent);
    if (!hParent) return NULL; // We are at the root (that has no parent)
    hItem = GetNextSiblingItem(hParent);
  }
  return hItem;
}

And maybe you find these ones also interesting:

///////////////////////////////////////////////////////////////////////////////
// Helpers to list out selected items. (Use similar to GetFirstVisibleItem(),
// GetNextVisibleItem() and GetPrevVisibleItem()!)

HTREEITEM CTreeCtrlEx::GetFirstSelectedItem()
{
     for ( HTREEITEM hItem = GetRootItem(); hItem!=NULL; hItem = GetNextVisibleItem( hItem ) )
          if ( GetItemState( hItem, TVIS_SELECTED ) & TVIS_SELECTED )
               return hItem;

     return NULL;
}

HTREEITEM CTreeCtrlEx::GetNextSelectedItem( HTREEITEM hItem )
{
     for ( hItem = GetNextVisibleItem( hItem ); hItem!=NULL; hItem = GetNextVisibleItem( hItem ) )
          if ( GetItemState( hItem, TVIS_SELECTED ) & TVIS_SELECTED )
               return hItem;

     return NULL;
}

HTREEITEM CTreeCtrlEx::GetPrevSelectedItem( HTREEITEM hItem )
{
     for ( hItem = GetPrevVisibleItem( hItem ); hItem!=NULL; hItem = GetPrevVisibleItem( hItem ) )
          if ( GetItemState( hItem, TVIS_SELECTED ) & TVIS_SELECTED )
               return hItem;

     return NULL;
}

Success.