Dynamically resize a form that matches the size of a frame in Delphi

微信扫一扫,分享到朋友圈

Dynamically resize a form that matches the size of a frame in Delphi

I have an application that has 5 different sized frames. I’d like to dynamically re-size the main form to fit the frame when I move from one frame to another.

I can use the MinHeight/MinWidth
properties of the frame to force the main form to fit the frame, but then when moving to a smaller frame, the main form does not adjust it’s size.

Any ideas?

–Edit

...
TFormMain = Class(TForm)
...
public
  FrameImportPackage: TFrameImportPackage;
...

procedure TFormMain.MenuPackagesImportClick(Sender: TObject);
begin
  if not (Assigned(FrameImportPackage)) then
    begin
      FrameImportPackage := TFrameImportPackage.Create(Self);
      FrameImportPackage.LabelFrameCaption.Caption := 'Import or Edit a Package';
    end
  else
    begin
      FrameImportPackage.BringToFront;
    end;

  FrameImportPackage.Parent := Self;
end;

–Edit

Regards, Pieter

If I understand your question correctly, you’ve got frames that don’t change size, you want the form to update size to fit your frames. Let Delphi handle that for you, using the AutoSize
property.

Set AutoSize = True
for your form.

I’ve tested AutoSize
with the following code, using Delphi 2010
:

  • Create a new VCL application. On the blank form drop a single Panel, let it keep it’s name ( Panel1
    ). Make sure the panel is not too small, because we’ll write code to decrease it’s size at runtime.
  • Set the form’s AutoSize
    property to True
    .
  • Drop two buttons on the panel, Button1
    and Button2
    .
  • Double click the buttons, and copy-paste the following event handlers:

Code:

procedure TForm31.Button1Click(Sender: TObject);
var NewR: TRect;
begin
  NewR := Panel1.BoundsRect;
  Dec(NewR.Right, 32);
  Dec(NewR.Bottom, 32);

  Button1.Parent := Self;
  Button2.Parent := Self;

  Panel1.Free;
  Panel1 := TPanel.Create(Self);
  Panel1.BoundsRect := NewR;
  Panel1.Parent := Self;

  Button1.Parent := Panel1;
  Button2.Parent := Panel1;
end;

procedure TForm31.Button2Click(Sender: TObject);
begin
  Panel1.Height := Panel1.Height - 32;
  Panel1.Width := Panel1.Width - 32;
end;

This essentially gives you two ways of decreasing the size of the panel, to handle two possible scenarios: Button1
frees the old panel and creates a new, smaller panel. Button2
directly resize the existing panel. Both work as expected!

微信扫一扫,分享到朋友圈

Dynamically resize a form that matches the size of a frame in Delphi

Second generation Threadripper CPUs spotted along with more Ryzen models

上一篇

学UI设计要花多少钱,UI设计培训价格怎么样?

下一篇

你也可能喜欢

Dynamically resize a form that matches the size of a frame in Delphi

长按储存图像,分享给朋友