AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Delphi-PRAXiS - Lounge Delphi-News aus aller Welt Delphi Tip of the Day: A Better Way to Center Modal Forms
Thema durchsuchen
Ansicht
Themen-Optionen

Delphi Tip of the Day: A Better Way to Center Modal Forms

Ein Thema von DP News-Robot · begonnen am 11. Mai 2025
Antwort Antwort
Benutzerbild von DP News-Robot
DP News-Robot

Registriert seit: 4. Jun 2010
15.824 Beiträge
 
#1

Delphi Tip of the Day: A Better Way to Center Modal Forms

  Alt Gestern, 12:50


Yesterday, I was going through some final aesthetic checks on my FMX desktop software application. I have two modal forms that popup up during click events. One is help about which I set to screen center. And the other contains code that centers the form on it's parent window.

I was testing the behavior of this form toggling between light and dark themes. I noticed that with light theme the window just appears but with the dark theme I could see the window move it's way from the top of the screen to it's centered position and then switch to dark.

In the OnShow event handler I was calculating Left and Top like I have always done to center the window. This was the first time I'd ever seen the window reposition itself. This is the first time I'd ever used a dark theme. And, this is the first major application I'd written with FMX.


Left := ParentForm.Left + (ParentForm.Width - Self.Width) div 2;Top := ParentForm.Top + (ParentForm.Height - Self.Height) div 2; To say the least, I was annoyed by this split second flash centering itself on the parent window. I tried all kinds of stuff to minimize this flash. Nothing work.

There's got to be a better way!



I didn't realize I had left the Position property of the form set to Default. In essence that is screen position (0,0), or the top-left corner.


The first line of my code was moving the form horizontally from the left edge to the center of its parent. (I never saw that move happening). The second line of code was moving the form vertically from the top edge to the center of its parent (I saw this move happening).



I thought there's got to be a better way? I don't want customers seeing this screen moving like this. I tried setting the form's Visible property to False before the centering code and setting the Visible to True after. Nope, that unauthorized.


I tried in the OnCreate instead. Nope. I had properties for the forms Left and Top along with getters and setters. I created a whole elaborate scheme to support positioning this form where I wanted it.


Then it dawned on me. "Isn't there a screen position property?"


Yes, there is.
MainFormCenter! It's a thing!



FMX Form Position Property
Are you kidding me. MainFormCenter! It's a thing! When did this show up?


I immediately set the form Position property to MainFormCenter and added an Exit statement to the top of the OnShow event handler.

And voila!


It works like a champ. No more flashy form realignment stuff happening.


I then removed all the getters, setters, and properties I added for manually doing the form centering activity. Simple, cleaner code. ?


BTW, the same thing exists for VCL.


VCL Form Position Property
DocWiki Links:
FMX.Forms.TFormPosition
VCL.Forms.TPosition


Enjoy
Semper Fi
Gunny Mike

https://zilchworks.com




Weiterlesen...
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 19:02 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz