Using Tree-View Controls

This section contains implementation details and example code for working with tree-view controls.

In this section

Topic Description
How to Create a Tree-View Control
To create a tree-view control, use the CreateWindowEx function, specifying the WC_TREEVIEW value for the window class. The tree-view window class is registered in the application's address space when the common control DLL is loaded. To ensure that the DLL is loaded, use the InitCommonControls function.
How to Initialize the Image List
Every item in a tree-view control can have two images associated with it. An item displays one image when it is selected and the other when it is not. To include images with tree-view items, first use the Image Lists functions to create an image list and add images to it. Then associate the image list with the tree-view control by using the TVM_SETIMAGELIST message.
How to Add Tree-View Items
You add an item to a tree-view control by sending the TVM_INSERTITEM message to the control. The message includes the address of a TVINSERTSTRUCT structure, specifying the parent item, the item after which the new item is inserted, and a TVITEM structure that defines the attributes of the item. The attributes include the item's label, its selected and nonselected images, and a 32-bit application-defined value.
How to Drag a Tree-View Item
This topic demonstrates code for handling dragging and dropping of tree-view items. The sample code consists of three functions. The first function begins the drag operation, the second function drags the image, and the third function ends the drag operation.
How to Work With State Image Indexes
There is often confusion about how to set and retrieve the state image index in a tree-view control. The following examples demonstrate the proper method for setting and retrieving the state image index. The examples assume that there are only two state image indexes in the tree-view control, unchecked and checked. If your application contains more than two, these functions will need to be modified to handle that case.
How to Use Tree-View Infotips
When you apply the TVS_INFOTIP style to a tree-view control, it generates TVN_GETINFOTIP notifications when the cursor is over an item in the tree view. By responding to this notification, you can set the text that appears in the infotip.