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

社会主义新天地 提交于 2019-12-23 20:48:55

问题


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:


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.



来源:https://stackoverflow.com/questions/41354446/add-icon-for-specific-tree-items-in-treectreectrl-in-mfc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!