将指定目录的结构装入TreeView中 2000-11-21 00:00:00·
符东亮·yesky
上一页 1 2 3 4 下一页
将自定义函数BrowDir()添加到程序中,然后双击Button1组件,在它的OnClick事件中加入:
//设置光标为漏斗 Screen-$#@62;Cursor=crHourGlass; //激活Animate Animate1-$#@62;Active=true; AnsiString Path=Edit1-$#@62;Text; //如果Path最后一个字符不是“\”就在后面加上“\” if(Path.SubString(Path.Length(),1)!="\\")
Path+="\\";
BrowDir(TreeView1-$#@62;Items,Path,TreeView1-$#@62;Items-$#@62;Add(NULL,Path)); //设置光标为正常状态 Screen-$#@62;Cursor=crDefault; //关闭Animate Animate1-$#@62;Active=false;
在Button2的OnClick事件中加入:
TreeView1-$#@62;Items-$#@62;Clear(); TreeView2-$#@62;Items-$#@62;Clear();
按F9编译运行,点击“装载TreeView1”按钮,过一会儿TreeView1组件就会出现C盘目录树的结构。 这种方法的优点是打开子节点的速度快,缺点就是遍历目录时,当子目录和文件越多,遍历时所需的时间就越长。用这样例子来做资源管理器,显然是不行的。 我们都知道,TreeView组件有一个OnChange事件,当TreeView组件的节点发生改变的时候就会发生该事件。若在该事件中加入相应的代码,把改变的节点所表示目录下的子目录添加到TreeView组件中,这样,程序运行时速度就会很快。 这种方法实现步骤如下: 往窗体Form1上再添加一个Button组件和一个TreeView组件,它们的Name属性分别为:Button3和TreeView2。把Button3的Caption属性改为“装载TreeView2”,然后双击Button3组件,在Button3的OnClick事件中加入以下代码:
AnsiString Path=Edit1-$#@62;Text; if(Path.SubString(Path.Length(),1)!="\\")
Path+="\\";
TreeView2-$#@62;Items-$#@62;Add(NULL,Path);
在TreeView2的OnChangeing事件中加入:
Screen-$#@62;Cursor=crHourGlass; Animate1-$#@62;Active=true; //防止重复增加节点 if(Node-$#@62;Count==0) {
TSearchRec sr; AnsiString DirName,DirTmp; TTreeNode * NodeTmp=Node; DirName=Node-$#@62;Text; //得到完整的路径 for(int I=Node-$#@62;Level ;I$#@62;0 ;I--) {
NodeTmp=NodeTmp-$#@62;Parent; DirTmp=NodeTmp-$#@62;Text; if(DirTmp.SubString(DirTmp.Length(),1)!="\\")
DirTmp+="\\";
DirName.Insert(DirTmp,0);
} if(DirName.SubString(DirName.Length(),1)!="\\")
DirName+="\\";
if (FindFirst(DirName+"*.*", faAnyFile, sr) == 0) {
do {
if((sr.Attr & faDirectory) && sr.Name!="." &&
sr.Name!="..") {
TreeView2-$#@62;Items-$#@62;AddChild(Node,sr.Name);
}
} while (FindNext(sr) == 0); FindClose(sr);
}
} Screen-$#@62;Cursor=crDefault; Animate1-$#@62;Active=false;
上一页 1 2 3 4 下一页 |