您的位置:首页 > 其它

MFC使用TreeCtrl控件

2012-10-29 16:49 435 查看
在开发过程中TreeCtrl控件的使用也比较广泛,比如我们的资源浏览视图,我这里为大家分享一下我使用TreeCtrl控件的一些技巧。

1.我们新建一个Dlg对话框MFC程序

2.拖入一个TreeCtrl控件,控件ID修改为IDC_TREE,把控件的Has Lines属性选择为True(这样能让根与根下的Item有连接线),把控件的Has Buttons属性选择为True(这样可以在父项中显示加减号)如果还需要复选框的话把Check Boxes属性选择为True就可以了。

3.为TreeCtrl控件关键CTreeCtrl变量m_treectrl

4.在dlg的OnInitDialog输入以下代码:

CImageList m_list;
m_list.Create(16,16,ILC_COLOR24,10,0);
m_list.SetBkColor(RGB(255,255,255));
m_list.Add(AfxGetApp()->LoadIcon(IDI_ACCORD));//加载图标
m_list.Add(AfxGetApp()->LoadIcon(IDI_AUTO));
m_list.Add(AfxGetApp()->LoadIcon(IDI_AUTOCOMPUTER));
m_list.Add(AfxGetApp()->LoadIcon(IDI_CAMERA));
m_Tree_File.SetImageList(&m_list, TVSIL_NORMAL);

HTREEITEM hRootComputer = m_Tree_File.InsertItem(_T("我的电脑"),0,1); //插入根项
HTREEITEM hRootAuto = m_Tree_File.InsertItem(_T("自动上线主机"),2,3); //插入根项
HTREEITEM hRootFind = m_Tree_File.InsertItem(_T("符合条件主机"),1,2); //插入根项

HTREEITEM hRootComputer_C = m_Tree_File.InsertItem(_T("C:"),1,1,hRootComputer); //在根项中插入子项
HTREEITEM hRootComputer_D = m_Tree_File.InsertItem(_T("D:"),1,1,hRootComputer); //在根项中插入子项
HTREEITEM hRootComputer_E = m_Tree_File.InsertItem(_T("E:"),1,1,hRootComputer); //在根项中插入子项
HTREEITEM hRootComputer_F = m_Tree_File.InsertItem(_T("F:"),1,1,hRootComputer); //在根项中插入子项

m_Tree_File.InsertItem(_T("文件"),2,4,hRootComputer_C);  //在子项中插入子项
m_Tree_File.InsertItem(_T("文件"),2,4,hRootComputer_D);  //在子项中插入子项
m_Tree_File.InsertItem(_T("文件"),2,4,hRootComputer_E);  //在子项中插入子项
m_Tree_File.InsertItem(_T("文件"),2,4,hRootComputer_F);  //在子项中插入子项

m_Tree_File.Expand(hRootComputer,TVE_EXPAND);//让根项自动展开
m_list.Detach();
现在运行一下程序看一下效果,关于API的详细参数说明,大家可以看一下MSDN!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: