uses
Windows, Messages, SysUtils, Variants,
Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, Grids;
type
TForm1 = class(TForm)
TreeView1:
TTreeView;
StringGrid1:
TStringGrid;
procedure
FormCreate(Sender: TObject);
procedure
TreeView1Change(Sender: TObject; Node: TTreeNode);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
Nodes: TTreeNodes;
node: TTreeNode;
begin
{初始化 TreeView1}
TreeView1.Align := alLeft;
Nodes := TreeView1.Items;
node := Nodes.Add(nil, '一连长');
node := Nodes.AddChild(node, '一排长');
node := Nodes.AddChild(node, '一班长');
node := Nodes.AddChild(node, '战士1');
Nodes.Add(node, '战士2');
Nodes.Add(node, '战士3');
Nodes.Add(node, '战士4');
node := node.Parent;
Nodes.Add(node, '二班长');
Nodes.Add(node, '三班长');
node := node.Parent;
Nodes.Add(node, '二排长');
Nodes.Add(node, '三排长');
node := node.Parent;
Nodes.Add(node, '二连长');
Nodes.Add(node, '三连长');
{初始化 StringGrid1}
with StringGrid1 do begin
Align := alClient;
FixedRows := 0;
ColCount := 2;
ColWidths[0] :=
78;
ColWidths[1] :=
50;
DefaultRowHeight :=
18;
RowCount := 15;
Cells[0,0]
:= '当前选择';
Cells[0,1]
:= '序号';
Cells[0,2]
:= '所在级别';
Cells[0,3]
:= '在兄弟中排行';
Cells[0,4]
:= '下级总数';
Cells[0,5]
:= '上级元素';
Cells[0,6]
:= '上一个';
Cells[0,7]
:= '下一个';
Cells[0,8]
:= '上一个兄弟';
Cells[0,9]
:= '下一个兄弟';
Cells[0,10] :=
'上一个可见';
Cells[0,11] :=
'下一个可见';
Cells[0,12] :=
'第一个下级';
Cells[0,13] :=
'最后一个下级';
Cells[0,14] :=
'总数';
end;
end;
procedure TForm1.TreeView1Change(Sender: TObject; Node:
TTreeNode);
begin
with StringGrid1 do begin
{清除第二列的数据}
StringGrid1.Cols[1].Clear;
{当前选择}
Cells[1,0] :=
Node.Text;
{序号; AbsoluteIndex
是绝对序号}
Cells[1,1] :=
IntToStr(Node.AbsoluteIndex);
{所在级别}
Cells[1,2] :=
IntToStr(Node.Level);
{在兄弟中排行}
Cells[1,3] :=
IntToStr(Node.Index);
{下级总数}
Cells[1,4] :=
IntToStr(Node.Count);
{上级元素}
if Boolean(Node.Parent)
then Cells[1,5] := Node.Parent.Text;
{上一个}
if Boolean(Node.GetPrev)
then Cells[1,6] := Node.GetPrev.Text;
{下一个}
if Boolean(Node.GetNext)
then Cells[1,7] := Node.GetNext.Text;
{上一个兄弟}
if
Boolean(Node.getPrevSibling) then Cells[1,8] :=
Node.getPrevSibling.Text;
{下一个兄弟}
if
Boolean(Node.getNextSibling) then Cells[1,9] :=
Node.getNextSibling.Text;
{上一个可见}
if
Boolean(Node.GetPrevVisible) then Cells[1,10] :=
Node.GetPrevVisible.Text;
{下一个可见}
if
Boolean(Node.GetNextVisible) then Cells[1,11] :=
Node.GetNextVisible.Text;
{第一个下级}
if Node.HasChildren then
Cells[1,12] := Node.getFirstChild.Text;
{最后一个下级}
if Node.HasChildren then
Cells[1,13] := Node.GetLastChild.Text;
{总数}
Cells[1,14] :=
IntToStr(Node.Owner.Count);
end;
end;
end.