unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ContNrs, VirtualTrees;
type
TNamedItem =
class
private
fName:
string;
fItems: TObjectList;
public
constructor Create(
const name:
string);
property Items: TObjectList
read fItems;
property Name:
string read fName
write fName;
end;
TNodeData = TNamedItem;
PNodeData = ^TNodeData;
TForm1 =
class(TForm)
VirtualStringTree1: TVirtualStringTree;
procedure FormCreate(Sender: TObject);
procedure VirtualStringTree1InitNode(Sender: TBaseVirtualTree; ParentNode,
Node: PVirtualNode;
var InitialStates: TVirtualNodeInitStates);
procedure VirtualStringTree1InitChildren(Sender: TBaseVirtualTree;
Node: PVirtualNode;
var ChildCount: Cardinal);
procedure VirtualStringTree1GetText(Sender: TBaseVirtualTree;
Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
var CellText: WideString);
private
{ Private-Deklarationen }
FVstRootItemList: TObjectList;
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
folder1 : TNamedItem;
folder1_1, folder1_2, folder1_3 : TNamedItem;
folder1_1_1 : TNamedItem;
begin
FVstRootItemList := TObjectList.Create;
folder1 := TNamedItem.Create('
Folder 1');
folder1_1 := TNamedItem.Create('
Folder 1.1');
folder1_1_1 := TNamedItem.Create('
Folder 1.1.1');
folder1_1.Items.Add(folder1_1_1);
folder1.Items.Add(folder1_1);
FVstRootItemList.Add(folder1);
folder1 := TNamedItem.Create('
node');
folder1_1 := TNamedItem.Create('
subnode');
folder1_2 := TNamedItem.Create('
subnode');
folder1_3 := TNamedItem.Create('
subnode');
folder1.Items.Add(folder1_1);
folder1.Items.Add(folder1_2);
folder1.Items.Add(folder1_3);
FVstRootItemList.Add(folder1);
folder1 := TNamedItem.Create('
node');
folder1_1 := TNamedItem.Create('
subnode');
folder1_2 := TNamedItem.Create('
subnode');
folder1_3 := TNamedItem.Create('
subnode');
folder1.Items.Add(folder1_1);
folder1.Items.Add(folder1_2);
folder1.Items.Add(folder1_3);
FVstRootItemList.Add(folder1);
VirtualStringTree1.NodeDataSize := SizeOf(TNodeData);
VirtualStringTree1.RootNodeCount := FVstRootItemList.Count;
end;
procedure TForm1.VirtualStringTree1GetText(Sender: TBaseVirtualTree;
Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
var CellText: WideString);
var
data: PNodeData;
begin
data:= Sender.GetNodeData(Node);
if not assigned(data^)
then exit;
case Column
of
0: CellText := data^.
Name;
end;
end;
procedure TForm1.VirtualStringTree1InitChildren(Sender: TBaseVirtualTree;
Node: PVirtualNode;
var ChildCount: Cardinal);
var
Data: PNodeData;
begin
Data := Sender.GetNodeData(Node);
ChildCount := Data^.items.Count;
end;
procedure TForm1.VirtualStringTree1InitNode(Sender: TBaseVirtualTree;
ParentNode, Node: PVirtualNode;
var InitialStates: TVirtualNodeInitStates);
var
data: PNodeData;
ParentData: PNodeData;
begin
data := Sender.GetNodeData(Node);
if VirtualStringTree1.GetNodeLevel(Node) = 0
then
data^ := TNamedItem(FVstRootItemList[Node.
Index])
else
begin
ParentData := Sender.GetNodeData(ParentNode);
data^ := TNamedItem(ParentData.items[Node.
Index]);
end;
if data^.Items.Count > 0
then InitialStates := InitialStates + [ivsHasChildren];
end;
{ TNamedItem }
constructor TNamedItem.Create(
const name:
string);
begin
fName :=
name;
fItems := TObjectList.Create;
end;
end.