To create a tree view control in C, use the CreateWindow or CreateWindowEx function and specify the WC_TREEVIEW style for the window class. If you are using MFC, create a CTreeCtrl object by using the Create member function. The following code produces a tree view control and uses the image list functions to create an image list associated with it:
HWND CreateTreeView (HWND hWndParent)
{
HWND hwndTree; // handle to tree view window
RECT rcl; // rectangle for setting size of window
HBITMAP hBmp; // handle to a bitmap
HIMAGELIST hIml; // handle to image list
// Ensure that the common control DLL is loaded.
InitCommonControls ();
// Get the size and position of the parent window.
GetClientRect (hWndParent, &rcl);
// Create the tree view window.
hwndTree = CreateWindowEx (
0L,
WC_TREEVIEW, // tree view class
"", // no default text
WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES | TVS_HASBUTTONS
|
TVS_LINESATROOT,
0, 0,
rcl.right - rcl.left, rcl.bottom - rcl.top - 15,
hWndParent,
(HMENU)ID_TREEVIEW,
hInst,
NULL);
if (hwndTree == NULL)
return NULL;
// Initialize the tree view window.
// First create the image list you will need.
hIml = ImageList_Create (
BITMAP_WIDTH, // width
BITMAP_HEIGHT, // height
0, // creation flags
NUM_BITMAPS, // number of images
0); // amount this list can grow
// Load the bitmaps and add them to the image list.
hBmp = LoadBitmap (hInst, MAKEINTRESOURCE (FORSALE));
idxForSale = ImageList_Add (
hIml, // handle to image list
hBmp) // handle of bitmap to add
NULL); // handle of bitmap mask
hBmp = LoadBitmap (hInst, MAKEINTRESOURCE (REDMOND));
idxRedmond = ImageList_Add (hIml, hBmp, NULL);
hBmp = LoadBitmap (hInst, MAKEINTRESOURCE (BELLEVUE));
idxBellevue = ImageList_Add (hIml, hBmp, NULL);
hBmp = LoadBitmap (hInst, MAKEINTRESOURCE (SEATTLE));
idxSeattle = ImageList_Add (hIml, hBmp, NULL);
// Be sure that all the bitmaps were added.
if (ImageList_GetImageCount (hIml) < NUM_BITMAPS)
return FALSE;
// Associate the image list with the tree view control.
TreeView_SetImageList (hwndTree, hIml, idxForSale);
return hwndTree;
}