3

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.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
Dipti
  • 106
  • 1
  • 8

1 Answers1

0

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.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 1
    This produces the same effect the OP is trying to prevent. If you don't set an image for an item, there will be empty space in its place. The OP wants each item to start at the far left, regardless of whether it has an icon or not. – IInspectable Dec 31 '16 at 19:56
  • I see. I think that is ugly. OK. – Andrew Truckle Dec 31 '16 at 21:29
  • 1
    @AndrewTruckle , IInspectable : Thank you for your response. If icon does not set for an item then by default, index 0 is used. But i need to add icon for specific tree items only. Please share the information if you have idea to achieve it. – Dipti Jan 04 '17 at 01:11
  • I guess you would have to do owner draw. Or use another technology like WPF where it is quite easy to decide how each item is drawn. – Andrew Truckle Jan 04 '17 at 05:40
  • @AndrewTruckle : Thank you for information. I will try suggested ways. – Dipti Jan 05 '17 at 00:35