VCL 的 TAlign 的功能比较弱,有时候,无法满足我们对齐的需要,所以这里提供了一个额外的 CustomAlign 函数来实现更多的对齐方式。当然,如果你是用 FMX 的话,这些对齐方式已经有了,你可以忽略这篇文章。
type
TCustomAlign = (caNone, caTop, caLeft, caRight, caBottom, caClient, caCenter,
caVertCenter, caHorzCenter, caHorizontal, caVertical);
procedure CustomAlign(ACtrl: TControl; ALayout: TCustomAlign);
implementation
procedure CustomAlign(ACtrl: TControl; ALayout: TCustomAlign);
var
R: TRect;
begin
if ALayout <> caNone then
begin
if ACtrl is TCustomForm then
R := (ACtrl as TCustomForm).Monitor.BoundsRect
else
R := ACtrl.Parent.ClientRect;
ACtrl.Align:=alNone;
case ALayout of
caTop:
ACtrl.SetBounds(R.Left, R.Top, R.Width, ACtrl.Height);
caLeft:
ACtrl.SetBounds(R.Left, R.Top, ACtrl.Width, R.Height);
caRight:
ACtrl.SetBounds(R.Right - ACtrl.Width, R.Top, ACtrl.Width, R.Height);
caBottom:
ACtrl.SetBounds(R.Left, R.Bottom - ACtrl.Height, R.Width, ACtrl.Height);
caClient:
ACtrl.SetBounds(R.Left, R.Top, R.Width, R.Height);
caCenter:
ACtrl.SetBounds(R.Left + (R.Width - ACtrl.Width) div 2,
R.Top + (R.Height - ACtrl.Height) div 2, ACtrl.Width, ACtrl.Height);
caVertCenter:
ACtrl.SetBounds(ACtrl.Left, R.Top + (R.Height - ACtrl.Height) div 2,
ACtrl.Width, ACtrl.Height);
caHorzCenter:
ACtrl.SetBounds(ACtrl.Left + (R.Width - ACtrl.Width) div 2, ACtrl.Top,
ACtrl.Width, ACtrl.Height);
caHorizontal:
ACtrl.SetBounds(R.Left, ACtrl.Top, R.Width, ACtrl.Height);
caVertical:
ACtrl.SetBounds(ACtrl.Left, R.Top, ACtrl.Width, R.Height);
end;
end;
end;当然,如果想要达到FMX的对齐方式的效果,从 TControl 级别直接实现当然是最好的,不过这里明显有点难度。
