Difference between revisions of "LCL Tips"

From Lazarus wiki
Jump to navigationJump to search
Line 35: Line 35:
  
 
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.
 
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.

Revision as of 22:29, 25 July 2006

Create controls manually without overhead

Set the Parent as last

For Delphians: Contrary to Delphi the LCL allows to set nearly all properties in any order. For example under Delphi you can not position a control without a parent. The LCL allows this and this feature can be used to reduce overhead.

 with TButton.Create(Form1) do begin
   // 1. creating a button sets the default size
   // 2. change 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. Set Parent. Now all the above takes place, but in a single action.
   Parent:=Form1;
 end;

When a control has a Parent, then all properties take effect immediately. Without a Parent many properties do nothing more than store the value. 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.