classes单元
procedure TWriter.WriteData(Instance: TComponent);
if not IgnoreChildren then
try
if (FAncestor <> nil) and (FAncestor is TComponent) then
begin
if (FAncestor is TComponent) and (csInline in TComponent(FAncestor).ComponentState) then
FRootAncestor := TComponent(FAncestor);
FAncestorList := TList.Create;
TComponent(FAncestor).GetChildren(AddAncestor, FRootAncestor);
end;
if csInline in Instance.ComponentState then
FRoot := Instance;
Instance.GetChildren(WriteComponent, FRoot);
finally
FAncestorList.Free;
end;
这里负责写子对象
controls单元
procedure TWinControl.GetChildren(Proc: TGetChildProc; Root: TComponent);
var
I: Integer;
Control: TControl;
begin
for I := 0 to ControlCount - 1 do
begin
Control := Controls[I];
if Control.Owner = Root then Proc(Control);
end;
end;
这里负责判断,是否要把WinControl的子Control作为子对象写入流中
判断条件是Control.Owner = Root
你最初的代码,输入的Root等于Panel,但是Panel上的子Control的Owner,实际上是属于Form的
所以判断失败,没有写入
你可以重载一个新的Panel类,然后把GetChildren方法改写掉
procedure TMyPanel.GetChildren(Proc: TGetChildProc; Root: TComponent);
var
I: Integer;
Control: TControl;
begin
for I := 0 to ControlCount - 1 do
begin
Control := Controls[I];
if (Control.Owner = Root) or (Self = Root) then
Proc(Control);
end;
end;
这样应该就可以利用Delphi自己的标准读写过程了
但是,你在流中只读写了Panel以下的组件,如果这些组件的某些属性引用到Panel以外的组件
那么必然导致属性装配失败
DISSENT IS THE HIGHEST FORM OF PATRIOTISM !
--Thomas Jefferson