在MFC的Tree(CTreeCtrl)中为特定树项添加图标 [英] Add icon for specific tree items in Tree(CTreeCtrl) in MFC
问题描述
我们可以为特定的树项目添加图标吗?
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);
HTREEITEM InsertItem(LPCTSTR lpszItem,int nImage,int nSelectedImage,HTREEITEM hParent = TVI_ROOT,HTREEITEM hInsertAfter = TVI_LAST);
要跳过某个项目的图标,我将nImage
和nSelectedImage
的值设为-1.这样,图标就不会出现,但是空间就要来了.
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.
推荐答案
您是否看过 CTreeCtrl :: SetItem ?
最简单的方法是填充并传递 TVITEM 结构.
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;
您将mask
设置为TVIF_IMAGE
并指定iImage
值.
首先,您需要创建一个CImageList
对象,该对象在CTreeCtrl
的持续时间内保持有效.您通常将其作为变量添加到类中.示例:
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);
初始化后,您可以调用 CTreeCtrl :: SetImageList 一个>.示例:
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.
这篇关于在MFC的Tree(CTreeCtrl)中为特定树项添加图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!