使用CTreeCtrl,主要是分两步,一步是设置展开与收起的图标,还有就是往目录树里面添加数据。
一般需要设置控件的属性:Has lines, lines at root,
下面是一个简单的例子:
BOOL InitMytree()
{
//节点的图标
int i=0;
int i_count=2;
//载入图标
HICON icon[4];
icon[0]=AfxGetApp()->LoadIcon (IDI_ICON2);
icon[1]=AfxGetApp()->LoadIcon (IDI_ICON1);
//创建图像列表控件
m_imagelist=new CImageList;
m_imagelist->Create(16,16,ILC_COLOR8|ILC_MASK,1,1);
m_imagelist->SetBkColor (RGB(255,255,255));
for(int n=0;n<i_count;n++)
{
m_imagelist->Add(icon[n]); //把图标载入图像列表控件
}
m_Tree.SetImageList(m_imagelist,TVSIL_NORMAL);
//为m_Tree设置一个图像列表,使CtreeCtrl的节点显示不同的图标
m_Tree.SetBkColor(RGB(255,255,255));
//设置m_Tree的背景色
//创建节点
//父节点
HTREEITEM root=m_Tree.InsertItem("所有",0,1,TVI_ROOT,TVI_LAST);
//一层子节点
HTREEITEM root1=m_Tree.InsertItem("1区",0,1,root,TVI_LAST);
HTREEITEM root2=m_Tree.InsertItem("2区",0,1,root,TVI_LAST);
HTREEITEM root3=m_Tree.InsertItem("2区",0,1,root,TVI_LAST);
//二层孙子节点
m_Tree.InsertItem("1-1区",0,1,root1,TVI_LAST);
m_Tree.InsertItem("1-2区",0,1,root1,TVI_LAST);
m_Tree.InsertItem("1-3区",0,1,root1,TVI_LAST);
m_Tree.InsertItem("1-4区",0,1,root1,TVI_LAST);
m_DefaultItem = m_Tree.InsertItem("1-5区",0,1,root1,TVI_LAST);
//二层孙子节点
m_Tree.InsertItem("2-1区",0,1,root2,TVI_LAST);
m_Tree.InsertItem("2-2区",0,1,root2,TVI_LAST);
m_Tree.InsertItem("2-3区",0,1,root2,TVI_LAST);
m_Tree.InsertItem("2-4区",0,1,root2,TVI_LAST);
m_Tree.InsertItem("2-5区",0,1,root2,TVI_LAST);
//二层孙子节点
m_Tree.InsertItem("3-1区",0,1,root3,TVI_LAST);
m_Tree.InsertItem("3-2区",0,1,root3,TVI_LAST);
m_Tree.InsertItem("3-2区",0,1,root3,TVI_LAST);
m_Tree.InsertItem("3-4区",0,1,root3,TVI_LAST);
m_Tree.Expand(root, TVE_EXPAND);
m_Tree.Expand(root1, TVE_EXPAND);
m_Tree.Expand(root2, TVE_EXPAND);
m_Tree.Expand(root3, TVE_EXPAND);
m_Tree.SelectItem(m_DefaultItem);
return true;
}