树形控件TreeCtrl关联的MFC中的类:CTreeCtrl

1、插入结点:InsertItem

2、获取与设置结点的文本:GetItemText、SetItemText

3、删除某个结点:DeleteItem

4、删除所有结点:DeleteAllItems

5、收起与展开结点:Expand

6、获取与设置选择结点:GetSelectedItem、SelectItem

7、获取与设置虚线的颜色:GetLineColor、SetLineColor

8、获取与设置文本的颜色:GetTextColor、SetTextColor

9、获取与设置Check:GetCheck、SetCheck

10、获取与设置扩展风格样式:GetExtendedStyle、SetExtendedStyle

11、获取与设置结点的图标:GetItemImage、SetItemImage

12、获取与设置图标列表:GetImageList、SetImageList


13、获取根结点:GetRootItem

14、获取父结点:GetParentItem

15、获取子结点:GetChildItem

16、获取下一个兄弟结点:GetNextSiblingItem

HTREEITEM hNext = m_TreeCtrl.GetNextSiblingItem(hSelect);

if ( hNext ) {

m_TreeCtrl.SetItemText(hNext, _T("ddddd"));

}

17、是否有子结点:ItemHasChildren


18、编辑结点:EditLabel

CString strText;

m_TreeCtrl.GetEditControl()->GetWindowText(strText);

m_TreeCtrl.SetItemText(pTVDispInfo->item.hItem, strText);


19、获取与设置某结点的附加数据:GetItemData、SetItemData

20、取得下一个结点,可以是:选择、兄弟、儿子、可见、下一个可见、上一个、根、父亲 等:GetNextItem


21、获取可视结点的个数、第一个可视结点、下一个可视结点、最后一个可视结点:

这里有必要跟大家解释一下可视结点的意思:即,当前窗口所能看见的结点的个数,如果超过了就会出现滚动条!

GetVisibleCount、GetFirstVisibleItem、GetNextVisibleItem、GetLastVisibleItem


UINT uCount = m_TreeCtrl.GetVisibleCount();

HTREEITEM hItem = m_TreeCtrl.GetFirstVisibleItem();

for (UINT idx = 0; idx < uCount; idx++) {

   ASSERT(hItem != NULL);

   m_TreeCtrl.SetCheck(hItem, !m_TreeCtrl.GetCheck(hItem));

   hItem = m_TreeCtrl.GetNextVisibleItem(hItem);

}


22.获取与设置文本的颜色:GetTextColor、SetTextColor只能设置全部节点文本颜色,当我们只设置某一个节点的文本时需要重写CTreeCtrl类,写一个派生类

TreeCtrlEx.h    TreeCtrlEx.cpp  其中的SetItemColor(HTREEITEM hItem, COLORREF color)来实现

 

TreeCtrlEx.h

/
// 
// Author:  Sami (M.ALSAMSAM), [email protected]
//
// Filename: TreeCtrlEx.h
//
// http  : www.ittiger.net
//
//
#if !defined(AFX_TREECTRLEX_H__5D969ED4_7DEA_4FB5_8C1D_E12D1CCF0989__INCLUDED_)
#define AFX_TREECTRLEX_H__5D969ED4_7DEA_4FB5_8C1D_E12D1CCF0989__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include

//
class CTreeCtrlEx : public CTreeCtrl
{
 DECLARE_DYNAMIC(CTreeCtrlEx)

public:
     CTreeCtrlEx();
 virtual   ~CTreeCtrlEx();

 void   SetItemFont(HTREEITEM, LOGFONT&);
 void   SetItemBold(HTREEITEM, BOOL);
 void   SetItemColor(HTREEITEM, COLORREF);
 BOOL   GetItemFont(HTREEITEM, LOGFONT *);
 BOOL   GetItemBold(HTREEITEM);
 COLORREF  GetItemColor(HTREEITEM);

protected:

 struct Color_Font {
  COLORREF color;
  LOGFONT  logfont;
 };

 CMap m_mapColorFont;

 protected:
 //{{AFX_MSG(CTreeCtrlEx)
  afx_msg void OnPaint();
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()

};

#endif // !defined(AFX_TREECTRLEX_H__5D969ED4_7DEA_4FB5_8C1D_E12D1CCF0989__INCLUDED_)

 

TreeCtrlEx.cpp

/
// 
// Author:  Sami (M.ALSAMSAM), [email protected]
//
// Filename: TreeCtrlEx.cpp
//
// http  : www.ittiger.net
//
//
#include "stdafx.h"
#include "ColorTree.h"
#include "TreeCtrlEx.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//
CTreeCtrlEx::CTreeCtrlEx()
{
}

CTreeCtrlEx::~CTreeCtrlEx()
{
}

//
IMPLEMENT_DYNAMIC(CTreeCtrlEx, CTreeCtrl)

BEGIN_MESSAGE_MAP(CTreeCtrlEx, CTreeCtrl)
 //{{AFX_MSG_MAP(CTreeCtrlEx)
 ON_WM_PAINT()
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

//
void CTreeCtrlEx::SetItemFont(HTREEITEM hItem, LOGFONT& logfont)
{
 Color_Font cf;
 if( !m_mapColorFont.Lookup( hItem, cf ) )
  cf.color = (COLORREF)-1;
 cf.logfont = logfont;
 m_mapColorFont[hItem] = cf;
}

//
void CTreeCtrlEx::SetItemBold(HTREEITEM hItem, BOOL bBold)
{
 SetItemState(hItem, bBold ? TVIS_BOLD: 0, TVIS_BOLD);
}

//
void CTreeCtrlEx::SetItemColor(HTREEITEM hItem, COLORREF color)
{
 Color_Font cf;
 if(!m_mapColorFont.Lookup(hItem, cf))
  cf.logfont.lfFaceName[0] = '\0';
 cf.color = color;
 m_mapColorFont[hItem] = cf;
}

//
BOOL CTreeCtrlEx::GetItemFont(HTREEITEM hItem, LOGFONT * plogfont)
{
 Color_Font cf;
 if(!m_mapColorFont.Lookup(hItem, cf))
  return FALSE;
 if(cf.logfont.lfFaceName[0] == '\0')
  return FALSE;
 *plogfont = cf.logfont;
 return TRUE;

}

//
BOOL CTreeCtrlEx::GetItemBold(HTREEITEM hItem)
{
 return GetItemState(hItem, TVIS_BOLD) & TVIS_BOLD;
}

//
COLORREF CTreeCtrlEx::GetItemColor(HTREEITEM hItem)
{
 // Returns (COLORREF)-1 if color was not set
 Color_Font cf;
 if(!m_mapColorFont.Lookup(hItem, cf))
  return (COLORREF) - 1;
 return cf.color;

}

//
void CTreeCtrlEx::OnPaint()
{
 CPaintDC dc(this);

 // Create a memory DC compatible with the paint DC
 CDC memDC;
 memDC.CreateCompatibleDC(&dc);

 CRect rcClip, rcClient;
 dc.GetClipBox( &rcClip );
 GetClientRect(&rcClient);

 // Select a compatible bitmap into the memory DC
 CBitmap bitmap;
 bitmap.CreateCompatibleBitmap( &dc, rcClient.Width(), rcClient.Height() );
 memDC.SelectObject( &bitmap );
 
 // Set clip region to be same as that in paint DC
 CRgn rgn;
 rgn.CreateRectRgnIndirect( &rcClip );
 memDC.SelectClipRgn(&rgn);
 rgn.DeleteObject();
 
 // First let the control do its default drawing.
 CWnd::DefWindowProc(WM_PAINT, (WPARAM)memDC.m_hDC, 0);

 HTREEITEM hItem = GetFirstVisibleItem();

 int iItemCount = GetVisibleCount() + 1;
 while(hItem && iItemCount--)
 {  
  CRect rect;

  // Do not meddle with selected items or drop highlighted items
  UINT selflag = TVIS_DROPHILITED | TVIS_SELECTED;
  Color_Font cf;
 
  //if ( !(GetTreeCtrl().GetItemState( hItem, selflag ) & selflag )
  // && m_mapColorFont.Lookup( hItem, cf ))
  
  if ((GetItemState(hItem, selflag) & selflag)
   && ::GetFocus() == m_hWnd)
   ;
  else if (m_mapColorFont.Lookup(hItem, cf))
  {
   CFont *pFontDC;
   CFont fontDC;
   LOGFONT logfont;

   if(cf.logfont.lfFaceName[0] != '\0')
    logfont = cf.logfont;
   else {
    // No font specified, so use window font
    CFont *pFont = GetFont();
    pFont->GetLogFont( &logfont );
   }

   if(GetItemBold(hItem))
    logfont.lfWeight = 700;

   fontDC.CreateFontIndirect(&logfont);
   pFontDC = memDC.SelectObject(&fontDC );

   if(cf.color != (COLORREF) - 1)
    memDC.SetTextColor(cf.color);
   else
    memDC.SetTextColor(GetSysColor(COLOR_WINDOWTEXT));


   CString sItem = GetItemText(hItem);

   GetItemRect(hItem, &rect, TRUE);
   memDC.SetBkColor( GetSysColor(COLOR_WINDOW));
   memDC.TextOut(rect.left + 2, rect.top + 1, sItem);
   
   memDC.SelectObject(pFontDC);
  }
  hItem = GetNextVisibleItem(hItem);
 }


 dc.BitBlt(rcClip.left, rcClip.top, rcClip.Width(), rcClip.Height(), &memDC,
    rcClip.left, rcClip.top, SRCCOPY);

 memDC.DeleteDC();
}

 

 

 

 

你可能感兴趣的:(MFC,CTreeCtrl,节点,文本颜色)