首页  编辑  

处理窗体代码

Tags: /超级猛料/Friends.网友专栏/zswang/函数大全/   Date Created:

(*//

标题:处理窗体代码

说明:既dfm格式;本函数来源于Delphi帮助

设计:Zswang

日期:2002-01-25

支持:wjhu111@21cn.com

//*)

///////Begin Source

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; { ComponentToString }

function StringToComponent(Value: string; Instance: TComponent): TComponent;

//如果元件是嵌套形式则必须注册子元件类

//参考RegisterClasses();UnRegisterClasses()

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(Instance);

   finally

     BinStream.Free;

   end;

 finally

   StrStream.Free;

 end;

end; { StringToComponent }

///////End Source

///////Begin Demo

procedure TForm1.Button1Click(Sender: TObject);

begin

 Memo1.Text := ComponentToString(Edit1);

end;

procedure TForm1.Button2Click(Sender: TObject);

begin

 StringToComponent(Memo1.Text, Edit1);

end;

///////End Demo