Get next item in outline




Imagine that the outline is completely expanded. Pressing the down key
will take you down on line to the next item. Now imagine that instead of
expanding the outline and pressing the down key, you want to get to the
items in the same sequence programmatically. Although the CTreeView control
provides a GetNextItem() function, it does not provide direct support for
our needs. Listed below is an overloaded version of GetNextItem() taking
a single argument. Before we actually look at the function lets first look
at the declaration of the function in the CTreeView derived class.

 

        HTREEITEM GetNextItem( HTREEITEM hItem, UINT nCode ){
                return CTreeCtrl::GetNextItem( hItem, nCode );
        }
        HTREEITEM GetNextItem( HTREEITEM hItem);

It is important that we override the original function taking two arguments
otherwise our new version of the function will hide it. Now here is the
listing of the overloaded GetNextItem() function.

 

// GetNextItem  - Get next item as if outline was completely expanded
// Returns      - The item immediately below the reference item
// hItem        - The reference item
HTREEITEM CTreeCtrlX::GetNextItem( HTREEITEM hItem )
{
        HTREEITEM       hti;

        if( ItemHasChildren( hItem ) )
                return GetChildItem( hItem );           // return first child
        else{
                // return next sibling item
                // Go up the tree to find a parent's sibling if needed.
                while( (hti = GetNextSiblingItem( hItem )) == NULL ){
                        if( (hItem = GetParentItem( hItem ) ) == NULL )
                                return NULL;
                }
        }
        return hti;
}

More by Author

Must Read