Add icon for specific tree items in Tree(CTreeCtrl) in MFC

2.1k views Asked by At

Can we add icons for specific tree items?

I am adding items with icon using following function:

HTREEITEM InsertItem(LPCTSTR lpszItem,int nImage,int nSelectedImage,HTREEITEM hParent = TVI_ROOT,HTREEITEM hInsertAfter = TVI_LAST);

To skip icon for an item, i am using -1 value for nImage and nSelectedImage. By doing this, icon is not appearing but space is coming.

1

There are 1 answers

5
Andrew Truckle On

Have you looked at CTreeCtrl::SetItem?

The easiest is to fill and pass a TVITEM structure.

typedef struct tagTVITEM {
  UINT      mask;
  HTREEITEM hItem;
  UINT      state;
  UINT      stateMask;
  LPTSTR    pszText;
  int       cchTextMax;
  int       iImage;
  int       iSelectedImage;
  int       cChildren;
  LPARAM    lParam;
} TVITEM, *LPTVITEM;

You set the mask to TVIF_IMAGE and specify the iImage value.

To begin, you need to create a CImageList object that stays valid for the duration of the CTreeCtrl. You usually add it to the class as a variable. Example:

m_imgList.Create(IDB_BMP_CHECK_IMAGELIST, 16, 10, 0x0000FF00);

Once it is initialised you can call CTreeCtrl::SetImageList. Example:

m_treeCtrl.SetImageList(&m_imgList, LVSIL_SMALL);

Thereafter you can use the image index values.