Step 1: create an image
Add a bitmap containing all the ICON resources to the resource editor. The following column, we use13X13 pixel bitmap resources, of course, you can also use bitmap resources of different sizes.
Step 2: add variables to the image list
Class CTreeCtrlX: public CTreeCtrl
{
// Construction
Public:
CTreeCtrlX ();
// Attributes
Public:
CImageList m_imageState;
:
:
:
}
Step 3: Create and set the image list
CallCreate () function, Create an image list with the image ID and size given in step 1, and use SetImageList () function to set the image list. The process is as follows:
M_tree.m_imageState.Create (IDB_STATE, 13, 1, RGB (255,255,255 ));
M_tree.SetImageList (& (m_tree.m_imageState), TVSIL_STATE );
When usingThe OnInitDialog () function or OnInitialUpdate () function calls it.
Step 4: Specify the status image for each project
Once an image list is assigned to your tree control, you can specify a status icon for the project when you insert it to your tree control.The SetItemState () function can achieve the above purpose. Here we will use the INDEXTOSTATEIMAGEMASK () macro to re-sort the index value.
// 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 );