Get and set the Z Order of controls at runtime in Delphi Firemonkey.
This is a follow in to my earlier post where I provided a VCL solution.
Now I Ive created a free Firemonkey unit this has the same get and set routines as the VCL solution. The full source code was available for download
Source and Demo Project
Full source code provided.
Download here
The demo lets you get and set the Z Order of controls and also get a list of all controls in Z order.
The list on the right hand side shows the Z order of all controls on the form.
You can change the z Order and watch the ' B ' tedit move up and down through the Z order.
What does Delphi provide?
Delphi provides a limited API to set the Z order of controls.
Can bring a control to the front or send it to the back ... that's all.
Begin Edit1.bringtofront; Edit2.sendtoback;end;
But we need to reposition a control anywhere in the Z order!
Don ' t Panic–we can do the using code that I provide the this post.
How does my code work?
The code uses a brute force approach, repeatedly bringing controls to the front until they is in the requested order. It ' s a primitive approach but it works well enough.
Get Z Order of a control
This is the code, which you write. Pretty easy
var vzorder:integer;begin vzorder:= zzgetcontrolzorder (myedit); end;
Modify the Z Order of a control
Another one liner. The brute force happens behind the scenes
Begin Zzsetcontrolzorder (Myedit, ten); end;
Reposition a control on top of another control
Reposition Edit1 to being on top of Edit2begin Zzsetcontrolzorder ( Edit1 , Zzgetcontrolzorder (Edit2) + 1 ); en D
Reposition a control below another control
Reposition Edit1 to being on top of Edit2begin Zzsetcontrolzorder ( Edit1 , Zzgetcontrolzorder (Edit2)-1 ); en D
Firemonkey Dummy Elements
Some Firemonkey objects such as Tform and Tpanel always include a child # 0 that's used to paint the background or border . Dont worry, I had coded around to ensure that these elements remain at the bottom of the list.
Close enough
In Vcl–some controls such as Tlabel is always positioned at the back and their Z Order can not be changed.
This was less of a issue for Firemoneky as Tlabels can was brought to the front. However, I expect it'll still be a issue for some controls
With the Zzsetcontrolzorder function would reorder the control to as close as possible to the Z position that You specify.
Zero Based Z-order
The Z Order is zero based and the first control is # 0, and the second control is #1.
Try to break it
You can throw an any object so you like at the routines and they would survive. If you pass in something this does not has a z-order position, it wont do anything and it also wont crash, show an error or raise an error. The GET function would return-1. The SET function would return FALSE. I could has raised an error, but for the purposes that I built this it is more convenient to suppress all errors.
This works for ...
- Tested for Delphi 10.1 Berlin and should work in many prior releases of Delphi
- Supports FMX controls on Forms, Panels and frames–and their descendents.
Let me know if you is interested in others so dont work and Ill see what I can do.
What's about VCL?
This post was about Firemonkey.
See this post for my solution for VCL
There is minor differences between VCL and Firemonkey
1) Firemonkey creates a child trectangle as element #0, placed at the bottom of the Z order in Forms and Panels.
2) You can don't change the z-order for Tlabel in VCL, but can in Firemonkey
Feedback
I ' m interested in your feedback. Let me know if you had an idea for improvement, find a bug or is interested in a scenario that the unit does not support .
Download
Download the demo project with full source code
https://scotthollows.com/2016/12/27/scott-hollows-z-order-of-controls-in-delphi-firemonkey-fmx/comment-page-1/#comment-41
Z Order of Controls in Delphi firemonkey (blog from Tom Yu)