Delphi中字符串和控件的相互转化

时间:2010-03-03 17:32:44  来源:第二电脑网  作者:第二电脑网

  第二电脑网导读:function ComponentToString(Component: TComponent): string;varBinStream:TMemoryStream;StrStream: TStringStream;s: string;beginBinStream := TMemoryStream.Create;try   StrStream := TStringStream.Create(s);   try     BinStream.WriteComponent(Component);
  正文:控件与字符串的相互转化可用于控件的保存与恢复.

//控件到字符串
function ComponentToString(Component: TComponent): string;

//字符串到控件
procedure StringToComponent(Value: string; Comp: TComponent);


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;

procedure StringToComponent(Value: string; Comp: TComponent);
var
StrStream:TStringStream;
BinStream: TMemoryStream;
begin
StrStream := TStringStream.Create(Value);
try
   BinStream := TMemoryStream.Create;
   try
     ObjectTextToBinary(StrStream, BinStream);
     BinStream.Seek(0, soFromBeginning);
     BinStream.ReadComponent(Comp);
   finally
     BinStream.Free;
   end;
finally
   StrStream.Free;
end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Lines.Text := ComponentToString(RichEdit1);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
StringToComponent(Memo1.Lines.Text, RichEdit1);
end;

"Delphi中字符串和控件的相互转化"由第二电脑网原创提供,转载请注明:http://www.002pc.com/master/College/Language/Delphi/13033.html


关键字:

关于《Delphi中字符串和控件的相互转化》文章的评论

站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td