Workaround for anchors that break when reconstructing a window?

This happens in all Delphi prior to XE3:

  • Create a form and place a panel on it. Attach the panel to [akLeft, akTop, akRight, akBottom], but leave a space between it and the borders.
  • Add a button that calls RecreateWnd()
  • Launch the app. Resize the form so that the panel is hidden, as it is less than 0 pixels, depending on the snap. Click the RecreateWnd button.
  • Change the size of the form and note that the anchoring of the panel is broken.

As long as I remember myself using Delphi, because of this, the boxes were always inaccessible. Resize the form, then attach it: the window is recreated, your layout is broken.

I wonder if there is any workaround?

Update

There are two workarounds in the comments: one proven and stable, but with a blinking form, the other experimental, but potentially more thorough and clean.

I am not going to vote for any time, since one of them belongs to me, and I'm not even sure that it is stable. Instead, I will wait for some publication.

+5
source share
2 answers

Two options that I used, none of them are ideal for problems with the lower and right anchors:

  • Make the window large before invoking or invoking it RecreateWnd();, and then make it small again. Must be visible before you make it small again.
  • Define the shape constraints so that they cannot be redone so small that the material is 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;
+4

, , , UpdateAnchorRules. TControl FOriginalParentSize FAnchorRules . UpdateAnchorRules() Width Height FOriginalParentSize FAnchorRules.

, , .

Width - , Windows, , , Delphi 0. UpdateAnchorRules, out-of-accord 0 . .

( , Width Width - )

, UpdateAnchorRules : WinAPI CreateWindow, WM_SIZE ( WM_SIZE UpdateAnchorRules), -, CreateHandle .

, UpdateAnchorRules CreateHandle, . UpdateAnchorRules CreateHandle, , - , .

, , - , , , - ?

UpdateAnchorRules: FAnchorMove csLoading. - , , RecreateWnd, UpdateAnchorRules.

, :

type
  TComponentHack = class helper for TComponent
  public
    procedure SetCsLoading(Value: boolean);
  end;

procedure TComponentHack.SetCsLoading(Value: boolean);
var i: integer;
begin
  if Value then
    Self.FComponentState := Self.FComponentState + [csLoading]
  else
    Self.FComponentState := Self.FComponentState - [csLoading];
  for i := 0 to Self.ComponentCount-1 do
    if Self.Components[i] is TControl then
      TControl(Self.Components[i]).SetCsLoading(Value);
end;

procedure SafeRecreateWnd();
begin
  MyControl.SetCsLoading(true);
  try
    MyControl.RecreateWnd(); //or any operation which triggers it -- such as docking or making the window visible first time after RecreateWnd()
  finally
    MyControl.SetCsLoading(false);
  end;
end;

, , TControl csLoading.

UpdateAnchorRules , UpdateAnchorRules ( Delphi UpdateAnchorRules), - UpdateAnchorRules, hook.

0

All Articles