LCL Tips/fr
│
Deutsch (de) │
English (en) │
français (fr) │
русский (ru) │
中文(中国大陆) (zh_CN) │
Créer des contrôles manuellement sans surcoût de calcul
Choisir le Parent à la fin
Pour les utilisateurs de Delphi: A la différence de Delphi la bibliothèque LCL permet de paramétrer presque toutes les propriétés dans n'importe quel ordre. Par exemple sous Delphi vous ne pouvez pas placer un contrôle sans un parent . La bibliothèque LCL permet ceci et cette fonctionnalité peut être employée pour réduire du calcul supplémentaire(overhead).
<delphi>
with TButton.Create(Form1) do begin // 1. crée un bouton fixe la taille de défaut // 2. change la position. No side effects, because Parent=nil SetBounds(10,10,Width,Height); // 3. change size depending on theme. Not yet, because Parent=nil AutoSize:=true; // 4. changing size because of AutoSize=true. Not yet, because Parent=nil Caption:='Ok'; // 5. Choix du parent. Maintenant tout ce qui est ci-dessus se met en place, mais dans une seule action. Parent:=Form1; end;
</delphi> Quand un contrôle a un parent, alors toutes les propriétés prennent effet immédiatement. Sans un parent beaucoup de propriétés ne font rien de plus que de stocker la valeur . And as soon as the Parent is set every property is applied. This is especially true for grand children:
GroupBox1:=TGroupBox.Create(Self); with GroupBox1 do begin with TButton1.Create(Self) do begin AutoSize:=true; Caption:='Click me'; Parent:=GroupBox1; end; Parent:=Form1; end; Form1.Show;
Autosizing starts not before every parent is setup and the form becomes visible.
Avoid early Handle creation
As soon as the Handle of a TWinControl is created, every change of a property changes the visual thing (called the widget). Even if a control is not visible, when it has a Handle, changes are still expensive.
Use SetBounds instead of Left, Top, Width, Height
Instead of
with Button1 do begin Left:=10; Top:=10; Width:=100; Height:=25; end;
Use
with Button1 do begin SetBounds(10,10,100,25); end;
Left, Top, Width, Height are calling SetBounds. And every change of position or size invokes recalculation of all sibling controls and maybe recursively the parent and/or the grandchild controls.
DisableAlign / EnableAlign
When positioning many controls, it is a good idea to disable the recalculation of all auto sizing, aligning, anchoring.
DisableAlign; try ListBox1.Width:=ClientWidth div 3; ListBox2.Width:=ClientWidth div 3; ListBox3.Width:=ClientWidth div 3; finally EnableAlign; end;
Note: Every DisableAlign call needs a EnableAlign call. For example if you call two times DisableAlign, you must call two times EnableAlign.
For Delphians: This works recursively. That means DisableAlign stops aligning in all childs and grand childs.