已我理解的规则 是把tree完全展开
自上而下生成的子串
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
RIndo = record
Level: Integer;
Text: String;
end;
PIndo = ^RIndo;
TForm1 = class(TForm)
TreeView1: TTreeView;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
Nodes: array of TTreeNode;
{ Private declarations }
procedure CreateTree(p_Ptr: PIndo);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
v_Str: String;
v_Strs: TStringList;
v_Count: Integer;
v_Ptr: PIndo;
begin
v_Str := '0:画面a|1:画面aa|2:画面aaa|3:画面aaaa|2:画面aab|1:画面ab|0:画面b|1:画面ba|1:画面bb';
v_Strs := TStringList.Create;
v_Strs.Delimiter := '|';
v_Strs.DelimitedText := v_Str;
for v_Count := 0 to v_Strs.Count - 1 do
begin
New(v_Ptr);
v_Ptr^.Level := StrToInt(Copy(v_Strs.Strings[v_Count], 1, Pos(':', v_Strs.Strings[v_Count]) - 1));
v_Ptr^.Text := Copy(v_Strs.Strings[v_Count], Pos(':', v_Strs.Strings[v_Count]) + 1, Length(v_Strs.Strings[v_Count]));
v_Strs.Objects[v_Count] := TObject(v_Ptr);
end;
SetLength(Nodes, v_Strs.Count);
for v_Count := 0 to v_Strs.Count - 1 do
begin
CreateTree(PIndo(v_Strs.Objects[v_Count]));
Dispose(PIndo(v_Strs.Objects[v_Count]));
end;
v_Strs.Free;
end;
procedure TForm1.CreateTree(p_Ptr: PIndo);
var
v_Count: Integer;
begin
for v_Count := p_Ptr.Level to Length(Nodes) - 1 do
begin
if Nodes[v_Count]<> nil then
Nodes[v_Count] := nil
else
Break;
end;
if p_Ptr.Level = 0 then
begin
Nodes[p_Ptr.Level] := TreeView1.Items.Add(nil, p_Ptr.Text);
end else
begin
Nodes[p_Ptr.Level] := TreeView1.Items.AddChild(Nodes[p_Ptr.Level - 1], p_Ptr.Text);
Nodes[p_Ptr.Level - 1].Expanded := True;
end;
end;
end.