如何将完全路径以资源管理器方式放入树控件里??

kaste 2005-11-17 05:37:54
各位大侠:
请问如保实现将以下 [列表1] 字等串按放在 [树1] 树控件里,能否提供实现代码。
谢谢!

[列表1]
a/b1.exe
a/b/c1.exe
a/b/c/d/e/f/g1.exe
a/b/c/d/e/f/g2.exe
a/b/c/d/e/f/g3.exe
a/b/c/d/e/f/g/h1.asp
a/b/c/d/e/f/g/h2.asp
a/c/c/d/e/f/g/k1.asp
a/c/c/d/e/f/g/k2.asp
a/c/d/d/e/f/y1.htm


[树1]
a
├ b1.exe
├ b
│ ├ c1.exe
│ ├ d
│ ├ e
│ ├ f
│ ├ g1.exe
│ ├ g2.exe
│ ├ g3.exe
│ ├ g
│ ├ h1.exe
│ ├ h2.exe
├ c
├ c
│ ├ d
│ ├ e
│ ├ f
│ ├ g
│ ├ k1.asp
│ ├ k2.asp
├ d
├ e
├ f
├ y1.htm
...全文
207 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
kaste 2005-11-22
  • 打赏
  • 举报
回复
拿位大侠帮帮忙呀!!
Snow_Ice11111 2005-11-22
  • 打赏
  • 举报
回复
至于查找有没有子项的方法,以第一个“a/b1.exe”为例:
HTREEITEM m_hRoot,hChild;
m_hRoot = m_tree.InsertItem("a"); //添加根项a
hChild=m_tree.GetChildItem(m_hRoot);
while (hChildItem != NULL)
{
if (m_tree.GetItemText(hChild)=="b1.exe") return;
hChild=GetNextItem(m_hRoot, TVGN_NEXT);
}
m_tree.InsertItem("b1.exe",m_hRoot);
return;
Snow_Ice11111 2005-11-22
  • 打赏
  • 举报
回复
惭愧,现在才弄懂你题目真正的意思。这个可以对列表中的每个字符串逐个字符进行解析,如字符串出现了斜杠/的话,就查找树形控件对应位置有没有这个子项,没有的话就在指定地方添加一个子项,名称为两个斜杠之间出现的字符串,有的话就继续解析后面的字符,直到字符串的结束。
kaste 2005-11-21
  • 打赏
  • 举报
回复
我上面的列表和树只是举个例子,不可能这样一个一个插入,因为我的列表的长度以及树的深度都是末知的。我需要的是一个算法,而不是对上面例子的实现。
Snow_Ice11111 2005-11-20
  • 打赏
  • 举报
回复
其实对Tree Control和List Control这两个控件的使用方法我也是从前面给的地址中下到的源代码上学到的,目前还不十分熟练,但已经基本掌握了方法了,呵呵^_^
Snow_Ice11111 2005-11-20
  • 打赏
  • 举报
回复
抱歉,昨天我的回复语气颇不友善,在此向楼主致歉!奉上实现的步骤和代码,以示歉意!!
下面是实现你的要求的步骤:
1 在界面上放一个Tree control,并通过MFC ClassWizard为它绑定一个CTreeCtrl类型的变量m_tree,再放一个按钮;
2 在对话框的OnInitDialog()函数中加上如下代码:
// TODO: Add extra initialization here
DWORD dwStyle = GetWindowLong(m_tree.m_hWnd,GWL_STYLE); //要自己添加的
dwStyle |= TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT; //要自己添加的
SetWindowLong(m_tree.m_hWnd,GWL_STYLE,dwStyle); //要自己添加的

return TRUE; // return TRUE unless you set the focus to a control
3 在按钮响应函数中加上如下代码:
void CMMDlg::OnButton1()
{
// TODO: Add your control notification handler code here
HTREEITEM m_hRoot,hChild,hChild1; //定义几个HTREEITEM变量用来保存子项的位置,作用有点象书签一样
m_hRoot = m_tree.InsertItem("a"); //这里是设置TreeCtrl根项的名称
m_tree.InsertItem("b1.exe",m_hRoot); //在m_hRoot所在的项中添加一个子项
hChild=m_tree.InsertItem("b",m_hRoot); //在m_hRoot所在的项中添加一个子项,并把该项的地址赋给hChild
m_tree.InsertItem("c1.exe",hChild); //在hChild所在的项中添加子项
hChild=m_tree.InsertItem("d",hChild);
hChild=m_tree.InsertItem("e",hChild);
hChild=m_tree.InsertItem("f",hChild);
m_tree.InsertItem("g1.exe",hChild);
m_tree.InsertItem("g2.exe",hChild);
m_tree.InsertItem("g3.exe",hChild);
hChild=m_tree.InsertItem("g",hChild);
m_tree.InsertItem("h1.exe",hChild);
m_tree.InsertItem("h2.exe",hChild);
hChild=m_tree.InsertItem("c",m_hRoot);
hChild1=hChild; //把当前hChild的地址给hChild1,以后方便使用
hChild=m_tree.InsertItem("c",hChild);
hChild=m_tree.InsertItem("d",hChild);
hChild=m_tree.InsertItem("e",hChild);
hChild=m_tree.InsertItem("f",hChild);
hChild=m_tree.InsertItem("g",hChild);
m_tree.InsertItem("k1.asp",hChild);
m_tree.InsertItem("k2.asp",hChild);
hChild=m_tree.InsertItem("d",hChild1);
hChild=m_tree.InsertItem("e",hChild);
hChild=m_tree.InsertItem("f",hChild);
m_tree.InsertItem("y1.htm",hChild);
m_tree.Expand(m_hRoot,TVE_EXPAND); //设置树形控件默认为展开根项显示
}


好了,全部工作完成了,是不是很简单呢!好好研究一下吧,祝你学习进步!!
Snow_Ice11111 2005-11-19
  • 打赏
  • 举报
回复
看看人家程序中添加子项方面的代码,很简单的,研究一下就明白了,这样才能学到知识。不能等人家把完全实现你要的功能现成的代码给你才去研究吧!
kaste 2005-11-18
  • 打赏
  • 举报
回复
我想一次就将这些列表的数据放入树控件中,并不像文件资源管理器那样,展开哪个子树才去遍历,
Snow_Ice11111 2005-11-17
  • 打赏
  • 举报
回复
这里也有一个例子:
http://www.vckbase.com/document/viewdoc/?id=836

有源码可下的。
psbeond 2005-11-17
  • 打赏
  • 举报
回复
这个得遍历文件夹,把遍历的结果插入树中了。VC没有提供现成的控件。
lixiaosan 2005-11-17
  • 打赏
  • 举报
回复
树的结构和列表如何对应的。。
goodboyws 2005-11-17
  • 打赏
  • 举报
回复
http://www.codeguru.com/Cpp/controls/treeview/directorybrowsers/article.php/c717/

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧