Size a Delphi Form to fit an ExpressLayout Control’s Content

Today I was faced with the need to have some MDI child forms automatically size to fit their contents. These forms all descend from a base form that has an ExpressBars bar manager and an ExpressLayout layout control. I was surprised (though I shouldn’t be at this point) at how easy this was to achieve:

procedure TDataDetailsForm.FormShow(Sender: TObject); 
begin 
  inherited;      
  SizeToRootLayoutContents; 
end;

procedure TDataDetailsForm.SizeToRootLayoutContents; 
begin 
  ClientWidth := DetailsLayoutControlGroup_Root.ViewInfo.Bounds.Right - 
    DetailsLayoutControlGroup_Root.ViewInfo.Bounds.Left + 
    (DetailsLayoutControl.LayoutLookAndFeel.Offsets.RootItemsAreaOffsetHorz * 2); 
  ClientHeight := DetailsLayoutControlGroup_Root.ViewInfo.Bounds.Bottom - 
    DetailsLayoutControlGroup_Root.ViewInfo.Bounds.Top + 
    (DetailsLayoutControl.LayoutLookAndFeel.Offsets.RootItemsAreaOffsetVert * 2) + 
    DetailsBarManagerBar1.RealDockControl.Height; 
end;

With that bit of code in place, each form now automatically size to fit its contents when shown, but can still be freely resized.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s