//显示任意一个目录的文件到TReeview中
unit uTreeViewDemo;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, FileCtrl;
const
SELDIRHELP = 1000;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
procedure GetDirectories(Tree: TTreeView; Directory: string; Item: TTreeNode;
IncludeFiles: Boolean);
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.GetDirectories(Tree: TTreeView; Directory: string; Item: TTreeNode;
IncludeFiles: Boolean);
var
SearchRec: TSearchRec;
ItemTemp: TTreeNode;
begin
TreeView1.Items.BeginUpdate;
if Directory[Length(Directory)] '\' then
Directory := Directory + '\';
if FindFirst(Directory + '*.*' , faDirectory, SearchRec) = 0 then
begin
repeat
if (SearchRec.Attr and faDirectory = faDirectory) and (SearchRec.Name[1] '.') then
begin
if (SearchRec.Attr and faDirectory > 0) then
Item := Tree.Items.AddChild(Item, SearchRec.Name);
ItemTemp := Item.Parent;
GetDirectories(Tree, Directory + SearchRec.Name, Item, IncludeFiles);
Item := ItemTemp;
end
else
if IncludeFiles then
if SearchRec.Name[1] '.' then
Tree.Items.AddChild(Item, SearchRec.Name);
until FindNext(SearchRec) 0;
FindClose(SearchRec);
TreeView1.Items.EndUpdate;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
strOpenDir: string;
begin
if SelectDirectory(strOpenDir, [sdAllowCreate, sdPerformCreate, sdPrompt],SELDIRHELP) then
try
GetDirectories(TreeView1, strOpenDir, nil, True);
except
ShowMessage('错误信息');
end;
end;
end.
2021-09-09 17:14:47
200KB
控件使用类
1