Chapter 21: Tree and List Controls Visual C++ and MFC Fundamentals
If you plan to display the control’s items in Report view and you want to use bitmaps,
you can create a long bitmap that will made of small pictures of the same size and each
picture will be used for each item. Each picture should have a size of 32x32.
If you plan to display the control’s items in Report view and other views, if you want to
use bitmaps, you should create two long bitmaps. One would be made of pictures that are
16x16 size. Such pictures would be used for the Small Icon, the List, and the Report
views. You should also create the second bitmap made of pictures of 32x32 size. These
pictures would be used for the List view.
After creating the bitmap and/or icons, you should declare a variable or a pointer to
CImageList class and initialize it using the CImageList::Create() method. To make the
image list available to the list control, call the CListCtrl::SetImageList() method. Its
syntax is:
CImageList* SetImageList(CImageList* pImageList, int nImageList);
The pImageList argument is a CImageList variable or pointer previously initialized. The
nImageList is a flag that specifies the type of image list used. It can have one of the
following values:
Value Description
LVSIL_NORMAL The image list is made of large bitmap or icons, typically 32x32
LVSIL_SMALL The image list is made of small bitmap or icons, typically 16x16
LVSIL_STATE The image list is made of pictures that will be used as mask
To associate a picture with a list item, you can use one of the following versions of the
CListCtrl::InsertItem() methods:
int InsertItem(int nItem, LPCTSTR lpszItem, int nImage );
int InsertItem(UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask,
int nImage, LPARAM lParam );
The nImage argument is the index of the image used for the item.
The nMask argument is the same as the LVITEM::mask member variable introduced
earlier. It is simply used to specify what information you need to specify on the item. The
nState argument is the same as the LVITEM::state member variable introduced earlier
and used to specify what to do with the item, whether to select it, give it focus, getting it
ready for a cut-and-paste operation, or highlighted for a drag-and-drop operation. The
nStateMask argument is used in conjunction with the nState argument. It is used to
specify what exact type of information, defined by the state flag, that will be changed or
retrieved on the item.
The lParam argument, as well as the lParam member variable of the TVITEM structure
are used to perform a specific operation on the item, such as involving it in a sort
algorithm or finding an item.