Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi 5: How to suspend anchor layouts?

Is there a way to suspend all anchored controls on a form from moving or resizing themselves temporarily? i.e.:

procedure ScaleFormBy(AForm: TForm; n, d: Integer);
begin
    AForm.SuspendAnchors();
    try
       AForm.ScaleBy(n, d);
    finally
       AForm.ResumeAnchors();
    end;
end;

I need to do this because I'm calling

AForm.ScaleBy(m, d);

Which does not handle anchored controls properly. (it pushes left+right or top+bottom anchored controls off the edge of the form.

Note: I want to disable Anchors, not Alignment.

like image 855
Ian Boyd Avatar asked Dec 31 '22 04:12

Ian Boyd


1 Answers

SuspendAnchors sound like a base method but I don't think it's part of the base Delphi language :) Here is some code that does the trick:


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;

Enjoy

like image 111
Kris De Decker Avatar answered Jan 13 '23 12:01

Kris De Decker