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...