Tree View Controls

A tree view control displays a hierarchical list of labeled items. Optionally, each item can have a bitmap (or a pair of bitmaps) associated with it. You've seen this type of hierarchy in File Manager (displaying directory information) and in Microsoft Mail (displaying mail folders). The top item in the hierarchy, which has no parent, is referred to as the root item. An item below the root item in the hierarchy is called a child item of the root. An item that has child items is called a parent item. Child items, when displayed, are indented below their parent item. (In fact, the entire hierarchy of items is expandable and collapsible.) If you specify the TVS_HASLINES style, you can connect the items with lines. Figure 3-9 shows a tree view window that lists houses for sale in various cities in the beautiful Pacific Northwest.

Figure 3-9.

Anatomy of a tree view control.

The first time I saw a tree hierarchy like this, I wanted to implement one in my application. Like most of you, I figured out how to do it on my own, but it was complex, and it was a pain. I really wished that a tree hierarchy had been built into the system. With Windows 95, it's finally happened.

In a tree view control, you can add a pair of bitmaps to the left of an item's label, displaying one bitmap when the item is selected and the other when the item is not selected. For example, when you select a folder in Windows Explorer, the image changes from a closed folder to an open one, as shown in Figure 3-10 on the following page.

Figure 3-10.

Selected and unselected items in a tree view control.