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

如何将Inno Setup安装类型下拉框改为独立页面单选按钮(类Install Shield)

把Inno Setup的安装类型下拉框改成单选按钮+组件列表样式

哈哈,我太懂这种需求了——默认Inno Setup那个下拉框选安装类型确实有点生硬,想要改成那种直观的单选按钮+组件列表的组合,选自定义的时候还能自己挑组件是吧?我给你整理一套可行的方案:

第一步:保留基础的Types和Components配置

先把你现有的配置保留好,这部分是核心的类型和组件关联逻辑:

[Types]
Name: "full"; Description: "Full Installation"
Name: "compact"; Description: "Lite Installation"
Name: "custom"; Description: "Custom Installation"; Flags: iscustom

[Components]
Name: "One"; Description: "First"; Types: full compact custom; Flags: fixed
Name: "Two"; Description: "Second"; Types: full
Name: "Three"; Description: "Third"; Types: full
Name: "Four"; Description: "Fourth"; Types: full
Name: "Five"; Description: "Fifth"; Types: full

第二步:添加自定义页面和交互脚本

我们需要用Inno Setup的Pascal脚本创建一个自定义页面,把单选按钮和组件列表整合到一起,还要实现类型选择和组件状态的联动。在脚本的[Code]段里加入以下代码:

[Code]
var
  FullRadio: TRadioButton;
  CompactRadio: TRadioButton;
  CustomRadio: TRadioButton;
  ComponentList: TNewCheckListBox;
  CustomInstallPage: TWizardPage;

procedure InitializeWizard;
begin
  // 创建自定义安装选择页面,替换默认的类型选择和组件页
  CustomInstallPage := CreateCustomPage(wpSelectComponents, 'Choose Installation Type', 
    'Pick the installation type that fits your needs.');

  // 构建完整安装单选按钮
  FullRadio := TRadioButton.Create(CustomInstallPage);
  FullRadio.Parent := CustomInstallPage.Surface;
  FullRadio.Caption := 'Full Installation';
  FullRadio.Top := 10;
  FullRadio.Left := 10;
  FullRadio.Checked := True; // 默认选中完整安装

  // 构建精简安装单选按钮
  CompactRadio := TRadioButton.Create(CustomInstallPage);
  CompactRadio.Parent := CustomInstallPage.Surface;
  CompactRadio.Caption := 'Lite Installation';
  CompactRadio.Top := FullRadio.Top + FullRadio.Height + 8;
  CompactRadio.Left := 10;

  // 构建自定义安装单选按钮
  CustomRadio := TRadioButton.Create(CustomInstallPage);
  CustomRadio.Parent := CustomInstallPage.Surface;
  CustomRadio.Caption := 'Custom Installation';
  CustomRadio.Top := CompactRadio.Top + CompactRadio.Height + 8;
  CustomRadio.Left := 10;

  // 构建组件选择列表
  ComponentList := TNewCheckListBox.Create(CustomInstallPage);
  ComponentList.Parent := CustomInstallPage.Surface;
  ComponentList.Top := CustomRadio.Top + CustomRadio.Height + 16;
  ComponentList.Left := 10;
  ComponentList.Width := CustomInstallPage.Surface.Width - 20;
  ComponentList.Height := CustomInstallPage.Surface.Height - ComponentList.Top - 10;
  ComponentList.Style := csCheckListBox;

  // 把预设的组件加载到列表里
  LoadComponentList(ComponentList, True);

  // 初始状态下组件列表不可编辑(因为默认选了完整安装)
  ComponentList.Enabled := False;
end;

// 单选按钮点击后的处理逻辑
procedure RadioButtonClicked(Sender: TObject);
begin
  if FullRadio.Checked then begin
    // 切换到完整安装类型,锁定组件列表
    WizardForm.TypesCombo.ItemIndex := WizardForm.TypesCombo.Items.IndexOf('full');
    ComponentList.Enabled := False;
    CheckComponents('full'); // 自动勾选完整安装对应的组件
  end 
  else if CompactRadio.Checked then begin
    // 切换到精简安装类型,锁定组件列表
    WizardForm.TypesCombo.ItemIndex := WizardForm.TypesCombo.Items.IndexOf('compact');
    ComponentList.Enabled := False;
    CheckComponents('compact'); // 自动勾选精简安装对应的组件
  end 
  else if CustomRadio.Checked then begin
    // 切换到自定义安装类型,解锁组件列表
    WizardForm.TypesCombo.ItemIndex := WizardForm.TypesCombo.Items.IndexOf('custom');
    ComponentList.Enabled := True;
  end;
end;

// 页面切换时绑定事件
procedure CurPageChanged(CurPageID: Integer);
begin
  if CurPageID = CustomInstallPage.ID then begin
    FullRadio.OnClick := @RadioButtonClicked;
    CompactRadio.OnClick := @RadioButtonClicked;
    CustomRadio.OnClick := @RadioButtonClicked;
  end;
end;

// 辅助函数:根据安装类型自动勾选对应组件
procedure CheckComponents(TypeID: String);
var
  I: Integer;
begin
  for I := 0 to ComponentList.Items.Count - 1 do begin
    ComponentList.Checked[I] := IsComponentSelectedForType(ComponentList.Items[I], TypeID);
  end;
end;

第三步:隐藏默认的旧页面

最后在[Setup]段里添加配置,把原来的类型选择页和组件选择页隐藏掉,让自定义页面取而代之:

[Setup]
; 其他原有配置...
DisablePage=wpSelectType
DisablePage=wpSelectComponents

这样设置之后,你的安装程序就会呈现出想要的效果:顶部是三个单选按钮,选「完整」或「精简」时,下方组件列表是锁定状态,自动勾选对应类型的组件;选「自定义」时,组件列表解锁,用户可以自由勾选需要的组件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 07:26:51