Forum > Designer

[SOLVED] anchors auto resize

(1/4) > >>

mercury:
I made a form like pic1.

And want the form auto resize like pic2.

So I set anchors like pic3.

But it auto resize like pic4.

How can I make it auto resize like pic2?

Thank for you help.

howardpc:
I don't know of an easy way to do this using the Form Designer (though there may well be one).
You can achieve this in code thus:

--- Code: ---unit LayoutButtons;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, StdCtrls;

type
  TBtnRange = 1..6;
  TBArray = array[TBtnRange] of TButton;

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    FButtons: TBArray;
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var
  i: integer;
  hw: TPoint;

  function NewButton(anIdx: integer): TButton;
  var
    r, c: integer;
  begin
    r:=(anIdx-1) div 3;
    c:=(anIdx-1) mod 3;
    Result:=TButton.Create(Self);
    Result.AutoSize:=False;
    Result.Name:=Format('FButtons_%d',[anIdx]);
    Result.SetBounds(c*hw.x-(c+1), r*hw.y-(r+1), hw.x, hw.y);
    Result.Parent:=Self;
  end;

  function GetHW: TPoint;
  begin
    Result.x:=ClientWidth div 3;
    Result.y:=ClientHeight div 2;
  end;

begin
  hw:=GetHW;
  for i in TBtnRange do
    FButtons[i]:=NewButton(i);

  Height:=FButtons[High(TBtnRange)].BoundsRect.Bottom;
  Width:=FButtons[High(TBtnRange)].BoundsRect.Right;
end;

end.

--- End code ---

balazsszekely:
@howardpc

Your forgot the resizing part, when the user resize the form. Other then that it's a nice and elegant solution.

howardpc:
Yes, OnResize event left as an exercise for the OP - he's got the basic algorithm already.

Blaazen:
Do not use Anchor nor OnResize event. Lazarus can do it.

Open ChildSizing property of the form and set:

ControlsPerLine := 3;
Layout := cclLeftToRightThenTopToBottom;
and all
Shrink/EnlargeVertical/Horizontal := crsHomogenousChildResize;

And you have it.

Navigation

[0] Message Index

[#] Next page

Go to full version