Initializing the Image Lists for a List View Control

A list view control can have up to three image lists associated with it: one for item icons in icon view, one for item icons in other views, and one for application-defined item states. The following example creates two image lists, adds an icon to each, and assigns them to a list view control by using the LVM_SETIMAGELIST message.

// InitListViewImageList - creates image lists for a list view.

// Returns TRUE if successful or FALSE otherwise.

// hwndLV - handle of the list view control

BOOL WINAPI InitListViewImageLists(HWND hwndLV)

{

HICON hiconItem; // icon for list view items

HIMAGELIST himlLarge; // image list for icon view

HIMAGELIST himlSmall; // image list for other views

// Create the full-sized and small icon image lists.

himlLarge = ImageList_Create(GetSystemMetrics(SM_CXICON),

GetSystemMetrics(SM_CYICON), TRUE, 1, 1);

himlSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON),

GetSystemMetrics(SM_CYSMICON), TRUE, 1, 1);

// Add an icon to each image list.

hiconItem = LoadIcon(g_hinst, MAKEINTRESOURCE(IDI_ITEM));

ImageList_AddIcon(himlLarge, hiconItem);

ImageList_AddIcon(himlSmall, hiconItem);

DeleteObject(hiconItem);

// Assign the image lists to the list view control.

ListView_SetImageList(hwndLV, himlLarge, LVSIL_NORMAL);

ListView_SetImageList(hwndLV, himlSmall, LVSIL_SMALL);

return TRUE;

}