function GetNodeByText
(ATree : TsTreeView; AValue:
String;
AVisible: Boolean): TTreeNode;
var
Node: TTreeNode;
begin
Result :=
nil;
if ATree.Items.Count = 0
then Exit;
Node := ATree.Items[0];
while Node <>
nil do
begin
if UpperCase(Node.Text) = UpperCase(AValue)
then
begin
Result := Node;
if AVisible
then
Result.MakeVisible;
Break;
end;
Node := Node.GetNext;
end;
end;
procedure StreamToTreeNode(
var Tree : TsTreeView; TreeStream : TMemoryStream);
procedure ProcessTreeItem(
var Node : TTreeNode; TreeStream : TMemoryStream);
var
ChildNode : TTreeNode;
TextLength, HasChildren, ImageIndex, SelectedIndex : Integer;
NodeName :
String;
begin
while TreeStream.Position < TreeStream.Size
do
begin
TreeStream.
Read(TextLength, SizeOf(Integer));
TreeStream.
Read(NodeName, TextLength);
TreeStream.
Read(ImageIndex, SizeOf(Integer));
TreeStream.
Read(SelectedIndex, SizeOf(Integer));
TreeStream.
Read(HasChildren, SizeOf(Integer));
//make new child node
ChildNode:= Tree.Items.AddChild(Node,NodeName);
ChildNode.ImageIndex := ImageIndex;
ChildNode.SelectedIndex := SelectedIndex;
ChildNode.HasChildren := Bool(HasChildren);
end;
end;
(*ProcessTreeItem*)
var
Node : TTreeNode;
TextLength, HasChildren, ImageIndex, SelectedIndex : Integer;
NodeName :
String;
begin
//get first node from stream:
TreeStream.
Read(TextLength, SizeOf(Integer));
TreeStream.
Read(NodeName, TextLength);
TreeStream.
Read(ImageIndex, SizeOf(Integer));
TreeStream.
Read(SelectedIndex, SizeOf(Integer));
TreeStream.
Read(HasChildren, SizeOf(Integer));
//check if node exists in treeview:
Node := GetNodeByText(Tree, NodeName, True);
//node found:
if Node <>
nil then
begin
//clear childs:
Node.DeleteChildren;
//add new childs:
ProcessTreeItem(Node, TreeStream)
end
else begin
//node doesn't exist:
Node:= Tree.Items.Add(
nil,NodeName);
//Root node
Node.ImageIndex := ImageIndex;
Node.SelectedIndex := SelectedIndex;
Node.HasChildren := Bool(HasChildren);
//if stream have more data:
if TreeStream.Position < TreeStream.Size
then
ProcessTreeItem(Node, TreeStream);
end;
end;
procedure TreeNodeToStream(TreeNode : TTreeNode;
var TreeStream : TMemoryStream);
procedure ProcessTreeItem(Node : TTreeNode;
var TreeStream : TMemoryStream);
var
TextLength, HasChildren : Integer;
begin
if (Node =
nil)
then Exit;
TextLength := Length(Node.Text);
TreeStream.
Write(TextLength, SizeOf(Integer));
TreeStream.
Write(Node.Text, TextLength);
TreeStream.
Write(Node.ImageIndex, SizeOf(Integer));
TreeStream.
Write(Node.SelectedIndex, SizeOf(Integer));
HasChildren := Integer(Node.HasChildren);
TreeStream.
Write(HasChildren, SizeOf(Integer));
end;
(*ProcessTreeItem*)
var
i : Integer;
begin
if TreeNode <>
NIL then
begin
//Handle Node itself
ProcessTreeItem(TreeNode, TreeStream);
//Handle Childs of the Node
for i := 0
to TreeNode.Count - 1
do
ProcessTreeItem (TreeNode.Item[i], TreeStream);
end;
end;