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. 远程ssh执行命令时提示找不到命令

    最开始的时候碰到这种问题,是在hadoop003上配置了jdk1.8, 在hadoop002上执行ssh hadoop003 java -version提示没有命令,先ssh hadoop003然后执 ...

  2. Parallel.For

    Parallel.For 你可能忽视的一个非常实用的重载方法    说起Parallel.For大家都不会陌生,很简单,不就是一个提供并行功能的for循环吗? 或许大家平时使用到的差不多就是其中最简单 ...

  3. WPF 自定义控件的坑(蠢的:自定义控件内容不显示)

    原文:WPF 自定义控件的坑(蠢的:自定义控件内容不显示) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/koloumi/article/detai ...

  4. qt翻译--QWaitCondition

    QWaitCondition Class Reference[QtCore module] 该类提供一个情况变量来同步线程. #include <QWaitCondition> 注意:该类 ...

  5. Go 语言如果按这样改进,能不能火过 Java?

    据 InfoWorld 消息,为改进 Go 语言的开发工具,Go 可能会获得自己的语言服务器,类似于 Microsoft 和 Red Hat 的语言服务器协议. 消息是从 Go 语言开发者的讨论组中流 ...

  6. EF 导航属性的使用

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  7. jquery评分星星

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  8. C++ Boost库简介(一些自己的感受)

    boost是一个准标准库,相当于STL的延续和扩充,它的设计理念和STL比较接近,都是利用泛型让复用达到最大化.不过对比STL,boost更加实用.STL集中在算法部分,而boost包含了不少工具类, ...

  9. python3 提示No module named _sqlite3

    yum install sqlite-devel 下载python3.6.3  重新编译安装 ./configure --enable-loadable-sqlite-extensions & ...

  10. 关于Android 7.0更新后调用系统相机及电筒问题

    android升级到7.0后对权限又做了一个更新即不允许出现以file://的形式调用隐式APP,需要用共享文件的形式:content:// URI 因为系统相机是提供的共享 Provider , C ...