下面是树状控件的一些应用,由于是由于用于演示,所以结构并不合理.其效果如图所示..
步骤如下:
1,Insert -> new class,插入一个新类CMyCtrl,基类为CTreeCtrl.
2,利用类向导为CMyTree添加WM_PANIT的响应函数.
代码如下:
void CMyTree::OnPaint() { CPaintDC dc(this); CWnd::DefWindowProc( WM_PAINT, (WPARAM)dc.GetSafeHdc(), 0 ); HTREEITEM hItem = this->GetFirstVisibleItem(); while(NULL != hItem) { UINT selflag = TVIS_DROPHILITED | TVIS_SELECTED; if(GetItemState( hItem, selflag ) & selflag) { hItem = GetNextVisibleItem(hItem); continue ; } CRect r; GetItemRect(hItem,&r,true); {//擦除旧内容 COLORREF col = dc.GetBkColor(); CBrush brush(col); CPen pen(PS_NULL,1,col); CBrush * pOldBrush = dc.SelectObject(&brush); CPen * pOldPen = dc.SelectObject(&pen); dc.Rectangle(&r); dc.SelectObject(pOldBrush); dc.SelectObject(pOldPen); } CString strText = GetItemText(hItem); //设置颜色 dc.SetBkColor( GetSysColor( COLOR_WINDOW ) ); dc.SetTextColor(RGB(255,0,0)); //更改字体 LOGFONT logfont ; GetFont()->GetLogFont( &logfont ); logfont.lfItalic = true; CFont font; font.CreateFontIndirect(&logfont); dc.SelectObject(&font); dc.TextOut(r.left + 2,r.top + 1,strText); hItem = GetNextVisibleItem(hItem); } }
3,建立一个基于对话框的程序,增加一个树状控件,ctrl + w打开类向导,为它增加一个关联变量m_tree_ctrl.
4,对树状控件右键,选取属性,把has buttons,has lines,lines at root都选上.
5,手动把CTreeCtrl m_tree_ctrl;改成CMyTree m_tree_ctrl;不要忘记了包含头文件,
#include "MyTree.h"
6,修改对话框的OnInitDialog
BOOL CTestTreeDlg::OnInitDialog() { ….. HTREEITEM hRootItem = m_tree_ctrl.InsertItem("五虎上将!"); m_tree_ctrl.InsertItem("关羽",hRootItem); m_tree_ctrl.InsertItem("张飞",hRootItem); m_tree_ctrl.InsertItem("赵云",hRootItem); m_tree_ctrl.InsertItem("马超",hRootItem); m_tree_ctrl.InsertItem("黄忠",hRootItem); }
7,Ctrl + F5.效果如图.
8,在资源管理器中增加一个位图,ID为IDB_STATE.
9, 在CMyTree中增加成员变量CImageList m_Images;
10,修改CMyTree的PreSubclassWindow的响应函数.
void CMyTree::PreSubclassWindow() { SetBkColor(RGB(255,255,255)); m_Images.Create (IDB_STATE, 18, 4, RGB (128, 0, 0)); SetImageList (&m_Images, TVSIL_NORMAL); SetWindowLong(m_hWnd, GWL_STYLE, TVS_SHOWSELALWAYS|GetWindowLong(m_hWnd, GWL_STYLE)); CTreeCtrl::PreSubclassWindow(); }
11,增CMyTree类=NM_CLICK的响应函数.
void CMyTree::OnClick(NMHDR* pNMHDR, LRESULT* pResult) { DWORD dw = GetMessagePos(); CPoint pt(LOWORD(dw),HIWORD(dw));//鼠标的屏幕坐标 //转换为相对树控件的坐标 CPoint pt1 = pt; CRect rect; GetClientRect(rect); ClientToScreen(&rect); pt1.x -= rect.left; pt1.y -= rect.top; //判断在树控件中的位置 TVHITTESTINFO tHitTestInfo; tHitTestInfo.pt = pt1; HitTest(&tHitTestInfo); if (TVHT_ONITEMICON == tHitTestInfo.flags) { int nImage,nSelectImage ; GetItemImage(tHitTestInfo.hItem,nImage,nSelectImage); if(0 == nImage) SetItemImage(tHitTestInfo.hItem,3,3); else if(3 == nImage) SetItemImage(tHitTestInfo.hItem,0,0); } *pResult = 0; }
12,把对话框标题改为”请选择出阵的武将”.
13,
BOOL CTestTreeDlg::OnInitDialog() { ….. HTREEITEM hItem ; hItem = m_tree_ctrl.InsertItem("关羽"); m_tree_ctrl.SetItemImage(hItem,1,1); m_tree_ctrl.InsertItem("张飞"); m_tree_ctrl.InsertItem("赵云"); m_tree_ctrl.InsertItem("马超"); hItem = m_tree_ctrl.InsertItem("黄忠"); m_tree_ctrl.SetItemImage(hItem,2,2); }
14,把树状控件的has line取消掉.
15,再为对话框换一个好看的图标.
16,结果如最前面.