为树控制设置状态图标

2014-11-23 20:16:22 · 作者: · 浏览: 10

 

步骤一:创建图像

添加一个包含全部图标资源的位图到资源编辑器。下面的列子,我们使用了一个13X13像素位图资源,当然你也可以使用不同尺寸的位图资源。

步骤二:为图像列表添加变量

class CTreeCtrlX : public CTreeCtrl

{

// Construction

public:

CTreeCtrlX();

// Attributes

public:

CImageList m_imageState ;

:

:

:

}

步骤三:创建并设置图像列表

调用Create()函数,用步骤一中给出的图像ID与大小创建图像列表,用SetImageList()函数设置图像列表。过程如下:

m_tree.m_imageState.Create( IDB_STATE, 13, 1, RGB(255,255,255) );

m_tree.SetImageList( &(m_tree.m_imageState), TVSIL_STATE );

使用时我们在OnInitDialog()函数或OnInitialUpdate()函数中调用它。

步骤四:为每一个项目,指定状态图像

一旦为你的树控制分配了图像列表,当你插入项目到你的树控制时,你可以为它指定状态图标SetItemState()函数可以达到上述目的。在此我们要使用INDEXTOSTATEIMAGEMASK( )宏,重新排序索引值。

// Using TV_INSERTSTRUCT

CString str = "xyzASDFqwerZCV";

TV_INSERTSTRUCT tv_is;

tv_is.hParent = parent parent : TVI_ROOT;

tv_is.hInsertAfter = TVI_LAST ;

tv_is.item.mask = TVIF_TEXT | TVIF_STATE;

tv_is.item.stateMask = TVIS_STATEIMAGEMASK;

tv_is.item.state = INDEXTOSTATEIMAGEMASK( 1 );

tv_is.item.pszText = str.GetBuffer(1);

tv_is.item.cchTextMax = str.GetLength();

hItem = InsertItem( &tv_is );

str.ReleaseBuffer();

// Using SetItemState

SetItemState( hItem, INDEXTOSTATEIMAGEMASK(1), TVIS_STATEIMAGEMASK );