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. ag-admin部署使用心得

    开源地址:https://github.com/wxiaoqi/Spring-Cloud-AG-Admin(后端)https://gitee.com/geek_qi/AG-Admin-v2.0(后端) ...

  2. View的绘制顺序

    1.写在 super.onDraw() 的下面 把绘制代码写在 super.onDraw() 的下面,由于绘制代码会在原有内容绘制结束之后才执行,所以绘制内容就会盖住控件原来的内容. 2.写在 sup ...

  3. 【22.48%】【codeforces 689D】Friends and Subsequences

    time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard o ...

  4. poj 3071 可能DP

    http://poj.org/problem? id=3071 推方程不难,可是难在怎么算 dp[i][j]表示第i场时第j仅仅队伍存活下来的概率 方程:dp[i][j]=sigma(dp[i-1][ ...

  5. 《网络编程》ioctl 操作

    概要 ioctl 功能与 fcntl 功能类似,它可以被用于描述操作的叙述字符,获取或设置属性的描述是开放式的叙事休息,但在网络编程的两个功能有关的不同类型的操作.fcntl 作.文件操作,而 ioc ...

  6. Matlab Tricks(二十六)—— 置乱(随机化)与恢复(shuffle/permutation & restore)

    x = 1:10; n = length(x); perm = randperm(n); x_perm = x(perm); % x_perm 表示置乱后的结果 x_ori(perm) = x_per ...

  7. silverlight,WPF动画终极攻略之迟来的第三章 动画整合篇(Blend 4开发)

    原文:silverlight,WPF动画终极攻略之迟来的第三章 动画整合篇(Blend 4开发) 有个问题想请教下大家,我仿了腾讯的SL版QQ,相似度95%以上.我想写成教程教大家怎么开发出来,会不会 ...

  8. ASP.NET Core 新增用户 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 新增用户 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 新增用户 上一章节我们实现了一个注册表单,但也留了一些东西还没完成, ...

  9. matlab GUI 编程

    matlab 语法的简便,在 GUI 上也不遑多让呀: uigetfile [filename, pathname] = uigetfile('*.m', 'choose a m file') 1. ...

  10. C# Thread 参数

     Thread (ParameterizedThreadStart) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托.   Thread (ThreadStart) 初 ...