Tree View Control Macros and Member Functions

Table 3-4 on the following page describes the macros and member functions supported by tree view controls. CTreeCtrl is the MFC class that supports these controls. To use tree view controls in your application, you must link with the COMCTL32.LIB library. If you are working in C, you must include the COMMCTRL.H header file; if you write your application in MFC, you must include the AFXCMN.H file in your STDAFX.H file (if you are using STDAFX.H).

For each message supported by tree view controls, the system provides a macro that an application can call. I used the macros rather than the messages in my samples because I find the macros more readable and because they make it easier to move between C and MFC. As you can see in Table 3-4, the name of the member function is nearly always the same as the end of the macro name (that is, the DeleteItem member function corresponds to the TreeView_DeleteItem macro).

Macro Member Function Description
TreeView_Create Create Creates a tree view control.
TreeView_CreateDragImage CreateDragImage Creates a drag image for the specified item.
TreeView_DeleteAllItems DeleteAllItems Deletes all items in a tree view window.
TreeView_DeleteItem DeleteItem Deletes a specified item from a tree view window. This message has two macros, TreeView_DeleteItem and TreeView_DeleteAllItems, which you can use to delete one item or all items from the control. If the item label is being edited when this message is sent, the edit operation is canceled, and the parent window receives a TVN_ENDLABELEDIT notification. Then a TVN_DELETEITEM notification is sent to the parent window. If hItem is TVI_ROOT, all items are deleted from the control.
TreeView_EditLabel EditLabel Begins in-place editing of an item's text label. The text is replaced by a single-line edit window containing the original text in a selected and focused state. A TVN_BEGINLABELEDIT notification is sent to the parent window of the tree view control. You can safely subclass the edit control, but do not destroy it. When the user completes or cancels the editing, the handle to the edit window becomes invalid.

Table 3-4. Tree view control macros and member functions. (continued)

Macro Member Function Description
TreeView_EnsureVisible EnsureVisible Ensures that an item is visible, and expands the parent item or scrolls the tree view window if necessary. If the message expands the parent item, TVN_ITEMEXPANDING and TVN_ITEMEXPANDED notifications are sent to the parent window.
TreeView_Expand Expand Expands or collapses the list of child items associated with a parent item. This message sends TVN_ITEMEXPANDING and TVN_ITEMEXPANDED notifications to the parent window.
TreeView_GetChild GetChildItem Retrieves the child of a specified tree view item.
TreeView_GetCount GetCount Returns the number of items in a tree view window.
TreeView_GetDrop- Hilight GetDrop- HilightItem Retrieves the target of a drag-and-drop operation.
TreeView_GetEditControl GetEditControl Retrieves the handle of the edit control being used for in-place editing of an item's text label.
TreeView_GetFirstVisible GetFirstVisibleItem Retrieves the first visible item of a tree view control.
TreeView_GetImageList GetImageList Retrieves the handle of the image list associated with a tree view window.
TreeView_GetIndent GetIndent Retrieves the amount, in pixels, that child items are indented relative to their parent item.
TreeView_GetItem GetItem Retrieves information about an item depending on the mask member in the TV_ITEM structure passed in.
TreeView_GetItemRect GetItemRect Retrieves the bounding rectangle and visibility state of an item.
TreeView_GetNextItem GetNextItem Retrieves the next item that matches a specified relationship.
TreeView_GetNextSibling GetNext- SiblingItem Retrieves the next sibling of an item.
TreeView_GetNextVisible GetNext- VisibleItem Retrieves the next visible item following the specified tree view item.
TreeView_GetParent GetParentItem Retrieves the parent of an item.
TreeView_GetPrevSibling GetPrev- SiblingItem Retrieves the previous sibling of an item.
TreeView_GetPrevVisible GetPrev- VisibleItem Retrieves the first visible item preceding the specified tree view item.
TreeView_GetRoot GetRootItem Retrieves the root of an item.
TreeView_GetSelection GetSelectedItem Retrieves the currently selected item.
TreeView_GetVisibleCount GetVisibleCount Retrieves the count of items that will fit into the tree view window.
TreeView_HitTest HitTest Retrieves the tree view item that occupies the specified point. This message is generally used for drag-and-drop operations.
TreeView_InsertItem InsertItem Inserts a new item in a tree view window. If the item is being edited, the edit operation is canceled, and the parent window receives a TVN_ENDLABELEDIT notification.
TreeView_Select Select Selects, scrolls into view, or redraws an item.
TreeView_SelectDropTarget SelectDropTarget Selects an item as the drop target.
TreeView_SelectItem SelectItem Selects an item.
TreeView_SetImageList SetImageList Sets the image list for a tree view window and redraws it.
TreeView_SetIndent SetIndent Sets the amount of indention for a child item.
TreeView_SetItem SetItem Sets the attributes of an item.
TreeView_SortChildren SortChildren Sorts the child items of a given parent item.
TreeView_SortChildrenCB SortChildrenCB Sorts items using an application-defined comparison function.