delphi 帮助中关于TStream.ReadComponent的例子中两个函数(50分)

  • 主题发起人 主题发起人 wjqdelphi
  • 开始时间 开始时间
W

wjqdelphi

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;

procedure TForm1.Button1Click(Sender: TObject);

begin
s:=ComponentToString(button2);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
StringToComponent(s)
end;
是不是这样用,为什么StringToComponent(s)时总出错!(“class Tbutton not found”)
 
在适当的地方注册StringToConponent涉及到的类
可以在单元的initialization段中
注册方法:
RegisterClass(TButton)
 
接受答案了.
 
后退
顶部