我正在尝试将项目从D6移至D-XE3。使用VirtualStringTree版本5.10在OnSaveNode和OnLoadEvents中保存和加载树数据时,我得到了垃圾。我可能无法正确处理Unicode,但是我可能还有其他一些无知之处:

procedure TfMain.vstGridSaveNode(Sender: TBaseVirtualTree; Node: PVirtualNode;
  Stream: TStream);
var
  Data: PStkData;
begin
  Data := Sender.GetNodeData(Node);

  //  Owned: boolean;
  Stream.Write(Data.Owned, SizeOf(boolean) );

  //  Symbol: string;
  Stream.Write(PChar(Data.Symbol)^, Length(Data.Symbol) * SizeOf(Char));

  //  AvgTarget: currency;
  //Stream.Write(Data.AvgTarget, SizeOf(currency));

  //  PE: double;
  Stream.Write(Data.PE, SizeOf(double));
end;

procedure TfMain.vstGridLoadNode(Sender: TBaseVirtualTree; Node: PVirtualNode;
  Stream: TStream);
var
  Data: PStkData;
begin
  Data := Sender.GetNodeData(Node);

  //Owned: boolean;
  Stream.Read(Data.Owned, SizeOf(boolean));

  //Symbol: string;
  Stream.Read(PChar(Data.Symbol)^, Length(Data.Symbol) * SizeOf(Char));

  //AvgTarget: currency;
  Stream.Read(Data.AvgTarget, SizeOf(currency));

  //PE: double;
  Stream.Read(Data.PE, SizeOf(double));
end;


谢谢你的帮助。

最佳答案

写入字符数据时,需要确保以某种方式写入它,以使您知道在加载时要再次读取多少数据。您目前仅写字符数据,因此您不知道以后需要再读多少。相反,您假设Symbol已经是正确的长度,现在我已经指出了这一点,您可能意识到这是一个无效的假设。

编写字符串时,请先编写其长度,以便在加载时可以读取它:

var
  SymbolLen: Integer;

SymbolLen := Length(Data.Symbol);
Stream.Write(SymbolLen, SizeOf(SymbolLen));
Stream.Write(PChar(Data.Symbol)^, Length(Data.Symbol) * SizeOf(Data.Symbol[1]));


然后您可以阅读它:

Stream.Read(SymbolLen, SizeOf(SymbolLen));
SetLength(Data.Symbol, SymbolLen);
Stream.Read(PChar(Data.Symbol)^, SymbolLen * SizeOf(Data.Symbol[1]));

关于delphi - 保存VirtualStringTree节点数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14713885/

10-10 16:55
查看更多