再添一个方法。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
Alevel:integer;
procedure ScanTree1(InNode:TTreeNode);
procedure ScanTree2(InNode:TTreeNode);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
Alevel:=TreeView1.Items[4].Level;
ScanTree2(TreeView1.Items[4]);
end;
procedure TForm1.ScanTree1(InNode: TTreeNode);
var
CurNode:TTreeNode;
begin
CurNode:=InNode.getFirstChild;
while (CurNode<>nil) and (CurNode.Level<>InNode.Level) do begin
Memo1.Lines.Add(CurNode.Text);
CurNode:=CurNode.GetNext;
end;
end;
procedure TForm1.ScanTree2(InNode: TTreeNode);
var
CurNode,temp:TTreeNode;
i:integer;
begin
CurNode:=InNode;
if CurNode<>nil then
if CurNode.Parent<>nil then begin
repeat
Memo2.Lines.Add(CurNode.Text);
if CurNode.HasChildren then
ScanTree2(CurNode.getFirstChild);
CurNode:=CurNode.Parent.GetNextChild(CurNode);
until (CurNode=nil) or (ALevel=CurNode.Level);
end
else ScanTree2(CurNode.getFirstChild);
end;
end.