天天看點

CTreeCtrl(圖檔和狀态圖檔、動态提示、選中節點顔色、查詢)

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, the mask 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;

  }

 }

}

繼續閱讀