-1

I am currently developing a fullscreen application that I want to be centered on the screen. I have set the following settings on the form:

WindowState:= wsMaximized
FormStyle:= fsStayOnTop

However due to the form height and width being much lower than the screens actual resolution, the form is aligned into the top-left corner. I have also attempted using the Position setting on the form, however none of these settings seem to have the desired outcome. They either have no effect or shift the entire maximized for towards the Bottom-Right corner, resulting in being able to see the forms behind (I hope I have described this well enough).

Thankyou

Seryth
  • 133
  • 1
  • 3
  • 10
  • 3
    How could one have a full screen centered window ? It can either be centered, or full screen. Am I missing something here ? – TLama Dec 18 '14 at 10:29
  • The application is full screen, but the form which I have maximized is not centered on the screen as the form is maximizing in the Right and Down directions. – Seryth Dec 18 '14 at 10:30
  • 1
    Do you mean that the form as such is maximized, but the components on the form are in top-left corner of form? If yes, place the components on a panel and center that panel in the forms OnShow event. – Tom Brunberg Dec 18 '14 at 10:47
  • @Tom, there's a trick for control centering. You can center the control inside its parent and clear the control's `Anchors` set (see [`this Q&A`](http://stackoverflow.com/q/12614453/960757)). – TLama Dec 18 '14 at 11:25
  • 1
    When *I* think of a "centered" form, it means the distances between the borders of the form and the edges of the monitor are equal. Is that what *you* think it means? Perhaps this question would benefit from a picture. Could you post one, please? – Rob Kennedy Dec 18 '14 at 13:35
  • @TLama Yes, that's a cool trick. There was once a discussion about it in the Emb forums. But, there's a catch. I recall a potential problem if one or more components had an anchor set and the scrollbar appeared when resizing. Repeating scrolling and resizing, you could get the components that were 'floating' to completely disappear. I think it has to do with negative position of the floating component after a scroll action. So, either have all components floating (without anchors) or disable autoscroll. – Tom Brunberg Dec 18 '14 at 14:24

1 Answers1

3

The way I understand your question is that the form, as such, maximises correctly, but the components are in top-left corner of the maximized form, according to their Left and Top properties as set in the form designer.

For example, a form as designed in the designer:

enter image description here

Form as it appears when maximized (and this is the problem):

enter image description here

If my understanding of the problem is correct, the solution is to place all components on a TPanel and then center that TPanel on the form.

There are two ways to center the TPanel
1) Clear (set to false) all anchors of the panel as in the link provided by TLama in his comment.
2) Center the panel in the forms OnResize event

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

Either way the result looks like:

enter image description here

Community
  • 1
  • 1
Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54
  • Thank you, I'm glad someone was able to understand my bad excuse of an explanation. This has worked! – Seryth Dec 19 '14 at 13:17