Einzelnen Beitrag anzeigen

Benutzerbild von DGL-luke
DGL-luke

Registriert seit: 1. Apr 2005
Ort: Bad Tölz
4.149 Beiträge
 
Delphi 2006 Professional
 
#1

Sub-Komponenten serialisieren

  Alt 30. Aug 2006, 13:51
Hallo, ich habe folgende Konstellation:

Delphi-Quellcode:
type
  TBaseFormElementManager = class abstract(TComponent)
    protected
      FControl: TControl; //<--- der da
      FNaturalDimensions: TDRect;

      MouseUpEvents: TEventList;
      MouseDownEvents: TEventList;
      MouseMoveEvents: TEventList;
    public
      procedure SetElement(Value: TControl); virtual;
      function GetElement: TControl; virtual;

      procedure SetLeftMm(Value: Double);
      procedure SetTopMm(Value: Double);
      procedure SetWidthMm(Value: Double);
      procedure SetHeightMm(Value: Double);

      procedure SetLeftPx(Value: Integer); //<--- hier
      procedure SetTopPx(Value: Integer);
      procedure SetWidthPx(Value: Integer);
      procedure SetHeightPx(Value: Integer);
das will ich speichern und vor allem laden:

Delphi-Quellcode:
procedure TFormElementSupplier.Save(FileName: string);
var i,size: Integer;
    m: TBaseFormElementManager;
    ms,msc: TMemoryStream;
begin
  ms := TMemoryStream.Create;
  msc := TMemoryStream.Create;
  try
    for i := 0 to Elements.Count -1 do
    begin
      ms.Size := 0;
      ms.WriteComponent(Elements[i]);
      ms.Position := 0;
      size := ms.Size;
      msc.Write(size,SizeOf(size));
      msc.CopyFrom(ms, size);
    end;
    msc.Position := 0;
    msc.SaveToFile(FileName);
  finally
    ms.Free;
    msc.Free;
  end;
end;
Delphi-Quellcode:
procedure TFormElementSupplier.Load(FileName: string);
var c: TComponent;
    ms,msc: TMemoryStream;
    size: Integer;
begin
  ms := TMemoryStream.Create;
  msc := TMemoryStream.Create;
  try
    ms.LoadFromFile(FileName);
    while ms.Position < ms.Size do
    begin
      ms.Read(size,SizeOf(size));
      msc.Clear;
      msc.CopyFrom(ms,size);
      msc.Position := 0;
      c := nil;
      c := msc.ReadComponent(nil); //<--- HIER
      Elements.Add(c as TBaseFormElementManager);
    end;
  finally
    ms.Free;
    msc.Free;
  end;
end;
Im ReadComponent wird aber gleich ins SetLeftPx gesprungen, was ich auch nachvollziehen kann:

Delphi-Quellcode:
procedure TBaseFormElementManager.SetLeftPx(Value: Integer);
begin
  if Assigned(FControl) then
    FControl.Left := Value
  else
    raise EElementNotCreated.Create('No Element Created yet.');
end;
Wo dann FControl auf nil ausgewertet wird und die Exception kommt.

Wie bringe ich das ganze System dazu, dass das Control ordentlich zugewiesen wird? Bzw. überhaupt mitgespeichert, ich weiß ja nicht, ob das überhaupt passiert.

Ich sollte vielleicht noch einmal erwähnen, dass TBaseFormElementManager nie instanziiert wird, sondern es sich immer um nachfahren handelt.

EDIT: hab inzwischen "Setsubcomponent" gefunden... ändert nix...
Lukas Erlacher
Suche Grafiktablett. Spenden/Gebrauchtangebote willkommen.
Gotteskrieger gesucht!
For it is the chief characteristic of the religion of science that it works. - Isaac Asimov, Foundation I, Buch 1
  Mit Zitat antworten Zitat