新建以CTreeView为视基类的单文档应用程序。
树控件通常和图像列表控件一起使用,在树控件中可以显示图像列表中的图标,即为树控件添加图标。
(1)创建位图
在资源编辑器中,添加一个包括所有图标的位图资源,在此位图中每个图标都是16*16像素大小,用户可以改变大小,不过必须为正方形。
(2)添加用于存放图像列表的成员变量
CImageList m_image;
(3)创建并设置图像列表
m_image.Create(IDB_IMAGE,16,1,RGB(255,255,255));//位图ID 16*16大小 新图像列表中图标数目为一个 白色为透明色
m_TreeCtrl.SetImageList(&m_ImageList,TVSIL_NORMAL); //设置图象列
TCItem.item.lParam=0;//序号
TCItem.item.iImage=0;//正常图标
TCItem.item.iSelectedImage=1;//选中时图标
(4)指定条目图标
CTreeCtrl::InsertItem和SetItemImage的参数条目都需要两个不同的图像值,一个用于选中一个用于未选中,可以相同
(5)设置状态图像,例如使用选中态和清除态复选框来表示条目状态,条目状态的12~15位指定了以1为基的状态图像索引(0表示没有状态图像),最多15个
m_TreeCtrl.SetImageList(&m_ImageState,TVSIL_STATE);//TVSIL for SetImageList STATE
//指定状态图标注意TVSIL_STATE
m_TreeCtrl.SetItemState(hRoot,INDEXTOSTATEIMAGEMASK(1),TVIS_STATEIMAGEMASK);//索引为1的设为状态图像
(6)设置覆盖图像
条目状态的8~11位指定覆盖图像
BOOL CImageList::SetOverlayImage(
int nImage,
int nOverlay
);
nImage 以0为基的图像索引,
nOverlay以1为基的覆盖图像索引,使用的是包含控件条目图像的图像列表对象。
//设置覆盖图像
m_ImageList.SetOverlayImage(0,1);
m_ImageList.SetOverlayImage(1,2);
//将条目状态图像设置为使用覆盖图像
m_TreeCtrl.SetItemState(hRoot,INDEXTOOVERLAYMASK(2),TVIS_OVERLAYMASK);//使用第一个覆盖图像
m_TreeCtrl.SetItemState(hRoot,INDEXTOOVERLAYMASK(0),TVIS_OVERLAYMASK);//清除覆盖图像
在view类添加图像列表控件,向工程中导入想要显示的图标资源。
public:
CImageList m_ImageList;
资源中添加两个图标,一个为选中时,一个为未选中
CTreeCtrl& m_TreeCtrl=GetTreeCtrl();;
//pTree.Create(TVS_LINESATROOT|TVS_HASLINES|TVS_HASBUTTONS|WS_BORDER|LVS_SHOWSELALWAYS,CRect(0,0,0,0),this,10001);
//创建树控件
m_ImageList.Create(16,16,ILC_COLOR,3,0); //大小为16*16的
m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_CHECK));
m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_SELECT));
m_TreeCtrl.SetImageList(&m_ImageList,TVSIL_NORMAL); //设置图象列
char * CJ[4]={"玉溪卷烟厂","云南卷烟厂","沈阳卷烟厂","成都卷烟厂"};//根数据名称
char * PM[4][5]={ {"红梅一","红梅二","红梅三","红梅四","红梅五"},//产品数据项
{"白梅一","白梅二","白梅三","白梅四","白梅五"}, {"绿梅一","绿梅二","绿梅三","绿梅四","绿梅五"}, {"青梅一","青梅二","青梅三","青梅四","青梅五"}};
int i,j;
HTREEITEM hRoot,hCur;//树控制项目句柄
TV_INSERTSTRUCT TCItem;//插入数据项数据结构
TCItem.hParent=TVI_ROOT;//增加根项
TCItem.hInsertAfter=TVI_LAST;//在最后项之后
TCItem.item.mask=TVIF_TEXT|TVIF_PARAM|TVIF_IMAGE|TVIF_SELECTEDIMAGE;//设屏蔽
TCItem.item.pszText="数据选择";
TCItem.item.lParam=0;//序号
TCItem.item.iImage=0;//正常图标
TCItem.item.iSelectedImage=1;//选中时图标
hRoot=m_TreeCtrl.InsertItem(&TCItem);//返回根项句柄
for(i=0;i<4;i++){//增加各厂家
TCItem.hParent=hRoot;
TCItem.item.pszText=CJ[i];
TCItem.item.lParam=(i+1)*10;//子项序号
hCur=m_TreeCtrl.InsertItem(&TCItem);
for(j=0;j<5;j++){//增加各产品
TCItem.hParent=hCur;
TCItem.item.pszText=PM[i][j];
TCItem.item.lParam=(i+1)*10+(j+1);//子项序号
m_TreeCtrl.InsertItem(&TCItem); }
m_TreeCtrl.Expand(hCur,TVE_EXPAND);//展开树
}
m_TreeCtrl.Expand(hRoot,TVE_EXPAND);//展开上一级树
BOOL Create( int cx, int cy, UINT nFlags, int nInitial, int nGrow );
BOOL Create( UINT nBitmapID, int cx, int nGrow, COLORREF crMask );
BOOL Create( LPCTSTR lpszBitmapID, int cx, int nGrow, COLORREF crMask );
BOOL Create( CImageList& imagelist1, int nImage1, CImageList& imagelist2, int nImage2, int dx, int dy );
BOOL Create( CImageList* pImageList );
Return Value
Nonzero if successful; otherwise 0.
cx每个图像宽度,像素数
cy每个图像高度,像素数
nFlags
Specifies the type of image list to create. This parameter can be a combination of the following values, but it can include only one of theILC_COLORvalues.
Value | Meaning |
ILC_COLOR | Use the default behavior if none of the otherILC_COLOR* flags is specified. Typically, the default isILC_COLOR4; but for older display drivers, the default isILC_COLORDDB. |
ILC_COLOR4 | Use a 4-bit (16 color) device-independent bitmap (DIB) section as the bitmap for the image list. |
ILC_COLOR8 | Use an 8-bit DIB section. The colors used for the color table are the same colors as the halftone palette. |
ILC_COLOR16 | Use a 16-bit (32/64k color) DIB section. |
ILC_COLOR24 | Use a 24-bit DIB section. |
ILC_COLOR32 | Use a 32-bit DIB section. |
ILC_COLORDDB | Use a device-dependent bitmap. |
ILC_MASK | Uses a mask. The image list contains two bitmaps, one of which is a monochrome bitmap used as a mask. If this value is not included, the image list contains only one bitmap. |
nInitial初始包含的图像数量.
nGrow新图像列表中的图标数目
nBitmapID与图像列表关联的位图资源IDs
crMask掩码颜色,也就是说使用此颜色的所有像素将被设为透明色。Each pixel of this color in the specified bitmap is changed to black, and the corresponding bit in the mask is set to one.
lpszBitmapID
A string containing the resource IDs of the images.
imagelist1
A reference to aCImageListobject.
nImage1
Index of the first existing image.
imagelist2
A reference to aCImageListobject.
nImage2
Index of the second existing image.
dx
Offset of the x-axis of the second image in relationship to the first image, in pixels.
dy
Offset of the y-axis of the second image in relationship to the first image, in pixels.
pImageList
A pointer to aCImageListobject.
Remarks
You construct aCImageListin two steps. First call the constructor, then callCreate, which creates the image list and attaches it to theCImageListobject.
Example
// The pointer to my image list.
extern CImageList* pmyImageList;
pmyImageList->Create(32, 32, ILC_COLOR8, 0, 4);