[D2007] Sérialisation de composant
Bonjour,
J'ai besoin de sérialiser un composant (disons un TPanel pour l'exemple) et l'ensemble des composants qu'il contient, récursivement.
Mon problème est que je n'obtiens pas ce que je souhaite, c-a-d justement une sérialisation récursive du composant.
Dans mon test trivial, je pose un TPanel (panel1) sur une fiche. Dans panel1 je pose un panel2 et dans panel2 je pose un panel3.
Je demande ensuite la chaîne de sérialisation avec cette fonction:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
function GetComponentDefinition(aComp: TComponent): string;
var
StreamChar: TMemoryStream;
StreamBin: TMemoryStream;
begin
StreamBin := TMemoryStream.Create;
try
StreamBin.WriteComponent(aComp);
StreamChar := TMemoryStream.Create;
try
StreamBin.Position := 0;
ObjectBinaryToText(StreamBin, StreamChar);
StreamChar.Position := 0;
Result := '';
SetLength(Result, StreamChar.Size);
StreamChar.Read(Result[1], StreamChar.Size);
finally
StreamChar.Free;
end;
finally
StreamBin.Free;
end;
end; |
voici le résultat de GetComponentDefinition(panel1):
Code:
1 2 3 4 5 6 7 8
|
object Panel1: TPanel
Left = 12
Top = 20
Width = 261
Height = 521
TabOrder = 1
end |
Je m'attendais à ce qu'il contienne aussi la définition de panel2 lui-même contenant la définition de panel3 (comme dans le dfm de la fiche).
J'ai pensé que la procédure de sérialisation de Delphi utilise la propriété Owner (components) et non Parent (controls). Et effectivement lorsqu'on pose les panels les uns dans les autres le Owner est la fiche:
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
//Panel2 (parent: Panel1, owner: Form1)
//Panel3 (parent: Panel2, owner: Form1)
//sérialisation de panel1
object Panel1: TPanel
Left = 12
Top = 20
Width = 261
Height = 521
TabOrder = 1
end |
J'ai donc créé une procédure qui réaffecte le Owner des composants à leur parent (InsertComponent), mais j'obtiens:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
//Panel2 (parent: Panel1, owner: Panel1)
//Panel3 (parent: Panel2, owner: Panel2)
//sérialisation de panel1
object Panel1: TPanel
Left = 12
Top = 20
Width = 261
Height = 521
TabOrder = 1
object Panel2: TPanel
Left = 28
Top = 28
Width = 213
Height = 417
Caption = 'Panel2'
TabOrder = 0
end
end |
Où est-donc le panel3 ? Je précise que la sérialisation ne m'affiche pas plus que le premier niveau d'inclusion alors que les Owners sont tous correctement affectés.
Comment obtenir une sérialisation correcte d'un composant visuel quelconque contenant lui-même n-composants visuels etc.. ?
Edit: Le parcours récursif lors de la sérialisation devrait quand même utiliser les "controls". En fait, je ne sais pas du tout comment cela fonctionne réellement...
Merci pour vos idées.