CTreeCtrl(图片和状态图片、动态提示、选中节点颜色、查询)

来源:互联网 发布:禾米网络支付 编辑:程序博客网 时间:2024/05/18 00:47

1.结构体

   typedef struct _TV_ITEM {

  UINT mask; //结构成员有效性屏蔽位

  HTREEITEM hItem; //数据项控制句柄

  UINT state; //数据项状态 s

  UINT stateMask; //状态有效性屏蔽位

  LPSTR pszText; //数据项名称字符串

  int cchTextMax; //数据项名称的最大长度

  int iImage; //数据项图标索引号

  int iSelectedImage;//选中数据项图标索引号

  int cChildren; //子项标识

  LPARAM lParam; //程序定义的32位数据

  } TV_ITEM, FAR *LPTV_ITEM;

 

          TV_INSERTSTRUCT m_tvinsert;

          m_tvinsert.hParent=NULL;
          m_tvinsert.hInsertAfter=TVI_LAST;
          m_tvinsert.item.mask=TVIF_TEXT|TVIF_STATE;
          m_tvinsert.item.hItem=NULL;
          m_tvinsert.item.state=INDEXTOSTATEIMAGEMASK(1);
          m_tvinsert.item.stateMask=TVIS_STATEIMAGEMASK;
          m_tvinsert.item.cchTextMax=10;
          m_tvinsert.item.iImage=0;
          m_tvinsert.item.iSelectedImage=0;
          m_tvinsert.item.cChildren=0;
          m_tvinsert.item.lParam=0;
          m_tvinsert.item.pszText="信息";
          InsertItem(&m_tvinsert);

 

mask
Array of flags that indicate which of the other structure members contain valid data. When this structure is used with the TVM_GETITEM message, themask member indicates the item attributes to retrieve. This member can be one or more of the following values.
TVIF_CHILDREN
The cChildren member is valid.
TVIF_DI_SETITEM
The tree-view control will retain the supplied information and will not request it again. This flag is valid only when processing the TVN_GETDISPINFO notification.
TVIF_HANDLE
The hItem member is valid.
TVIF_IMAGE
The iImage member is valid.
TVIF_PARAM
The lParam member is valid.
TVIF_SELECTEDIMAGE
The iSelectedImage member is valid.
TVIF_STATE
The state and stateMask members are valid.
TVIF_TEXT
The pszText and cchTextMax members are valid.

2.设置是否选中图片

(1)加载图片

     CImageList m_ImageList;

     m_ImageList.Create(16, 16, ILC_COLOR32 ,10,CLR_NONE);
     m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON1));
     m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON2));
     m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON3));

     10:Number of images that the image list initially contains.

     nGrow:#define CLR_NONE                0xFFFFFFFFL
                  #define CLR_DEFAULT             0xFF000000L

(2)设置状态图片

    SetImageList(&m_ctrlTree.m_ImageList,TVSIL_NORMAL);

(3)设置显示图片位置

    InsertItem("消息",0,1);

   0、1对应m_tvinsert.item.iImage、m_tvinsert.item.iSelectedImage=0;

   

BOOL SetItemImage(   HTREEITEM hItem,   int nImage,   int nSelectedImage );

3.设置状态图片

     (1)加载图片

第一种方法:

 CImageList m_imgState;

 m_imgState.Create(IDB_BITMAP3,10, 15, RGB(255,255,255));

           其中

           BOOL Create(UINT nBitmapID, int cx, int nGrow, COLORREF crMask);

           cx图片要显示的尺寸,参考以下类型,值自己定也可以如7:

ILC_COLOR

Use the default behavior if none of the other ILC_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.

           nGrow:Number of images by which the image list can grow when the system needs to resize the list to

           make room for new images. This parameter represents the number of new images the resized image

           list can contain.

                      #define CLR_NONE                0xFFFFFFFFL
                      #define CLR_DEFAULT             0xFF000000L

           RGB(255,255,255):图片背景颜色

第二种方法:

m_imgeStateList.Create(10, 15, ILC_COLOR32 ,2,CLR_NONE);
             m_imgeStateList.Add(AfxGetApp()->LoadIcon(IDI_ICON1));
             m_imgeStateList.Add(AfxGetApp()->LoadIcon(IDI_ICON2));

(2)设置状态图片

   SetImageList(&m_imgState,TVSIL_STATE);

     (3)设置显示图片位置

            SetItemState(hItem, INDEXTOSTATEIMAGEMASK(0), TVIS_STATEIMAGEMASK); // 没有图片

            SetItemState(hItem, INDEXTOSTATEIMAGEMASK(1), TVIS_STATEIMAGEMASK); // 位置1的图片

            SetItemState(hItem, INDEXTOSTATEIMAGEMASK(2), TVIS_STATEIMAGEMASK); // 位置2的图片

            。。。。。。

(4)根据条件改变状态图片

if (INDEXTOSTATEIMAGEMASK(2) != GetItemState(i, LVIS_STATEIMAGEMASK))
                 SetItemState(i, INDEXTOSTATEIMAGEMASK(1), LVIS_STATEIMAGEMASK);

4.设置背景色

    m_ImageList.SetBkColor(clrColor);

5.注意,每一个节点可以设置一个值

BOOL SetItemData(   HTREEITEM hItem,   DWORD_PTR dwData );

6.添加动态提示(ToopTip)---Create对象时,必须TVS_INFOTIP

   重载afx_msg void OnTvnGetInfoTip(NMHDR *pNMHDR, LRESULT *pResult);

  例:

void CTreeX::OnTvnGetInfoTip(NMHDR *pNMHDR, LRESULT *pResult)
{
   LPNMTVGETINFOTIP pGetInfoTip = reinterpret_cast<LPNMTVGETINFOTIP>(pNMHDR);

   strcpy(pGetInfoTip->pszText, str);
   *pResult = 0;
}

7.保持选中节点的颜色---Create对象时,必须TVS_SHOWSELALWAYS

   重载afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);

   例:

 void CTreeX::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
   LPNMTVCUSTOMDRAW pNMCD = reinterpret_cast<LPNMTVCUSTOMDRAW>(pNMHDR);
   NMCUSTOMDRAW nmCustomDraw = pNMCD->nmcd;
   switch(nmCustomDraw.dwDrawStage)
   {
     case CDDS_ITEMPREPAINT:
     {
      if(nmCustomDraw.uItemState & CDIS_SELECTED)
        pNMCD->clrTextBk = RGB(30,144,255);
     }
    default:
      break;
   }
   *pResult = 0;
   *pResult |= CDRF_NOTIFYPOSTPAINT;
   *pResult |= CDRF_NOTIFYITEMDRAW;
}

8.查询

void CMyCtrl::FindTree(CString strName)
{
 HTREEITEM selectItem = m_pTree->GetSelectedItem();
 if (selectItem != NULL)
 {
  // 从当前选择位置向下查找
  HTREEITEM hItemTemp = m_pTree->FindItem(selectItem, strName);
  if (hItemTemp != NULL)
  {
   m_pTree->SelectItem(hItemTemp);
   return;
  }
 }

 // 从开始位置到当前位置查找
 {
  HTREEITEM hItemRoot = m_pTree->GetRootItem();
  if (hItemRoot == NULL)
   return;
  // 根节点是否符合
  CString strText = m_pTree->GetItemText(hItemRoot);
  if (strText.Find(strName) != -1)
  {
   m_pTree->SelectItem(hItemRoot);
   return;
  }
  // 根节点以外的节点是否符合
  HTREEITEM hItemTemp = m_pTree->FindItem(hItemRoot, strName);
  if (hItemTemp != NULL)
  {
   m_pTree->SelectItem(hItemTemp);
   return;
  }
 }
}