Tstream.WriteComponent ??(100分)

  • 主题发起人 主题发起人 ET外星人
  • 开始时间 开始时间
E

ET外星人

Unregistered / Unconfirmed
GUEST, unregistred user!
DELPHI的
还有Tstream.WriteComponent();
这个方法怎样才可以保存自己继承TComponent的组件?
 
type
Tscript=record
GMscript:TStringlist
end;
///
property s[x,y: Integer]: Tscript read GetS write SetS;
///
form1.s[0,0].GMscript:=TStringlist.create;//这行错误..
//[Error] GM_EtamityMap.pas(259): Left side cannot be assigned to
//为什么??
 
type
TComponent1 = class(TComponent)
private
fS : array[0..5, 0..10] of string ;
function GetS(x, y: Integer): String;
procedure SetS(x, y: Integer
const Value: String);
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
property s[x,y: Integer]: String read GetS write SetS;
published
{ Published declarations }
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Standard', [TComponent1]);
end;

{ TComponent1 }

function TComponent1.GetS(x, y: Integer): String;
begin
if (x in [0..5]) and (y in [0..10]
then Result := fS[x, y]
else Result := '' ;
end;

procedure TComponent1.SetS(x, y: Integer
const Value: String);
begin
if (x in [0..5]) and (y in [0..10] then fS[x, y] := Value ;
end;


This example shows how to load and save an unpublished property whose value is a component that was created at runtime. It defines two methods, LoadCompProperty and StoreCompProperty that load or save the property value. The also defines an override for the DefineProperties method where these methods are passed to the streaming system. The DefineProperties method checks the filer’s Ancestor property to avoid saving a property value in inherited forms.

procedure TSampleComponent.LoadCompProperty(Reader: TReader);
begin
if Reader.ReadBoolean then
MyCompProperty := Reader.ReadComponent(nil);
end;

procedure TSampleComponent.StoreCompProperty(Writer: TWriter);

begin
Writer.WriteBoolean(MyCompProperty <> nil);
if MyCompProperty <> nil then
Writer.WriteComponent(MyCompProperty);
end;

procedure TSampleComponent.DefineProperties(Filer: TFiler);

function DoWrite: Boolean;
begin
if Filer.Ancestor <> nil then { check Ancestor for an inherited value }
begin
if TSampleComponent(Filer.Ancestor).MyCompProperty = nil then
Result := MyCompProperty <> nil
else if MyCompProperty = nil or
TSampleComponent(Filer.Ancestor).MyCompProperty.Name <> MyCompProperty.Name then
Result := True

else Result := False;
end
else { no inherited value -- check for default (nil) value }
Result := MyCompProperty <> nil;
end;
begin
inherited
{ allow base classes to define properties }
Filer.DefineProperty('MyCompProperty', LoadCompProperty, StoreCompProperty, DoWrite);
end;
}
 
上面属性的问题我解决了
var
str: Tstream;
begin
str:=Tfilestream.Create(filename,fmcreate);
str.WriteComponent(listbox1);
str.Free;
end

我现在是想 如果str.WriteComponent(listbox1);不是listbox1
而是自己继承自TComponent的组件叫MYComponent吧
然后
TMYComponent=class(TComponent)
list:Tstringlist;
end;
constructor TMYComponent.Create(aOwner: TComponent);
begin
list:=Tstringlist.create;
end;

var
MYComponent:TMYComponent;
str: Tstream;
begin
MYComponent:=TMYComponent;
MYComponent.list.add('hello! asda ');
str:=Tfilestream.Create(filename,fmcreate);
str.WriteComponent(MYComponent);
str.Free;
end

保存的时候 把MYComponent 包括list 里面的所有属性都保存起来?
要怎样才可以实现? 因为LISTBOX1也是继承TComponent的 但是我不明白
为什么STREAM可以保存LISTBOX1的所有属性?
 
楼主太不厚道了,一个帖子问完一个问题,在得到pascal!的正确答案后,又改个题目继续问,实在是......无言.
 
接受答案了.
 
后退
顶部