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 dynamic-link library (DLL) is loaded. To ensure that the DLL is loaded, use the InitCommonControls function.
The following example creates a tree-view control that is sized to fit the client area of the parent window. It also uses application-defined functions to associate an image list with the control and add items to the control.
// CreateATreeView - creates a tree-view control.
// Returns the handle of the new control if successful or NULL
// otherwise.
// hwndParent - handle of the control's parent window
// lpszFileName - name of the file to parse for tree-view items
HWND CreateATreeView(HWND hwndParent, LPSTR lpszFileName)
{
RECT rcClient; // dimensions of client area
HWND hwndTV; // handle of tree-view control
// Ensure that the common control DLL is loaded.
InitCommonControls();
// Get the dimensions of the parent window's client area, and create
// the tree-view control.
GetClientRect(hwndParent, &rcClient);
hwndTV = CreateWindowEx(0, WC_TREEVIEW, "Tree View",
WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES,
0, 0, rcClient.right, rcClient.bottom,
hwndParent, (HMENU) ID_TREEVIEW, g_hinst, NULL);
// Initialize the image list, and add items to the control.
// InitTreeViewImageLists and InitTreeViewItems are application-
// defined functions.
if (!InitTreeViewImageLists(hwndTV) ||
!InitTreeViewItems(hwndTV, lpszFileName)) {
DestroyWindow(hwndTV);
return FALSE;
}
return hwndTV;
}