一、创建对话框

二、添加树视图控件,将控件Has Buttons 属性、Has lines 属性和Lines at root 属性打勾,并使用类向导为控件关联变量m_Tree.

三、在对话框类中定义一个图像列表控件

CImageList m_ImageList;

四、在对话框初始化(OnInitDialog方法)中添加如下代码:

m_ImageList.Create(16,16,ILC_COLOR24|ILC_MASK,1,1);  //创建图像列表控件 //向图像列表中添加图标 m_ImageList.Add (AfxGetApp()->LoadIcon(IDI_ICON1)); m_ImageList.Add (AfxGetApp()->LoadIcon(IDI_ICON2)); m_ImageList.Add (AfxGetApp()->LoadIcon(IDI_ICON3)); m_ImageList.Add (AfxGetApp()->LoadIcon(IDI_ICON4)); m_ImageList.Add (AfxGetApp()->LoadIcon(IDI_ICON5)); m_ImageList.Add (AfxGetApp()->LoadIcon(IDI_ICON6)); //m_ImageList.Add(LoadIcon(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_ICON4))); m_Tree.SetImageList(&m_ImageList,TVSIL_NORMAL);  //关联图像列表 HTREEITEM hMyDown=m_Tree.InsertItem("我的下载",-1,-1);  //添加根节点 //HTREEITEM hMyShare=m_Tree.InsertMenuItem("全部任务",1,1,hMyDown);  //添加二级子节点 //在“我的下载”节点下添加子节点 m_Tree.InsertItem("全部任务",0,0,hMyDown); m_Tree.InsertItem("正在下载",1,1,hMyDown); m_Tree.InsertItem("已完成",2,2,hMyDown); m_Tree.InsertItem("垃圾箱",3,3,hMyDown); HTREEITEM hMyShare=m_Tree.InsertItem("我的分享",-1,-1);  //添加根节点 //在“我的分享”节点下添加子节点 m_Tree.InsertItem("添加分享",4,4,hMyShare); m_Tree.InsertItem("删除分享",5,5,hMyShare);//m_Tree.SetItemState(hMyShare,INDEXTOSTATEIMAGEMASK(1),TVIS_STATEIMAGEMASK); m_Tree.Expand(hMyDown,TVE_EXPAND);  //展开“我的下载”根节点 m_Tree.Expand(hMyShare,TVE_EXPAND);  //展开“我的分享”根节点