有关保存控件的问题?(200分)

  • 主题发起人 主题发起人 meckyhan
  • 开始时间 开始时间
M

meckyhan

Unregistered / Unconfirmed
GUEST, unregistred user!
有这么一个问题: 在程序运行期间我动态地创建了几个控件,并且将控件的某些时间处理过程
指向了一些函数或过程,现在想做的事情是如何将这些控件保存到一个文件,并且能在其他的窗口中
打开这个文件,自动创建这些控件,请指教,谢谢
 
用一个xx:=TStringList.Create
保存你创建的控件名StringList.Add(componentname);
TStringList.SaveToFile保存,
另外一个程序TStringList.LoadFromFile
分析StringList既控件名,创建.
 
学习,帮你up
 
要根據你所建建的控件是否可見,創建后哪些屬性已經修改,將這些屬性分別記錄下來,
下次運行時先創建再載入這些屬性.
可以用Ini文件記錄這些屬性.
 
你窗体关闭前保存控件的属性在下次窗体创建时恢复即可,一般可纪录
控件的如下属性:
Caption
Left
Top
Width
Height
Parent
Color
Font
Name
其它你感兴趣的属性
 
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
freeandnil(StrStream);

end;
finally
freeandnil(BinStream)
end;
end;
 
Converts the binary representation of an object into more readily understandable text.

Unit

Classes

Category

streaming utilities

procedure ObjectBinaryToText(Input, Output: TStream); overload;
procedure ObjectBinaryToText(Input, Output: TStream; var OriginalFormat: TStreamOriginalFormat); overload;

Description

Call ObjectBinaryToText to convert the representation of an object as it is saved in files and memory streams into a text version that human readers can understand. ObjectBinaryToText creates a TReader object for the Input stream, and uses this to read the saved representation of the object. A symbolic text representation is then written to the Output stream. OriginalFormat indicates the format in which the object was saved.

Use the WriteComponent method of the Input stream to write a binary representation before calling ObjectBinaryToText.
 
ObjectBinaryToText的帮助有例子
 
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;
 
ReadCompoentResFile & WriteComponentResFile
 
后退
顶部