-2

I am using delphi

I have a program that runs in multiple shops but some people have a screen with a low resolution and some have a screen with a high resolution.

When i run the program on a screen with a lower resolution, the components are more to the right and a scroll bar appears. I want my components to appear exactly in the middle of the form no matter what resolution. how?

Issyy Moola
  • 137
  • 3
  • 13
  • You see, i have a top and left property and i set the components top and left properties so the the components appear in the middle of the form but when i run the program on a smaller screen, the components are no in the middle anymore, they are too far left. – Issyy Moola Aug 31 '13 at 08:21
  • If you are bumping into issues like this your whole UI design is incorrect and you should maybe pick up a book or read an article on standard GUI design practices. – Peter Aug 31 '13 at 08:56
  • Which version of Delphi? I know that in Delphi 6, even with anchoring, and Setting the Scaled property, machines with large fonts enabled will still screw up the UI on sizeable forms. – MikeD Aug 31 '13 at 10:43
  • Please repost your real question. It seems you already know how to get components to stay in the center of the form, but you really want them to stay in the center of the screen. That's a *different* question. – Lasse V. Karlsen Aug 31 '13 at 11:44

3 Answers3

7

Screen resolution is no issue, unless you are talking about maximized forms, so I will assume that. That implies you having trouble with aligning components in the center of changing form sizes.

Considering a single control in that regard, then the solution is relatively easy: change its position in an OnResize event handler:

procedure TForm1.FormResize(Sender: TObject);
begin
  Control.Left := (ClientWidth - Control.Width) div 2;
  Control.Top := (ClientHeight - Control.Height) div 2;
end;

For a multiple control situation, the most easy solution is to place them on a single container, e.g. a panel, and use the above approach. But for small forms, this could result in the container being too large resulting in showing the form's scroll bars as you say (which can be suppressed by the AutoScroll property of the form), and for large form sizes, the container could render too small.

Another solution is the relative anchor solution: Just set the Anchors property to [] for all controls, and all controls will stay in their relative position, regardless form size. (This trick also applies to solution #1).

In both cases, the control's size and font size remain the same which could or will distort the layout. There are multiple possible solutions for that, but that's beyond the scope of your question and this answer I think.

Community
  • 1
  • 1
NGLN
  • 43,011
  • 8
  • 105
  • 200
  • Wouldn't `Control.Left:=Round((ClientWidth - Control.Width)/2);` be more accurate? – TheGreenOmega Aug 18 '17 at 15:02
  • @TheGreenOmega Yes. But I prefer (1) an integer (quicker) calculation in this resize handler, (2) cleaner code, (3) not to bother, because you won't notice the difference. – NGLN Aug 23 '17 at 10:55
3

You can use Anchors property. For example if you align TEdit, TCombobox as you need and you want them to have always same position relatively left and right side of the form, just set Anchors property accordingly (akLeft=True, akRight=True). You can do that in design time or from the code.

P.S. Descriptions of problem is no so clear actually.

Andrei Galatyn
  • 3,322
  • 2
  • 24
  • 38
  • That doesn't solve my problem, does my above comment make it clearer? – Issyy Moola Aug 31 '13 at 08:31
  • 2
    It might not solve your problem, but it answers the question you asked. You asked, *I want my components to appear exactly in the middle of the form no matter what resolution.* If you meant to ask a different question, that's your own fault, but this is an answer to the one that you did ask. – David Heffernan Aug 31 '13 at 09:34
  • 1
    Although this solution kind of answers the question, it also resizes the component which surely isn't desired. – NGLN Aug 31 '13 at 11:10
1

Use a TGridPanel to place your components in if they have to be centered all the time. The TGridPanel will align it's child components according to the Alignment and VerticalAlignment properties within each cell.

  • Place the TGridPanel where you want to show your component
  • Set the Align to alClient.
  • Set the ColumnCollection to only one column.
  • Set the RowCollection to only one row.
  • Set the Alignment to taCenter
  • Set the VerticalAlignment to taVerticalCenter
  • Place your component in the TGridPanel

Note: You can put only one component in each cell of a TGridPanel, to overcome this restriction put a TPanel in a cell and then multiple components in that panel.