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/

Z Order of Controls in Delphi VCL的更多相关文章

  1. Z Order of Controls in Delphi FireMonkey(Tom Yu的博客)

    Get and set the Z Order of controls at runtime in Delphi FireMonkey. This is a follow on to my earli ...

  2. 自定义经纬度索引(非RTree、Morton Code[z order curve]、Geohash的方式)

    自定义经纬度索引(非RTree.Morton Code[z order curve].Geohash的方式) Custom Indexing for Latitude-Longitude Data 网 ...

  3. Z Order(Copy From WIN32.HLP)

    The Z order of a window indicates the window's position in a stack of overlapping windows. This wind ...

  4. delphi VCL研究之消息分发机制-delphi高手突破读书笔记

    1.VCL 概貌 先看一下VCL类图的主要分支,如图4.1所示.在图中可以看到,TObject是VCL的祖先类,这也是Object Pascal语言所规定的.但实际上,TObject以及TObject ...

  5. delphi VCL组件同名继承

    当我们在扩展一个 vcl 组件功能的时候,既想保留IDE中能拖动大小与直接设置属性的功能,又想减少写创建与释放代码和安装扩展后新组件的麻烦,那么本文中的方法,就非常实用了. 以给TStringGrid ...

  6. QT类库与Delphi VCL类库的体系结构对比——两者十分类似!

    今天在看QT对象内存管理的一篇文章时:http://blog.csdn.net/dbzhang800/article/details/6300025想到了一个问题:就是QT类库体系结构与Delphi类 ...

  7. DELPHI (VCL及FMX[Firemonkey])启动时的欢迎窗口实现代码

    VCL里面的的实现 program ZhouFamily; uses Vcl.Forms, Winapi.Windows, FrmZhouFamilyMainU in 'FrmZhouFamilyMa ...

  8. CSDN论坛 > Delphi > VCL组件开发及应用 DBLookupComboBox用法

    (1)DataSource属性    该属性用于连接要编辑数据的主表数据源(2)DataField属性    该属性用于指定要编辑的数据字段名(3)ListSource属性    .    该属性用于 ...

  9. Delphi VCL类结构

随机推荐

  1. mysql 数据库 添加查询 修改 删除

    cmd 命令行模式操作数据库 添加查询 修改 删除 ( 表 字段 数据)   一 查看数据库.表.数据字段.数据 1 首先配置环境变量 进入mysql  或者通过一键集成工具 打开mysql命令行   ...

  2. [Cordova+Sencha Touch] 移动开发1 sencha 2.4.0 + 在 安卓2.3.6上使用报错 - has no method 'bind'

    Sencha Touch 2.3.2和2.4.0在安卓2.3上面用会报错,具体报错信息如下: 解决办法是: 打开文件:你的file:///android_asset/www/sencha-touch- ...

  3. 打开一个很好的介绍Lucene4 FST文章

    我没有看到源代码.看到这个博客了解一些基本的,像笔者下: http://download.csdn.net/download/guanxinquan/7380591 http://blog.sina. ...

  4. win10 uwp 使用 Matrix3DProjection 进行 3d 投影

    原文:win10 uwp 使用 Matrix3DProjection 进行 3d 投影 版权声明:博客已迁移到 http://lindexi.gitee.io 欢迎访问.如果当前博客图片看不到,请到 ...

  5. 写在使用 Linux 工作一年后

    start 去年公司空了几台台式机,当时看了下似乎配置比我用的乞丐版 air 略高一些,而且除了 ssd 以外还有一个 1T 的大硬盘,加上后面可能会有一段时间不做 iOS 了,那就不需要 macOS ...

  6. sqlserver中获取最后一个字符所在的位置

    CHARINDEX('字符',reverse(字段名称)) 这个意思就是将字段进行反转,就是从后往前取,这样就能够获取一个字符最后所在的位置

  7. Delphi Bpl包学习

    对于BPL包,我个人理解是:就是一种封装方式,和DLL,EXE类似,把代码放到包(package)里面保存而已. 一.先说说如何创建BPL包 1.   打开delphi IDE(delphi7 为例) ...

  8. mybatis如何实现分页功能?

    1)原始方法,使用limit,需要自己处理分页逻辑: 对于mysql数据库可以使用limit,如: select * from table limit 5,10; --返回6-15行 对于oracle ...

  9. Global Contrast based Salient Region Detection (Ming ming Cheng)

    abstract: Automatic estimation of salient object regions across images, without any prior assumption ...

  10. pandas 学习(二)—— pandas 下的常用函数

    import pandas as pd; 1. 数据处理函数 pd.isnull()/pd.notnull():用于检测缺失数据: 2. 辅助函数 pd.to_datetime() 3. Series ...