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

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

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

DownloadVCL demo project with full source code

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

最新文章

  1. PHP 删除目录及目录下文件
  2. Qt 设计师手册
  3. vs快捷键visual studio
  4. java14-4 Pattern和Matcher类的使用
  5. 使用dbms_system追踪其它session
  6. read 计时命令
  7. <context:annotation-config> 和 <context:component-scan>的差别
  8. Servlet中response.sendRedirect()跳转时不能设置target的解决办法
  9. oracle实现自增列
  10. 基于ArcGIS的栅格图像平滑处理(转)
  11. C# 文件创建时间,修改时间
  12. 关于WIN7 家庭版 iis 部署问题
  13. openstack私有云布署实践【13.1 网络Neutron-compute节点配置(科兴环境)】
  14. 3360: [Usaco2004 Jan]算二十四
  15. java 重载与重写 【转】
  16. MySql数据库第一天
  17. Vuex初识
  18. MySQL 8 新特性之Invisible Indexes
  19. mysql函数调用过程
  20. JS(JavaScript)的进一步了解3(更新中···)

热门文章

  1. MyReport报表引擎2.2.0.0新功能
  2. win32命令行小程序获取指定文件夹或者目录下面的所有文件大小,文件数量,目录数量
  3. 关于 Mesos,你知道多少?13 个问题带你深入了解 Mesos
  4. Python 标准库 —— uuid(生成唯一 ID)
  5. vista/win7系统 红警/CS/星际争霸 局域网连接方法
  6. Numpy Usage Introduction
  7. AVL 树的插入、删除、旋转归纳
  8. 如何成为QTP专家
  9. ubuntu 12.04 简单配置samba服务,实现主机与虚拟机互通(设置Windows虚拟盘)
  10. 新浪微博Python客户端接口OAuth2