Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 21: Tree and List Controls


21.1.5..Tree Control With Bitmaps or Icons................................................


Bitmaps can be used to enhanced the display of items on a tree control. Each tree item
can be configured to display or not to display a small picture on its left. To do this, you
can declare a CImageList variable and add pictures to it. Once the image list is ready, you
can call the CTreeCtrl::SetImageList() method. Its syntax is:

CImageList* SetImageList(CImageList * pImageList, int nImageListType);

The first argument, pImageList, is a pointer to a CImageList variable. The
nImageListType argument specifies the type of image list that will be used. The possible
values are:

Value Type Description
LVSIL_NORMAL The image list is made of large icons
LVSIL_SMALL The image list is made of small icons
LVSIL_STATE The image list is made of state images

To specify the pictures used for a tree item, call one of the following versions of the
CTreeCtrl:: InsertItem() methods:

HTREEITEM InsertItem(UINT nMask, LPCTSTR lpszItem, int nImage,
int nSelectedImage, UINT nState,
UINT nStateMask, LPARAM lParam,
HTREEITEM hParent, HTREEITEM hInsertAfter );
HTREEITEM InsertItem(LPCTSTR lpszItem, int nImage, int nSelectedImage,
HTREEITEM hParent = TVI_ROOT,
HTREEITEM hInsertAfter = TVI_LAST);

The nMask argument specifies the type of value used to set on the list item. As seen
already, the lpszItem is the text that will be displayed for the current item.

The value of nImage is the index of the image used for the item being inserted from the
image list. The nSelectedImage value is the index of the image that will display when the
inserted item is selected or has focus.

Practical Learning: Using an Image List on a Tree Control



  1. Display the Add Resource dialog box and double-click Bitmap

  2. On the Properties, change its ID to IDB_IMGTREE

  3. Design is as follows:

  4. In the header file of the dialog, declare a private CImageList variable and name it
    ImgList

  5. To assign the pictures to the tree items, change the OnInitDialog() event of the dialog
    box as follows:

Free download pdf