Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What use are the Align and Anchor properties on TForm?

It's possible to set the Alignment on a TForm, say set one form to alTop and another to alClient - whereby the two forms take up the whole screen area in the obvious way. Is this a sensible thing to do in an application?

I also notice that anchors are exposed on forms - but I can't think what they would be useful for (resolution changes? MDI apps?) Any ideas?

Edit: I've made a video about this post to make things clearer.

like image 561
Alister Avatar asked Oct 10 '12 03:10

Alister


3 Answers

You can use a TForm like an ordinary control by setting its Parent property:

procedure TForm1.Button1Click(Sender: TObject);
begin
  frmEmbed:= TForm.Create(Self);
  frmEmbed.Parent:= Self;
  frmEmbed.Width:= 50;
  frmEmbed.Height:= 50;
  frmEmbed.Align:= alRight;
  frmEmbed.Anchors:= [akLeft, akBottom];
  frmEmbed.Visible:= True;
end;

you should comment frmEmbed.Align:= alRight; line to see how Anchors property works.


If you are interested where the above is used: parented form without a caption bar is an alternative to TFrame; frames were not available with early Delphi versions, so parented forms were used instead. You can find them in legacy code.

like image 156
kludg Avatar answered Oct 07 '22 04:10

kludg


You can place a Form inside another Form. Dunno how good that would work though. In Delphi1 times there were special 3rd-party controls to route the event. Today it seems to more or less work out of the box, except for modal dialogs. Try like this:

procedure TMainForm.Button1Click(...);
begin
  with TForm.Create(Self) do begin
       Caption := 'Internal one';
       Parent := Self;
       Visible := True;
  end;    
end;    

Perhaps anchors and align would make sense in this setup. Afterall this seems how new "one-window" IDE layout is implemented.

like image 3
Arioch 'The Avatar answered Oct 07 '22 04:10

Arioch 'The


One simple case is for a captionless form (e.g. win-8 metropolis style) you can anchor an exit button to upper right corner.

But the best use is to simplify making a complex form responsive to size changes... Using the akLeft and akRight, you can make a horizontal control fill space horizontally. Using all 4 anchors is similar to setting a client to alclient, just without needing to surround it with other panels.

Much of what you can do with anchors can also be done with many panels, but as the form becomes more complex it will get messy sometimes requiring several levels of panels upon panels.

Of course using a combination of panels and anchors will often be the best answer.

like image 2
Craig Hanson Avatar answered Oct 07 '22 06:10

Craig Hanson