Search code examples
widthinno-setuppascalscript

Setting control width to half of custom page SurfaceWidth does not work correctly in Inno Setup


I placed a Panel on my custom page and gave it a width of SurfaceWidth. Then I changed its width to SurfaceWidth div 2. Here's the result:

enter image description here

As you can see from the screenshot the new panel's width is definitely not equal to SurfaceWidth div 2. Why is that so?

Here's the code:

[Setup]
WizardStyle=modern

[Code]
procedure InitializeWizard();
var
  Page: TWizardPage;
  Panel: TPanel;
begin
  Page := CreateCustomPage(wpWelcome, 'Custom wizard page controls', 'TButton and others');
  Panel := TPanel.Create(Page);
  Panel.Width := Page.SurfaceWidth div 2;
  Panel.Left := 0;
  Panel.Height := 46;
  Panel.Anchors := [akLeft, akTop, akRight];
  Panel.Caption := 'TPanel';
  Panel.Color := clWindow;
  Panel.BevelKind := bkFlat;
  Panel.BevelOuter := bvNone; 
  Panel.ParentBackground := False;
  Panel.Parent := Page.Surface;
end;

Solution

  • That's because of the akRight in Panel.Anchors and modern WizardStyle (or rather the 120 WizardSizePercent it implies). The wizard is scaled only after the InitializeWizard. With akRight, the panel width will grow linearly (not proportionally) with the wizard. There are solutions, but they depend on how you actually want the panel to behave in the resizable wizard (also implied by the modern style).

    See also Inno Setup - how to center an animated gif in resized wizard.

    If you want to keep the panel at half size, when the wizard is resized (either automatically due to WizardSizePercent or by the user due to WizardResizable), handle WizardForm.OnResize:

    [Code]
    var
      Page: TWizardPage;
      Panel: TPanel;
    
    procedure WizardFormResize(Sender: TObject);
    begin
      Panel.Width := Page.SurfaceWidth div 2;
    end;
    
    procedure InitializeWizard();
    begin
      Page := CreateCustomPage(
        wpWelcome, 'Custom wizard page controls', 'TButton and others');
      Panel := TPanel.Create(Page);
      Panel.Width := Page.SurfaceWidth div 2;
      // ...
      WizardForm.OnResize := @WizardFormResize;
    end;
    

    Make sure you do not set the akRight anchor.