You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Inno Setup中WizardStyle=modern时wpSelectDir页面BitmapImage无法缩放

解决Inno Setup中wpSelectDir页面BitmapImage无法缩放的问题

我帮你找出几个导致wpSelectDir页面BitmapImage无法缩放的关键问题,咱们一步步修正:

1. 无效的Center属性赋值

你代码里的Center := Center;完全是无效操作——相当于把属性值赋给自己,根本不会改变任何状态。这会导致图片的布局逻辑混乱,间接影响缩放表现。

2. 未指定控件尺寸

虽然你开启了Stretch := True,但AutoSize := False的情况下,必须明确给TBitmapImage设置Width和Height。否则控件不知道要把图片缩放到多大,自然不会有缩放效果。而wpWelcome页面的默认BitmapImage是Inno Setup已经绑定了页面尺寸的,所以能正常缩放。

3. 页面切换时未同步尺寸

在CurPageChanged中调整InnerNotebook布局后,没有同步更新BitmapImage的尺寸,导致图片无法适配新的页面空间。


修正后的完整代码

[Setup] 
WizardStyle=modern

[Code]
var
  biLeftSideImage: TBitmapImage; // 全局变量,确保在多个过程中能访问

function CreateBitmapImage(AParent: TWizardPage): TBitmapImage;
var
  ImgWidth, ImgHeight: Integer;
begin
  Result := TBitmapImage.Create(WizardForm);
  with Result do begin
    Parent := AParent.Surface;
    Bitmap.LoadFromFile(ExpandConstant('{tmp}\LefthandsideImg.bmp'));
    Stretch := True;
    AutoSize := False;
    // 修正Center赋值,设为True让图片在Surface区域居中(按需调整)
    Center := True;

    // 计算保持宽高比的最大尺寸,适配Surface区域
    ImgWidth := AParent.Surface.Width;
    ImgHeight := (Bitmap.Height * ImgWidth) div Bitmap.Width;
    // 如果高度超出Surface,再按高度适配
    if ImgHeight > AParent.Surface.Height then
    begin
      ImgHeight := AParent.Surface.Height;
      ImgWidth := (Bitmap.Width * ImgHeight) div Bitmap.Height;
    end;

    // 应用缩放后的尺寸
    Width := ScaleX(ImgWidth);
    Height := ScaleY(ImgHeight);
    // 手动居中(如果Center属性没生效的话)
    Left := (AParent.Surface.Width - Width) div 2;
    Top := (AParent.Surface.Height - Height) div 2;
  end;
end;

procedure SetSelectDirPageProps;
begin
  biLeftSideImage := CreateBitmapImage(PageFromID(wpSelectDir));
  // 调整其他控件布局(你的原有逻辑保留)
  WizardForm.SelectDirBitmapImage.Left := biLeftSideImage.Left + biLeftSideImage.Width + ScaleX(10);
  WizardForm.SelectDirLabel.Left := WizardForm.SelectDirBitmapImage.Left + WizardForm.SelectDirBitmapImage.Width + ScaleX(12);
  WizardForm.SelectDirLabel.Width := WizardForm.InnerNotebook.Width - WizardForm.SelectDirLabel.Left;
  WizardForm.SelectDirLabel.Autosize := True;
  WizardForm.SelectDirBrowseLabel.Left := WizardForm.SelectDirBitmapImage.Left;
  WizardForm.SelectDirBrowseLabel.Width := WizardForm.InnerNotebook.Width - WizardForm.SelectDirBrowseLabel.Left;
  WizardForm.SelectDirBrowseLabel.Autosize := True;
  WizardForm.DirEdit.Left := WizardForm.SelectDirBitmapImage.Left;
  WizardForm.DirEdit.Width := WizardForm.DirEdit.Width - ScaleX(150);
  WizardForm.DirBrowseButton.Left := WizardForm.DirEdit.Left + WizardForm.DirEdit.Width + ScaleX(10);
  WizardForm.DiskSpaceLabel.Left := WizardForm.DirEdit.Left;
  WizardForm.DiskSpaceLabel.Width := WizardForm.InnerNotebook.Width - WizardForm.DiskSpaceLabel.Left;
  WizardForm.DiskSpaceLabel.Top := WizardForm.InnerNotebook.Height - 2 * WizardForm.DiskSpaceLabel.Height;
  WizardForm.DiskSpaceLabel.Autosize := True;
end;

procedure InitializeWizard;
begin
  SetSelectDirPageProps;
end;

procedure CurPageChanged(CurPageID: Integer);
var
  ImgWidth, ImgHeight: Integer;
begin
  case CurPageID of
    wpSelectDir: begin
      biLeftSideImage.Visible := True;
      // 页面切换时同步BitmapImage尺寸到当前Surface
      ImgWidth := PageFromID(wpSelectDir).Surface.Width;
      ImgHeight := (biLeftSideImage.Bitmap.Height * ImgWidth) div biLeftSideImage.Bitmap.Width;
      if ImgHeight > PageFromID(wpSelectDir).Surface.Height then
      begin
        ImgHeight := PageFromID(wpSelectDir).Surface.Height;
        ImgWidth := (biLeftSideImage.Bitmap.Width * ImgHeight) div biLeftSideImage.Bitmap.Height;
      end;
      biLeftSideImage.Width := ScaleX(ImgWidth);
      biLeftSideImage.Height := ScaleY(ImgHeight);
      biLeftSideImage.Left := (PageFromID(wpSelectDir).Surface.Width - biLeftSideImage.Width) div 2;
      biLeftSideImage.Top := (PageFromID(wpSelectDir).Surface.Height - biLeftSideImage.Height) div 2;

      // 你的原有InnerNotebook调整逻辑
      WizardForm.InnerNotebook.Left := ScaleX(10);
      WizardForm.InnerNotebook.Width:= INNER_NOTEBOOK_WIDTH + 2 * INNER_NOTEBOOK_LEFT - WizardForm.InnerNotebook.Left;
      WizardForm.InnerNotebook.Top := WizardForm.MainPanel.Top + WizardForm.MainPanel.Height + ScaleY(10);
    end
    else begin
      biLeftSideImage.Visible := False;
      WizardForm.InnerNotebook.Left := INNER_NOTEBOOK_LEFT;
      WizardForm.InnerNotebook.Width := INNER_NOTEBOOK_WIDTH;
      WizardForm.InnerNotebook.Top := INNER_NOTEBOOK_TOP;
    end;
  end;
end;

额外说明

  • 如果不需要保持图片宽高比,直接把Width和Height设为AParent.Surface.Width和AParent.Surface.Height即可,图片会被拉伸填满整个Surface区域。
  • 之前设置AutoSize := True没效果,是因为这个属性会让控件适配图片的原始尺寸,而不是把图片缩放到控件尺寸——和Stretch的作用刚好相反,所以两者不要同时依赖。

内容的提问来源于stack exchange,提问作者JConstantine

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:45:45