Expanding a branch

The treeview has built in support for expanding expanding the outline one
level at a time. If you want to completely expand a branch, the code below
will help. It uses recursion to expand all items. The last line, that is,
the call to EnsureVisible() is useful when this code is hooked up to a
user interface. After expanding the outline branch, the previously selected
item may have scrolled off and calling EnsureVisible() brings it back.

// ExpandBranch – Expands a branch completely
// hti          – Handle of the tree item to expand
void CTreeCtrlX::ExpandBranch( HTREEITEM hti )
        if( ItemHasChildren( hti ) ){
                Expand( hti, TVE_EXPAND );
                hti = GetChildItem( hti );
                        ExpandBranch( hti );
                }while( (hti = GetNextSiblingItem( hti )) != NULL );
        EnsureVisible( GetSelectedItem() );

More by Author

Must Read