Inno Setup:如何在printer任务执行前、第二个程序安装后显示自定义页
解决方案:在指定Run任务间插入自定义向导页
要在第二个程序安装完成后、printer驱动安装前显示自定义向导页,你不能直接通过InitializeWizard将页面插入到wpInstalling等固定位置——因为[Run]段的所有任务都是在wpInstalling页面的执行阶段批量处理的,固定位置的自定义页无法精准插入到两个Run任务之间。
正确的做法是通过中断Run任务流,手动触发自定义页,再执行后续驱动安装,具体步骤如下:
1. 修改[Run]段配置
保留前两个程序的安装项,移除printer驱动的自动Run项,改为后续通过代码手动执行:
[Run] Filename: "{tmp}\First.exe"; WorkingDir: {app}; StatusMsg: First program install; Tasks: fp1; Filename: "{tmp}\Second.exe"; WorkingDir: {app}; StatusMsg: Second program install; Tasks: fp2; AfterInstall: ShowCustomDriverPage;
这里给第二个程序的Run项添加了AfterInstall: ShowCustomDriverPage,表示安装完成后执行自定义的ShowCustomDriverPage过程。
2. 完善[Code]段逻辑
调整代码,将自定义页的创建和显示逻辑分离,在ShowCustomDriverPage过程中显示页面,然后手动执行驱动安装:
[Code] var DriverWizardPage: TWizardPage; procedure InitializeWizard; var BitmapFileName: string; BitmapImage: TBitmapImage; begin // 提前创建自定义页,但不自动插入到向导流程中 DriverWizardPage := CreateCustomPage(wpNone, '', ''); // 加载并设置页面上的图片(和你原来的逻辑一致) BitmapFileName := ExpandConstant('{tmp}\image.bmp'); ExtractTemporaryFile(ExtractFileName(BitmapFileName)); BitmapImage := TBitmapImage.Create(DriverWizardPage); BitmapImage.AutoSize := True; BitmapImage.Bitmap.LoadFromFile(BitmapFileName); BitmapImage.Cursor := crHand; BitmapImage.Left := 10; BitmapImage.Top := 10; BitmapImage.Parent := DriverWizardPage.Surface; end; procedure ShowCustomDriverPage; var InstallResult: Integer; begin // 显示自定义向导页,直到用户点击下一步 WizardForm.ActivePage := DriverWizardPage; while WizardForm.ActivePage = DriverWizardPage do WizardForm.ProcessMessages; // 手动执行printer驱动安装任务 if WizardIsTaskSelected('printer') then begin WizardForm.StatusLabel.Caption := 'Drivers install'; WizardForm.ProgressGauge.Style := npbstNormal; InstallResult := Exec(ExpandConstant('{tmp}\Drivers\Install.exe'), '', ExpandConstant('{app}'), SW_SHOW, ewWaitUntilTerminated, nil); if InstallResult <> 0 then MsgBox('Driver installation failed!'#13#10'Error code: ' + IntToStr(InstallResult), mbError, MB_OK); end; end;
关键逻辑说明
CreateCustomPage(wpNone):创建自定义页但不将其自动加入向导的默认流程,这样我们可以手动控制它的显示时机。AfterInstall钩子:在第二个程序安装完成后触发自定义过程,此时刚好卡在两个Run任务的间隙。- 手动显示页面:通过
WizardForm.ActivePage切换到自定义页,循环处理消息直到用户离开该页面,保证用户必须完成该页操作后才会继续驱动安装。 - 手动执行驱动安装:通过
Exec函数模拟原来Run项的行为,同时保留Tasks: printer的任务判断逻辑,只有当用户勾选了printer任务时才执行。
这样就能精准实现第二个程序安装完成后 → 显示自定义向导页 → 用户操作后 → 执行printer驱动安装的流程。
内容的提问来源于stack exchange,提问作者Vadim Pavlovich
相关产品推荐
相关产品推荐

