谢谢 zjan521:
知道用TStream.WriteComponent ReadComponent这种方法,但是不会写啊!我写了一部分,但是写两个以上并读取的时候出错了,它提示说没有父窗体!麻烦你和各位高手帮我更正一下,谢谢!再次谢谢了!
procedure TMainForm.SaveFileActExecute(Sender: TObject);
var
OutPut: TMemoryStream;
ResName:string;
I,J:Integer;
begin
ResName:='c:/myshow.dat';
If FileExists(ResName) Then DeleteFile(ResName);
Output:=TMemoryStream.Create;
For I:=0 to ProxyForm.FProxyCompList.Count-1 do
Begin
OutPut.WriteComponent(ProxyForm.FProxyCompList.Items);
With (ProxyForm.FProxyCompList.Items As TBGPanel) Do
Begin
For J:=0 to CompList.Count-1 do
Begin
OutPut.WriteComponent(CompList.Items[J]);
End;
End;
End;
Output.SaveToFile(ResName);
Output.Free;
end;
//写控件是没有出错的,但是读就有错了!
procedure TMainForm.OpenFileActExecute(Sender: TObject);
var
Input: TMemoryStream;
ResName:string;
TmpCmpt,ChildCmpt:TComponent;
begin
RegisterClass(TBGPanel);
RegisterClass(TMoveImagePanel);
RegisterClass(TMoveMovicePanel);
RegisterClass(TMoveRichEditPanel);
RegisterClass(TTimePanel);
ResName:='c:/myshow.dat';
If Not FileExists(ResName) Then Exit;
Input:=TMemoryStream.Create;
Input.LoadFromFile(ResName);
Input.Position:=0;
TmpCmpt:=Input.ReadComponent(Nil); //这里就开始出错了
While TmpCmpt<>Nil Do
Begin
If TmpCmpt.ClassType=TBGPanel Then
Begin
ProxyForm.InsertComponent(TmpCmpt);
(TmpCmpt as TBGPanel).Parent:=ProxyForm;
ProxyForm.FProxyCompList.Add(TmpCmpt);
ChildCmpt:=Input.ReadComponent(Nil);
While (ChildCmpt<>Nil) and (ChildCmpt.ClassType<>TBGPanel) Do
Begin
TmpCmpt.InsertComponent(ChildCmpt);
(ChildCmpt as TWinControl).Parent:=(TmpCmpt as TBGPanel);
(TmpCmpt as TBGPanel).CompList.Add(ChildCmpt);
ChildCmpt:=Input.ReadComponent(Nil);
End;
If (ChildCmpt<>Nil) and (ChildCmpt.ClassType=TBGPanel) Then
TmpCmpt:=ChildCmpt;
End;
End;
Input.Free;
ShowMessage('Over!');
end;
{
注:FProxyCompList:TComponentList; 在ProxyForm中每动态增加一个控件就用
FProxyCompList.add(NewComponent);来把控件增加到控件列表中
Property CompList:TComponentList read FCompList write FCompList;
以下是我自定义的控件,他们都继承了TPanel类
RegisterClass(TBGPanel);
RegisterClass(TMoveImagePanel);
RegisterClass(TMoveMovicePanel);
RegisterClass(TMoveRichEditPanel);
RegisterClass(TTimePanel);}