有没有办法暂停表单上的所有锚定控件暂时移动或调整大小?即:
procedure ScaleFormBy(AForm: TForm; n, d: Integer);
begin
AForm.SuspendAnchors();
try
AForm.ScaleBy(n, d);
finally
AForm.ResumeAnchors();
end;
end;
我需要这样做,因为我正在打电话
AForm.ScaleBy(m, d);
哪个不能正确处理锚定控件。 (它将左+右或顶部+底部锚定控件从表单边缘推出。
注意:我想要停用锚点,而不是对齐。
答案 0 :(得分:6)
SuspendAnchors听起来像一个基本方法,但我不认为它是基础Delphi语言的一部分:) 这里有一些代码可以解决这个问题:
var aAnchorStorage: Array of TAnchors;
procedure AnchorsDisable(AForm: TForm);
var
iCounter: integer;
begin
SetLength(aAnchorStorage, AForm.ControlCount);
for iCounter := 0 to AForm.ControlCount - 1 do begin
aAnchorStorage[iCounter] := AForm.Controls[iCounter].Anchors;
AForm.Controls[iCounter].Anchors := [];
end;
end;
procedure AnchorsEnable(AForm: TForm);
var
iCounter: integer;
begin
SetLength(aAnchorStorage, AForm.ControlCount);
for iCounter := 0 to AForm.ControlCount - 1 do
AForm.Controls[iCounter].Anchors := aAnchorStorage[iCounter];
end;
procedure TForm1.btnAnchorsDisableClick(Sender: TObject);
begin
AnchorsDisable(Self);
end;
procedure TForm1.btnAnchorsEnableClick(Sender: TObject);
begin
AnchorsEnable(Self);
end;
享受
答案 1 :(得分:4)
盖伊有一个好主意,但它没有处理儿童控制(即TPanel,TPageControl等)
这是一个使用递归的变体。另外,请注意我实际上并没有禁用锚点 - 事实证明,ScaleBy也不能用于锚点。
现在您可以使用以下方式缩放表单:
procedure ScaleFormBy(AForm: TForm; M, D: Integer);
var
StoredAnchors: TAnchorsArray;
begin
StoredAnchors := DisableAnchors(AForm);
try
AForm.ScaleBy(M, D);
finally
EnableAnchors(AForm, StoredAnchors);
end;
end;
使用支持库函数:
TAnchorsArray = array of TAnchors;
function DisableAnchors(ParentControl: TWinControl): TAnchorsArray;
var
StartingIndex: Integer;
begin
StartingIndex := 0;
DisableAnchors_Core(ParentControl, Result, StartingIndex);
end;
procedure EnableAnchors(ParentControl: TWinControl; aAnchorStorage: TAnchorsArray);
var
StartingIndex: Integer;
begin
StartingIndex := 0;
EnableAnchors_Core(ParentControl, aAnchorStorage, StartingIndex);
end;
procedure DisableAnchors_Core(ParentControl: TWinControl; var aAnchorStorage: TAnchorsArray; var StartingIndex: Integer);
var
iCounter: integer;
ChildControl: TControl;
begin
if (StartingIndex+ParentControl.ControlCount+1) > (Length(aAnchorStorage)) then
SetLength(aAnchorStorage, StartingIndex+ParentControl.ControlCount+1);
for iCounter := 0 to ParentControl.ControlCount - 1 do
begin
ChildControl := ParentControl.Controls[iCounter];
aAnchorStorage[StartingIndex] := ChildControl.Anchors;
if ([akLeft, akRight ] * ChildControl.Anchors) = [akLeft, akRight] then
ChildControl.Anchors := ChildControl.Anchors - [akRight];
if ([akTop, akBottom] * ChildControl.Anchors) = [akTop, akBottom] then
ChildControl.Anchors := ChildControl.Anchors - [akBottom];
Inc(StartingIndex);
end;
//Add children
for iCounter := 0 to ParentControl.ControlCount - 1 do
begin
ChildControl := ParentControl.Controls[iCounter];
if ChildControl is TWinControl then
DisableAnchors_Core(TWinControl(ChildControl), aAnchorStorage, StartingIndex);
end;
end;
procedure EnableAnchors_Core(ParentControl: TWinControl; aAnchorStorage: TAnchorsArray; var StartingIndex: Integer);
var
iCounter: integer;
ChildControl: TControl;
begin
for iCounter := 0 to ParentControl.ControlCount - 1 do
begin
ChildControl := ParentControl.Controls[iCounter];
ChildControl.Anchors := aAnchorStorage[StartingIndex];
Inc(StartingIndex);
end;
//Restore children
for iCounter := 0 to ParentControl.ControlCount - 1 do
begin
ChildControl := ParentControl.Controls[iCounter];
if ChildControl is TWinControl then
EnableAnchors_Core(TWinControl(ChildControl), aAnchorStorage, StartingIndex);
end;
end;
end;