You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何基于Inno Setup创建左侧显示安装步骤并高亮当前步骤的安装程序

嘿,这个需求在Inno Setup里完全能搞定!核心思路是通过自定义控件搭建左侧步骤面板,再结合页面切换事件来高亮当前步骤。下面给你一步步拆解具体实现:

实现步骤拆解

1. 定义步骤数据与控件变量

首先在脚本的[Code]段开头,我们需要定义存储步骤标题的常量数组,以及用来管理步骤标签的变量:

var
  StepLabels: array of TLabel; // 存储每个步骤的标签控件
  CurrentStep: Integer;        // 记录当前所处的步骤索引

// 这里替换成你需要的安装步骤标题
const
  StepTitles: array[0..4] of string = (
    '欢迎使用安装向导',
    '阅读许可证协议',
    '选择安装目录',
    '准备开始安装',
    '安装完成'
  );

2. 创建左侧步骤面板与标签

InitializeWizard事件里,我们创建左侧的面板容器,并逐个生成步骤标签:

procedure InitializeWizard;
var
  I: Integer;
  LeftStepPanel: TPanel;
begin
  // 创建左侧面板作为步骤列表的容器
  LeftStepPanel := TPanel.Create(WizardForm);
  LeftStepPanel.Parent := WizardForm;
  LeftStepPanel.Left := 0;
  LeftStepPanel.Top := 0;
  LeftStepPanel.Width := 190; // 可根据需求调整宽度
  LeftStepPanel.Height := WizardForm.ClientHeight;
  LeftStepPanel.Color := $00F5F5F5; // 浅灰背景,匹配大多数安装风格
  LeftStepPanel.BevelOuter := bvNone; // 去掉边框

  // 为每个步骤创建标签控件
  SetLength(StepLabels, Length(StepTitles));
  for I := 0 to High(StepTitles) do
  begin
    StepLabels[I] := TLabel.Create(WizardForm);
    StepLabels[I].Parent := LeftStepPanel;
    StepLabels[I].Left := 20;
    StepLabels[I].Top := 50 + I * 32; // 垂直间距,可调整
    StepLabels[I].Caption := StepTitles[I];
    StepLabels[I].Font.Size := 10;
    StepLabels[I].Font.Color := $00666666; // 默认灰色文字
    StepLabels[I].Width := LeftStepPanel.Width - 40;
    StepLabels[I].WordWrap := True; // 标题过长时自动换行
  end;

  // 调整向导主内容区域的位置,腾出左侧空间
  WizardForm.InnerNotebook.Left := LeftStepPanel.Width;
  WizardForm.InnerNotebook.Width := WizardForm.ClientWidth - LeftStepPanel.Width;
end;

3. 处理页面切换时的高亮逻辑

通过CurPageChanged事件,我们可以捕获页面切换动作,然后更新对应步骤标签的样式来实现高亮:

procedure CurPageChanged(CurPageID: Integer);
var
  I: Integer;
  TargetStepIndex: Integer;
begin
  // 将Inno默认的页面ID映射到我们的步骤索引
  case CurPageID of
    wpWelcome:    TargetStepIndex := 0;
    wpLicense:    TargetStepIndex := 1;
    wpSelectDir:  TargetStepIndex := 2;
    wpReady:      TargetStepIndex := 3;
    wpFinished:   TargetStepIndex := 4;
    // 如果有自定义页面,在这里添加映射,比如:
    // wpMyCustomPage: TargetStepIndex := 5;
  else
    TargetStepIndex := -1;
  end;

  // 重置所有步骤标签的样式
  for I := 0 to High(StepLabels) do
  begin
    StepLabels[I].Font.Style := [];
    StepLabels[I].Font.Color := $00666666;
    StepLabels[I].ParentColor := True; // 继承面板背景
  end;

  // 高亮当前步骤
  if (TargetStepIndex >= 0) and (TargetStepIndex <= High(StepLabels)) then
  begin
    StepLabels[TargetStepIndex].Font.Style := [fsBold]; // 加粗
    StepLabels[TargetStepIndex].Font.Color := $00007ACC; // 高亮蓝色(可自定义)
    StepLabels[TargetStepIndex].ParentColor := False;
    StepLabels[TargetStepIndex].Color := $00E8F4FF; // 高亮背景色(可选)
    CurrentStep := TargetStepIndex;
  end;
end;

4. 优化与自定义调整

你可以根据截图的风格做这些优化:

  • 调整面板的背景色、标签的字体大小/颜色,匹配目标风格
  • 如果步骤数量较多,把TPanel换成TScrollBox,实现步骤列表的滚动
  • 可以给高亮步骤添加图标,比如在标签左侧加TImage控件
  • 自定义页面的话,记得在CurPageChanged的case语句里添加对应的页面ID映射

把以上代码全部放到你的Inno Setup脚本的[Code]段中,编译运行后就能看到左侧带高亮的步骤列表了!

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

火山引擎 最新活动