Initializing 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, you must use the image list functions to create an image list and add images to it. Then you must associate the image list with the tree-view control by using the TVM_SETIMAGELIST message.

The following example creates an image list, adds three bitmaps to the list, and associates the image list with a tree-view control.

// InitTreeViewImageLists - creates an image list, adds three bitmaps to // it, and associates the image list with a tree-view control.

// Returns TRUE if successful or FALSE otherwise.

// hwndTV - handle of the tree-view control

//
// Global variables and constants

// g_nOpen, g_nClosed, and g_nDocument - integer variables for

// indexes of the images

// CX_BITMAP and CY_BITMAP - width and height of an icon

// NUM_BITMAPS - number of bitmaps to add to the image list

BOOL InitTreeViewImageLists(HWND hwndTV)

{

HIMAGELIST himl; // handle of image list

HBITMAP hbmp; // handle of bitmap

// Create the image list.

if ((himl = ImageList_Create(CX_BITMAP, CY_BITMAP,

FALSE, NUM_BITMAPS, 0)) == NULL)

return FALSE;

// Add the open file, closed file, and document bitmaps.

hbmp = LoadBitmap(g_hinst, MAKEINTRESOURCE(IDB_OPEN_FILE));

g_nOpen = ImageList_Add(himl, hbmp, (HBITMAP) NULL);

DeleteObject(hbmp);

hbmp = LoadBitmap(g_hinst, MAKEINTRESOURCE(IDB_CLOSED_FILE));

g_nClosed = ImageList_Add(himl, hbmp, (HBITMAP) NULL);

DeleteObject(hbmp);

hbmp = LoadBitmap(g_hinst, MAKEINTRESOURCE(IDB_DOCUMENT));

g_nDocument = ImageList_Add(himl, hbmp, (HBITMAP) NULL);

DeleteObject(hbmp);

// Fail if not all of the images were added.

if (ImageList_GetImageCount(himl) < 3)

return FALSE;

// Associate the image list with the tree-view control.

TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL);

return TRUE;

}