Z Order of Controls in Delphi VCL

Get and set the Z Order of controls at runtime in Delphi VCL.

z_order_of_controls_vcl_intro

If you are looking for a FireMonkey solution see this post

Delphi provides a limited API for the Z order of controls.
You can bring a control to the front or send it to the back … that is all.

begin
  Edit1.BringToFront;
  Edit2.SendToBack;
end;

There is no API to get and set the Z order of a control.
Don’t panic – we can fix that using code that I provide in this post.

Demo Project

z_order_of_controls_vcl

Full source code provided.  See download link below.

The project demonstrated how to get and set the Z Order of controls.

You can change the Z order and watch the YELLOW TEdit move up and down through the Z Order.

The list on the right hand side shows the Z order of all controls on the form.
The Yellow TEdit that is being manipulated is highlighted in the list with “*******”

How does it work ?

The code uses the same BringToFront API repeatedly until everything is in the desired order.  Its primitive but it works well enough.

It also deals with some other quirks, but the basic technique is based on brute force

Get Z Order of a control

This is the code that 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, 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;

Close enough

Some controls such as TLabel are always positioned at the back and their Z Order can not be changed.   This can make it difficult for the zzSetControlZOrder  function to reorder the control to exactly the right position, so it attempts to get it as close as possible to what you specify

Zero Based Z-Order

The Z Order is zero based, so the first control is # 0, the second control is #1.

Try to break it

You 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 …

  • Controls on Forms, Panels and Frames.
  • Tested for VCL in Delphi 10.1 Berlin and should work in many prior releases of Delphi

Download

Download VCL demo project with full source code

https://scotthollows.com/2016/12/04/scott-hollows-z-order-of-controls-in-delphi-vcl/

原文地址:https://www.cnblogs.com/findumars/p/6307058.html