📄 treeview_unit.pas
字号:
unit treeview_Unit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, FileCtrl, ComCtrls;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
DriveComboBox1: TDriveComboBox;
FileListBox1: TFileListBox;
procedure FormCreate(Sender: TObject);
procedure TreeView1Expanding(Sender: TObject; Node: TTreeNode;
var AllowExpansion: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var firstnode,dirnode:ttreenode;
itemcount,index:integer;
itemstr:string;
begin
itemcount:=DriveComboBox1.Items.Count;
firstnode:=treeview1.Items.GetFirstNode;
for index:=0 to itemcount-1 do
begin
itemstr:=DriveComboBox1.Items[index];
itemstr:=copy(itemstr,1,pos(':',itemstr));
dirnode:=treeview1.Items.AddChild(firstnode,itemstr);
dirnode.HasChildren:=true;
dirnode.SelectedIndex:=1;
end;
end;
procedure TForm1.TreeView1Expanding(Sender: TObject; Node: TTreeNode;
var AllowExpansion: Boolean);
var
dirnode:ttreenode;
itemcount,index,level,icount:integer;
itemstr,strpath:string;
begin
if node.Count = 0 then
begin
icount:=0;
level:=node.Level ;
dirnode:=node;
strPath:=node.Text+'\' ;
while level<0 do
begin
strPath:=dirnode.Parent.Text+'\'+strpath;
dirnode:=dirnode.parent;
level :=level -1;
end;
FileListBox1.Clear ;
FileListBox1.Directory := strpath;
ItemCount:= FileListBox1.Items.Count;
for index:=0 to ItemCount -1 do
begin
itemstr:=filelistbox1.items[index];
itemstr:= copy(ItemStr,2,pos(']',ItemStr)-2) ;
if (itemstr<>'.') and (itemstr<>'..') then
begin
DirNode :=TreeView1.Items.AddChild(Node,itemstr );
DirNode.HasChildren :=true;
DirNode.ImageIndex := 0;
DirNode.SelectedIndex := 1;
icount:=icount+1;
end;
if icount = 0 then
Node.HasChildren := false;
end;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -