How to work around AutoSize's being off while invisible?

Delphi controls have AutoSize property which is exposed e.g. in TPanel. It adjusts the width/height of the panel depending on the content.

Apparently it does nothing when the panel is invisible, and does no realigning later when it's set to visible. So if I put some controls into it and then make it visible, the size is not adjusted.

I can trigger adjusting size by setting height to any value in FormShow:

procedure TForm1.FormShow(Sender: TObject);
begin
  Panel1.Height := Panel1.Height + 1; //triggers auto-resize
end;

But I have to do this manually for every control which has AutoSize on. I'm bound to forget something.

Are there better ways to fix this, preferably once and for all?

Answers


I don't think there's much that you can do. A better way to for the re-sizing is to add a call to the Realign method of the panel immediately after you make it visible.

You could hook into the CM_VISIBLECHANGED message and force the matter there, for auto-sized controls. For instance, using an interceptor:

type
  TPanel = class(Vcl.ExtCtrls.TPanel)
  protected
    procedure CMVisibleChanged(var Message: TMessage); message CM_VISIBLECHANGED;
  end;

procedure TPanel.CMVisibleChanged(var Message: TMessage);
begin
  inherited;
  if Visible and AutoSize then
    Realign;
end;

Its been a while since i used delphi, but one thing i remember is that the controls did play a bit of mind games on me, most of the time it was because the rendering engine did not refresh the form and the controls. If you have the control set to auto-resize i would suggest checking if form1.refresh or panel1.refresh since its been a few years since i played with it ( delphi 7 ) i might confuse refresh with repaint. which some controls had, which initiated the size calculation before repainting the control. since delphi controls are open source you can go in to the apropriate pas file to find the control and look at refresh/repaint to see if you can persist the auto-resizing.

hope that helped.


Need Your Help

how to set html page size and page component with different screen resolution?

css html5 responsive-design media-queries

Page body get the width as screens size. Then how to other components of the page sizing with the relevant screen size ?