该问题问过好多遍了,这是COPY下来的:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, Menus;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
ListView1: TListView;
Edit1: TEdit;
Button1: TButton;
OpenDialog1: TOpenDialog;
PopupMenu1: TPopupMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
procedure Button1Click(Sender: TObject);
procedure TreeView1Click(Sender: TObject);
procedure N1Click(Sender: TObject);
private
function GetTreePath(CurrentNode:TTreeNode):string;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var i,j,k:integer;
sr: TSearchRec;
Node1:TTreeNode;
s:string;
begin
OpenDialog1.InitialDir :=Edit1.text;
if OpenDialog1.Execute Then
begin
Edit1.Text :=ExtractFilePath(OpenDialog1.Filename);
TreeView1.Items.Clear;
Node1:=TreeView1.Items.Add(nil,Edit1.text);
i:=0;
while i'.') and (sr.name<>'..') then
TreeView1.items.AddChild(node1,sr.name+'/');
k:=FindNext(sr);
end;
if j=0 then FindClose(sr);
i:=i+1;
end;
TreeView1.FullExpand;
end;
end;
function TForm1.GetTreePath(CurrentNode:TTreeNode):string;
var
ParentNode:TTreeNode;
begin
result:=CurrentNode.text;
ParentNode:=CurrentNode.Parent;
while ParentNode<>nil do
begin
result:=ParentNode.text+result;
ParentNode:=ParentNode.Parent;
end;
end;
procedure TForm1.TreeView1Click(Sender: TObject);
var i,j:integer;
sr: TSearchRec;
Node:TTreeNode;
s:string;
NewColumn: TListItem;
begin
if TreeView1.selected=nil then exit;
s:=GetTreePath(TreeView1.selected)+'*.*';
ListView1.Items.Clear;
j:=FindFirst(s,0,sr);
i:=j;
while i=0 do
begin
NewColumn:=ListView1.items.Add;
NewColumn.Caption:=sr.name;
i:=FindNext(sr);
end;
if j=0 then FindClose(sr);
end;
procedure TForm1.N1Click(Sender: TObject);
var
Node:TTreeNode;
s:string;
begin
With TreeView1 do begin
Node:=Selected;
s:=IntToStr(random(MaxInt));
Node:=items.AddChild(node,s);
Selected:=Node;
end;
end;
end.