
Navigation
Given a tree or an item in a tree, there are a limited
number of possible ways to go from there. You can get the topmost item, the next
sibling, the previous sibling, the first child or the parent.
HTREEITEM htiChild =
GetChildItem( htiCurrent );
HTREEITEM htiSibling = GetNextSiblingItem( htiCurrent );
HTREEITEM htiSibling = GetPrevSiblingItem( htiCurrent );
HTREEITEM htiParent = GetParentItem( htiCurrent );
HTREEITEM htiCurrent = GetSelectedItem();
HTREEITEM htiRoot = GetRootItem();
Delayed Population
Let’s take the situation where we want to wait until the
user clicks on an item’s ‘+’ sign to populate the children of the selected item.
The parent to the control is set up to receive TVN_ITEMEXPANDING notification
messages. Let’s further stipulate that the population exercise only happens
once. Once populated, don’t do it again.
OK, first thing – what triggers the tree to put a ‘+’ on
an item? It will only happen if the item has a child. Since we don’t actually go
out and look for an item’s children until it is expanded, we have ourselves a
problem. If the item has no children then it doesn’t have a ‘+’. The work around
is to append a blank item to each item when it is added. Then, at expansion
time, we look at the first child of the item. If the child is blank then this
item has never been expanded before and needs to be populated. Delete the blank
child item and populate the item’s children.
Image List
One of the really nice things about the tree control is the
ability to associate a small bitmap with each item in the tree. The tree control
has a single bitmap which is subdivided into a series of bitmaps. Each item in
the control gets the zero relative index of two separate sub bitmaps, one for
when the item is selected and one for, oddly enough, when the item isn’t
selected. Fancy that.
The image list itself is encapsulated within a CImageList.
Getting Check State
If the tree control has the checkbox attribute then you can
ask the tree control for the check state.
BOOL bChecked =
ctrlTree.GetCheck(hItem );

|