Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Workaround for anchors being broken when recreating a window?

Tags:

delphi

This happens in all Delphi up to XE3:

  1. Create a form and put a panel on it. Anchor the panel to [akLeft, akTop, akRight, akBottom], but leave space between it and the borders.
  2. Add a button which calls RecreateWnd()
  3. Run the app. Resize the form so that the panel is hidden because it's less than 0 pixels in size due to anchoring. Press the RecreateWnd button.
  4. Resize the form back and note that the panel's anchoring is broken.

As long as I can remember myself using Delphi, anchors were always impossible to use because of this. Resize the form, then dock it: the window is recreated, your layout is broken.

I wonder if there's some sort of workaround?

Update

Two workarounds are available in the comments, one proven and stable but with form blinking, another one experimental but potentially more thorough and clean.

I'm not going to vote for either one for a while, as one of those is mine and I'm not even sure it is stable. Instead, I'll wait for some public input.

like image 339
himself Avatar asked Oct 04 '22 22:10

himself


1 Answers

The two options I have used of which neither is really ideal for problems with the bottom and right anchors are:

  1. Make the window big again before calling or causing to be called RecreateWnd();, then make it small again. Has to be visible before you make it small again however.
  2. Set the Form's constraints so it can't be re-sized so small that stuff ends up hidden.

An example that flashes the larger form, use height and width large values enough so the panel is not hidden:

procedure TForm1.Button1Click(Sender: TObject);
Var
  OldWidth, OldHeight : integer;
begin
  OldWidth := Form1.Width;
  OldHeight := Form1.Height;
  Form1.Visible := false;
  Form1.Width := 1000;
  Form1.Height := 800;
  RecreateWnd();
  Form1.Visible := true;
  Form1.Width := OldWidth;
  Form1.Height := OldHeight;
end;
like image 180
Brian Avatar answered Oct 10 '22 04:10

Brian