使用delphi XE8和VirtualStringTree正确添加,加载和保存节点

时间:2016-05-11 13:35:33

标签: delphi virtualtreeview tvirtualstringtree

我正在使用virtualstringtree来获取基本树视图功能以及复选框等。
我使用了原始文档(从2006年开始),但是在加载我保存的数据时遇到错误,即使虚拟字符串树看起来没问题。

我相信我应该使用InitNode而不是MAddVSTStructure

有人可以建议在这种情况下添加节点的正确方法吗? 这可能是加载过程加载信息的原因,但是在单击已加载的节点时会出现访问冲突。

type
   PTreeData = ^TTreeData;
   TTreeData = record
      FCaption: String;
      FRichText: String;
      FImageIndex: integer;
      FVisibility: integer;
      FUniqueID: integer;
   end;

function MAddVSTStructure(AVST: TCustomVirtualStringTree; ANode: PVirtualNode;  ARecord: TTreeData): PVirtualNode;
var
   Data: PTreeData;
begin
   Result:= AVST.AddChild(ANode);
   Result.CheckType:=ctTriStateCheckBox;
   Data:= AVST.GetNodeData(Result);
   Data^.FCaption:= ARecord.FCaption;
   Data^.FRichText:= ARecord.FRichText;
   Data^.FImageIndex:= ARecord.FImageIndex;
   Data^.FVisibility:= ARecord.FVisibility;
   Data^.FUniqueID:= ARecord.FUniqueID;
   AVST.ValidateNode(Result, False);
end;    

procedure vstSaveNode(Sender: TBaseVirtualTree; Node: PVirtualNode; Stream: TStream);
var
   Data: PTreeData;
   Len: integer;
begin
   Data:= Sender.GetNodeData(Node);

   //FCaption
   Len:= Length(Data.FCaption);
   Stream.Write(Len, SizeOf(Len));
   Stream.Write(PChar(Data.FCaption)^, Len * SizeOf(Char));

   //FRichText
   Len:= Length(Data.FRichText);
   Stream.Write(Len, SizeOf(Len));
   Stream.Write(PChar(Data.FRichText)^, Len * SizeOf(Char));

   //FImageIndex
   Stream.Write(Data.FImageIndex, SizeOf(Data.FImageIndex));

   //FVisibility
   Stream.Write(Data.FVisibility, SizeOf(Data.FVisibility));

   //FUniqueID
   Stream.Write(Data.FUniqueID, SizeOf(Data.FUniqueID));

end;
procedure vstLoadNode(Sender: TBaseVirtualTree; Node: PVirtualNode; Stream: TStream);
var
   Data: PTreeData;
   Len: Integer;
begin
   Data:= Sender.GetNodeData(Node);
   //Len:= Length(Data.FCaption);

   //FCaption
   Stream.Read(Len, SizeOf(Len));
   SetLength(Data.FCaption, Len);
   Stream.Read(PChar(Data.FCaption)^, Len * SizeOf(Char) );

   //FRichText
   Stream.Read(Len, SizeOf(Len));
   SetLength(Data.FRichText, Len);
   Stream.Read(PChar(Data.FRichText)^, Len * SizeOf(Char));

   //FImageIndex
   Stream.Read(PChar(Data.FImageIndex)^, SizeOf(Data.FImageIndex));

   //FVisibility
   Stream.Read(PChar(Data.FVisibility)^, SizeOf(Data.FVisibility));

   //FUniqueID
   Stream.Read(PChar(Data.FUniqueID)^, SizeOf(Data.FUniqueID));

end;

0 个答案:

没有答案