在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Get and set the Z Order of controls at runtime in Delphi FireMonkey.
This is a follow on to my earlier post where I provided a VCL solution. Source and Demo ProjectFull source code provided. 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. What does Delphi Provide ?Delphi provides a limited API to set the Z order of controls. begin Edit1.BringToFront; Edit2.SendToBack; end; But we need to reposition a control anywhere in the Z order ! Don’t panic – we can do that using code that I provide in this post. How does my code work ?The code uses a brute force approach, repeatedly bringing controls to the front until they are in the requested order. It’s a primitive approach but it works well enough. Get Z Order of a controlThis is the code that you write. Pretty easy var vZorder : integer; begin vZorder:= zzGetControlZOrder (MyEdit); end; Modify the Z Order of a controlAnother one liner. The brute force happens behind the scenes begin zzSetControlZOrder (MyEdit, 10); end; Reposition a control on top of another control// reposition Edit1 to be on top of Edit2 begin zzSetControlZOrder ( Edit1 ,zzGetControlZOrder (Edit2) + 1 ); end; Reposition a control below another control// reposition Edit1 to be on top of Edit2 begin zzSetControlZOrder ( Edit1 ,zzGetControlZOrder (Edit2) - 1 ); end; FireMonkey dummy elementsSome FireMonkey objects such as TForm and TPanel always include a child # 0 that is used to paint the background or border. Dont worry, I have coded around to ensure that these elements remain at the bottom of the list. Close enoughIn VCL – some controls such as TLabel are always positioned at the back and their Z Order can not be changed. This is less of an issue for FireMoneky as TLabels can be brought to the front. However, I expect it will still be an issue for some controls With this in mind, the zzSetControlZOrder function will reorder the control to as close as possible to the Z position that you specify. Zero Based Z-OrderThe Z Order is zero based, so the first control is # 0, the second control is #1. Try to break itYou can throw any object that you like at the routines and they will survive. If you pass in something that does not have a Z-Order position, it wont do anything and it also wont crash, show an error or raise an error. The GET function will return -1. The SET function will return FALSE. I could have raised an error, but for the purposes that I built this it was more convenient to suppress all errors. This works for …
What About VCL ?This post is about FireMonkey. There are 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 not change the Z-Order for TLabel in VCL, but you can in FireMonkey FeedbackI’m interested in your feedback. Let me know if you have an idea for improvement, find a bug or are interested in a scenario that the unit does not support. DownloadDownload 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 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论