A Tree View Control is a window that displays a hierarchical list of items, such as the headings in a document, the entries in an index, or the files and directories on a disk. Each item consists of a label and an optional bitmapped image, and each item can have a list of subitems associated with it. By clicking an item, the user can expand and collapse the associated list of subitems. It is represented by CTreeCtrl class.
Sr.No.
Name & Description
1
Create
Creates a tree view control and attaches it to a CTreeCtrl object.
2
CreateDragImage
Creates a dragging bitmap for the specified tree view item.
3
CreateEx
Creates a tree control with the specified Windows extended styles and attaches it to a CTreeCtrl object
4
DeleteAllItems
Deletes all items in a tree view control.
5
DeleteItem
Deletes a new item in a tree view control.
6
EditLabel
Edits a specified tree view item in-place.
7
EndEditLabelNow
Cancels the edit operation on the label of a tree-view item in the current tree-view control.
8
EnsureVisible
Ensures that a tree view item is visible in its tree view control.
9
Expand
Expands, or collapses, the child items of the specified tree view item.
10
GetBkColor
Retrieves the current background color of the control.
11
GetCheck
Retrieves the check state of a tree control item.
12
GetChildItem
Retrieves the child of a specified tree view item.
13
GetCount
Retrieves the number of tree items associated with a tree view control.
14
GetDropHilightItem
Retrieves the target of a drag-and-drop operation.
15
GetEditControl
Retrieves the handle of the edit control used to edit the specified tree view item.
16
GetExtendedStyle
Retrieves the extended styles that the current treeview control is using.
17
GetFirstVisibleItem
Retrieves the first visible item of the specified tree view item.
18
GetImageList
Retrieves the handle of the image list associated with a tree view control.
19
GetIndent
Retrieves the offset (in pixels) of a tree view item from its parent.
20
GetInsertMarkColor
Retrieves the color used to draw the insertion mark for the tree view.
21
GetItem
Retrieves the attributes of a specified tree view item.
22
GetItemData
Returns the 32-bit application-specific value associated with an item.
23
GetItemExpandedImageIndex
Retrieves the index of the image to display when the specified item of the current tree-view control is in the expanded state.
24
GetItemHeight
Retrieves the current height of the tree view items.
25
GetItemImage
Retrieves the images associated with an item.
26
GetItemPartRect
Retrieves the bounding rectangle for a specified part of a specified item in the current tree-view control.
27
GetItemRect
Retrieves the bounding rectangle of a tree view item.
28
GetItemState
Returns the state of an item.
29
GetItemStateEx
Retrieves the extended state of the specified item in the current tree-view control.
30
GetItemText
Returns the text of an item.
31
GetLastVisibleItem
Retrieves the last expanded item in the current treeview control.
32
GetLineColor
Retrieves the current line color for the tree view control.
33
GetNextItem
Retrieves the next tree view item that matches a specified relationship.
34
GetNextSiblingItem
Retrieves the next sibling of the specified tree view item.
35
GetNextVisibleItem
Retrieves the next visible item of the specified tree view item.
36
GetParentItem
Retrieves the parent of the specified tree view item.
37
GetPrevSiblingItem
Retrieves the previous sibling of the specified tree view item.
38
GetPrevVisibleItem
Retrieves the previous visible item of the specified tree view item.
39
GetRootItem
Retrieves the root of the specified tree view item.
40
GetScrollTime
Retrieves the maximum scroll time for the tree view control.
41
GetSelectedCount
Retrieves the number of selected items in the current tree-view control.
42
GetSelectedItem
Retrieves the currently selected tree view item.
43
GetTextColor
Retrieves the current text color of the control.
44
GetToolTips
Retrieves the handle to the child ToolTip control used by a tree view control.
45
GetVisibleCount
Retrieves the number of visible tree items associated with a tree view control.
46
HitTest
Returns the current position of the cursor related to the CTreeCtrl object.
47
InsertItem
Inserts a new item in a tree view control.
48
ItemHasChildren
Returns nonzero if the specified item has child items
49
MapAccIdToItem
Maps the specified accessibility identifier to the handle to a tree-view item in the current tree-view control.
50
MapItemToAccID
Maps the specified handle to a tree-view item in the current tree-view control to an accessibility identifier.
51
Select
Selects, scrolls into view, or redraws a specified tree view item.
52
SelectDropTarget
Redraws the tree item as the target of a drag-and-drop operation.
53
SelectItem
Selects a specified tree view item.
54
SelectSetFirstVisible
Selects a specified tree view item as the first visible item.
55
SetAutoscrollInfo
Sets the autoscroll rate of the current tree-view control.
56
SetBkColor
Sets the background color of the control.
57
SetCheck
Sets the check state of a tree control item.
58
SetExtendedStyle
Sets the extended styles for the current tree-view control
59
SetImageList
Sets the handle of the image list associated with a tree view control.
60
SetIndent
Sets the offset (in pixels) of a tree view item from its parent.
61
SetInsertMark
Sets the insertion mark in a tree view control.
62
SetInsertMarkColor
Sets the color used to draw the insertion mark for the tree view.
63
SetItem
Sets the attributes of a specified tree view item.
64
SetItemData
Sets the 32-bit application-specific value associated with an item.
65
SetItemExpandedImageIndex
Sets the index of the image to display when the specified item of the current tree-view control is in the expanded state.
66
SetItemHeight
Sets the height of the tree view items.
67
SetItemImage
Associates images with an item.
68
SetItemState
Sets the state of an item.
69
SetItemStateEx
Sets the extended state of the specified item in the current tree-view control.
70
SetItemText
Sets the text of an item.
71
SetLineColor
Sets the current line color for the tree view control.
72
SetScrollTime
Sets the maximum scroll time for the tree view control.
73
SetTextColor
Sets the text color of the control.
74
SetToolTips
Sets a tree view control's child ToolTip control.
75
ShowInfoTip
Displays the infotip for the specified item in the current tree-view control.
76
SortChildren
Sorts the children of a given parent item.
77
SortChildrenCB
Sorts the children of a given parent item using an application-defined sort function.
Let us look into a simple example by creating a new MFC dialog based project.
Step 1 − Once the project is created you will see the TODO line, which is the Caption of Text Control. Remove the Caption and set its ID to IDC_STATIC_TXT.
Step 2 − Add a value variable m_strTree for the Static Text control.
Step 3 − From the Controls toolbox, drag the Tree Control.
Step 4 − On the dialog box, click the Tree Control to select it. On the Properties window, set the Has Buttons, the Has Lines, the Lines At Root, the Client Edge and the Modal Frame properties to True.
Step 5 − Add a control variable m_treeCtrl for Tee Control.
Step 6 − Here is initialization of tree control in OnInitDialog()
BOOL CMFCTreeControlDlg::OnInitDialog() {
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
HTREEITEM hItem, hCar;
hItem = m_treeCtrl.InsertItem(L"Car Listing", TVI_ROOT);
hCar = m_treeCtrl.InsertItem(L"Economy", hItem);
m_treeCtrl.InsertItem(L"BH-733", hCar);
m_treeCtrl.InsertItem(L"SD-397", hCar);
m_treeCtrl.InsertItem(L"JU-538", hCar);
m_treeCtrl.InsertItem(L"DI-285", hCar);
m_treeCtrl.InsertItem(L"AK-830", hCar);
hCar = m_treeCtrl.InsertItem(L"Compact", hItem);
m_treeCtrl.InsertItem(L"HG-490", hCar);
m_treeCtrl.InsertItem(L"PE-473", hCar);
hCar = m_treeCtrl.InsertItem(L"Standard", hItem);
m_treeCtrl.InsertItem(L"SO-398", hCar);
m_treeCtrl.InsertItem(L"DF-438", hCar);
m_treeCtrl.InsertItem(L"IS-833", hCar);
hCar = m_treeCtrl.InsertItem(L"Full Size", hItem);
m_treeCtrl.InsertItem(L"PD-304", hCar);
hCar = m_treeCtrl.InsertItem(L"Mini Van", hItem);
m_treeCtrl.InsertItem(L"ID-497", hCar);
m_treeCtrl.InsertItem(L"RU-304", hCar);
m_treeCtrl.InsertItem(L"DK-905", hCar);
hCar = m_treeCtrl.InsertItem(L"SUV", hItem);
m_treeCtrl.InsertItem(L"FE-948", hCar);
m_treeCtrl.InsertItem(L"AD-940", hCar);
hCar = m_treeCtrl.InsertItem(L"Truck", hItem);
m_treeCtrl.InsertItem(L"HD-394", hCar);
return TRUE; // return TRUE unless you set the focus to a control
}
Step 7 − When the above code is compiled and executed, you will see the following output.