Inno Setup中TBitmapImage随窗口自动缩放问题求助
Inno Setup可调整向导窗口时TBitmapImage自动缩放问题修复
我在Inno Setup安装程序的欢迎页中添加了一个TBitmapImage控件。当将WizardResizable设置为True并调整向导窗口大小时,承载位图的控件边界会自动缩放,且背景颜色随之改变,效果如下:

这并非预期行为,我尝试过将AutoSize设为false、设置固定的Height和Width属性,以及测试不同的Anchors组合,但问题仍然存在。
以下是我的完整代码(AuthorWebsiteBitmap为目标位图):
[Setup] WizardResizable=yes WizardStyle=Classic [Code] // - - - - - - - - - - - - - - - - - - - - - - // // 创建作者网站相关控件 // // - - - - - - - - - - - - - - - - - - - - - - // procedure CreateAuthorControls(AuthorWebsiteUrl: String); var InstallerAuthorLabel: TNewStaticText; AuthorWebsiteLabel : TNewStaticText; AuthorWebsiteBitmap : TBitmapImage; begin // 设置AuthorWebsiteBitmap控件属性... AuthorWebsiteBitmap := TBitmapImage.Create(WizardForm); AuthorWebsiteBitmap.Parent := WizardForm.WelcomePage; AuthorWebsiteBitmap.AutoSize := True; AuthorWebsiteBitmap.Left := (WizardForm.WizardBitmapImage.Left + WizardForm.WizardBitmapImage.Width) + ScaleX(10); AuthorWebsiteBitmap.Top := (WizardForm.WelcomeLabel2.Top + WizardForm.WelcomeLabel2.Height) - (AuthorWebsiteBitmap.Height div 2) - ScaleX(16); AuthorWebsiteBitmap.Cursor := crHand; AuthorWebsiteBitmap.OnClick := @AuthorWebsiteControlClick; AuthorWebsiteBitmap.Anchors := [akLeft, akBottom]; AuthorWebsiteBitmap.Visible := (AuthorWebsiteUrl <> ''); ExtractTemporaryFiles('{tmp}\WizardBitmaps\AuthorWebsiteWhite.bmp'); AuthorWebsiteBitmap.Bitmap.LoadFromFile(ExpandConstant('{tmp}\WizardBitmaps\AuthorWebsiteWhite.bmp')); // 调整WelcomeLabel2高度以显示AuthorWebsiteBitmap控件 WizardForm.WelcomeLabel2.Height := WizardForm.WelcomeLabel2.Height - (AuthorWebsiteBitmap.Height + ScaleY(8)); // 此代码无效... // AuthorWebsiteBitmap.BringToFront(); // 设置AuthorWebsiteLabel控件属性 AuthorWebsiteLabel := TNewStaticText.Create(WizardForm); AuthorWebsiteLabel.Parent := WizardForm.WelcomePage; AuthorWebsiteLabel.Left := AuthorWebsiteBitmap.Left; AuthorWebsiteLabel.Top := AuthorWebsiteBitmap.Top - ScaleY(18); AuthorWebsiteLabel.Cursor := crHand; AuthorWebsiteLabel.OnClick := @AuthorWebsiteControlClick; AuthorWebsiteLabel.Anchors := [akLeft, akBottom]; AuthorWebsiteLabel.Visible := (AuthorWebsiteUrl <> ''); AuthorWebsiteLabel.Caption := CustomMessage('SetupOpenAuthorWebsite'); // 设置InstallerAuthorLabel控件属性 InstallerAuthorLabel := TNewStaticText.Create(WizardForm); InstallerAuthorLabel.Parent := WizardForm; InstallerAuthorLabel.Left := ScaleX(2); InstallerAuthorLabel.Top := WizardForm.NextButton.Top + WizardForm.NextButton.Height div 2 + ScaleY(10) - ScaleY(2); InstallerAuthorLabel.Anchors := [akLeft, akBottom]; InstallerAuthorLabel.Caption := CustomMessage('SetupMadeBy'); end; <event('InitializeWizard')> procedure InitializeWizard1(); begin CreateAuthorControls(ExpandConstant('{#AuthorWebsite}')); end;
更新
我通过以下方式缓解了背景变色问题,但理想方案是完全避免控件自动缩放:
AuthorWebsiteBitmap.BackColor := TNewNotebookPage(AuthorWebsiteBitmap.Parent).Color;
修复方案
要彻底解决控件自动缩放问题,需按以下步骤调整代码:
加载位图后固定控件尺寸
先加载位图,再关闭AutoSize并将控件尺寸设置为位图的实际大小,确保控件不会随窗口拉伸:ExtractTemporaryFiles('{tmp}\WizardBitmaps\AuthorWebsiteWhite.bmp'); AuthorWebsiteBitmap.Bitmap.LoadFromFile(ExpandConstant('{tmp}\WizardBitmaps\AuthorWebsiteWhite.bmp')); // 关闭自动缩放并固定控件尺寸为位图尺寸 AuthorWebsiteBitmap.AutoSize := False; AuthorWebsiteBitmap.Width := AuthorWebsiteBitmap.Bitmap.Width; AuthorWebsiteBitmap.Height := AuthorWebsiteBitmap.Bitmap.Height;调整位置计算时机
当前代码在加载位图前就计算了Top值,但此时AuthorWebsiteBitmap.Height还未被正确赋值(位图未加载),导致初始位置可能偏移。建议将位置计算移到位图加载完成后:// 先加载位图并固定尺寸,再计算位置 ExtractTemporaryFiles('{tmp}\WizardBitmaps\AuthorWebsiteWhite.bmp'); AuthorWebsiteBitmap.Bitmap.LoadFromFile(ExpandConstant('{tmp}\WizardBitmaps\AuthorWebsiteWhite.bmp')); AuthorWebsiteBitmap.AutoSize := False; AuthorWebsiteBitmap.Width := AuthorWebsiteBitmap.Bitmap.Width; AuthorWebsiteBitmap.Height := AuthorWebsiteBitmap.Bitmap.Height; // 重新计算Top位置 AuthorWebsiteBitmap.Top := (WizardForm.WelcomeLabel2.Top + WizardForm.WelcomeLabel2.Height) - (AuthorWebsiteBitmap.Height div 2) - ScaleX(16);同步背景色
在设置父控件后直接同步背景色,避免窗口缩放时背景色不一致:AuthorWebsiteBitmap.Parent := WizardForm.WelcomePage; AuthorWebsiteBitmap.BackColor := WizardForm.WelcomePage.Color;
内容的提问来源于stack exchange,提问作者ElektroStudios
相关产品推荐
相关产品推荐

