List Items and Image Lists
The latest version of this topic can be found at List Items and Image Lists.
An "item" in a list control (CListCtrl) consists of an icon, a label, and possibly other information (in "subitems").
The icons for list control items are contained in image lists. One image list contains full-sized icons used in icon view. A second, optional, image list contains smaller versions of the same icons for use in other views of the control. A third optional list contains "state" images, such as check boxes, for display in front of the small icons in certain views. A fourth optional list contains images that are displayed in individual header items of the list control.
Note
If a list view control is created with the LVS_SHAREIMAGELISTS
style, you are responsible for destroying the image lists when they are no longer in use. Specify this style if you assign the same image lists to multiple list view controls; otherwise, more than one control might try to destroy the same image list.
For more information about list items, see List View Image Lists and Items and Subitems in the Windows SDK. Also see class CImageList in the MFC Reference and Using CImageList in this family of articles.
To create a list control, you need to supply image lists to be used when you insert new items into the list. The following example demonstrates this procedure, where m_pImagelist
is a pointer of type CImageList
and m_listctrl
is a CListCtrl
data member.
m_ListImageList.Create(16, 16, ILC_COLOR, 2, 2);
m_ListImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON1));
m_ListImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON2));
m_ListCtrl.SetImageList(&m_ListImageList, LVSIL_SMALL);
However, if you don't plan to display icons in your list view or list control, you don't need image lists.