请问各位高手,如何存储一个含子控件的控件到文件里,然后再读出来,不是分别存、读?(100分)

  • 主题发起人 c_yd2000
  • 开始时间
C

c_yd2000

Unregistered / Unconfirmed
GUEST, unregistred user!
请问各位高手,如何存储一个含子控件的控件到文件里,然后再读出来,不是分别存、读?
 
function ComponentToString(Component: TComponent): string;

var
BinStream:TMemoryStream;
StrStream: TStringStream;
s: string;
begin
BinStream := TMemoryStream.Create;
try
StrStream := TStringStream.Create(s);
try
BinStream.WriteComponent(Component);
BinStream.Seek(0, soFromBeginning);
ObjectBinaryToText(BinStream, StrStream);
StrStream.Seek(0, soFromBeginning);
Result:= StrStream.DataString;
finally
StrStream.Free;

end;
finally
BinStream.Free
end;
end;

function StringToComponent(Value: string): TComponent;
var
StrStream:TStringStream;
BinStream: TMemoryStream;
begin
StrStream := TStringStream.Create(Value);
try
BinStream := TMemoryStream.Create;
try
ObjectTextToBinary(StrStream, BinStream);
BinStream.Seek(0, soFromBeginning);
Result := BinStream.ReadComponent(nil);

finally
BinStream.Free;
end;
finally
StrStream.Free;
end;
end;
 
to yyanghhong:
读出控件时提示:class T.... not fount.即第二个控件的类找不到。其中第二个控件
为 非 Twincontrol 类。
 
because the child component is referenced by parent component,
not a part of parent component, parent component only contain a pointer
of his child component.
so you have to convert the child to a string first,
then convert the parent. then save two string to a file.
when you like to load them from file, load the child first, then load the parent

 
接受答案了.
 
顶部