Inno Setup - 根据任务选择有条件地 hide/show 静态文本

Inno Setup - Conditionally hide/show static text based on task selection

[Components]
Name: "Slasher"; Description: "Dagon Slasher"; Types: Slasher Full
Name: "Frankenstein"; Description: "Dagon Frankenstein"; Types: Frankenstein Full

[Types]
Name: "Full"; Description: "Dagon Video Tools"
Name: "Slasher"; Description: "Dagon Slasher"
Name: "Frankenstein"; Description: "Dagon FrankenStein"

[Tasks]
Name: "Debug"; Description: "Nothing"; Components: not Slasher
Name: "Vid"; Description: "Install Extra Codecs for Frankenstein"; Flags: unchecked; Components: not Slasher

[Code]
var
  Warning: TNewStaticText;

procedure InitializeWizard;
begin
  Warning := TNewStaticText.Create(WizardForm);
  Warning.Parent := WizardForm.SelectTasksPage;
  Warning.Visible := False;
  Warning.AutoSize := False;
  Warning.SetBounds(
    WizardForm.TasksList.Left,
    WizardForm.TasksList.Top + WizardForm.TasksList.Height,
    WizardForm.TasksList.Width,
    50
  );
  Warning.Font.Color := clRed;
  Warning.Caption := 'Warning: This will result in a non-functional "Join in FrankenStein" button in the Tools Menu.';
end;

我又用到了另一块很棒的code by TLama。问题是我需要当用户选择任务时注释可见,否则隐藏(在同一页面上)。

您必须处理 WizardForm.TasksList.OnClickCheck 事件并相应地更新 Warning 标签可见性。

var
  Warning: TNewStaticText;

procedure TasksListClickCheck(Sender: TObject);
begin
  Warning.Visible :=
    { This (and the task index below)  has to be kept in sync with the expression }
    { in "Components" parameter of the respective task. }
    { Though note that in your specific case the test }
    { is redundant as when "Slasher" is selected, you have no tasks, }
    { and the "Tasks" page is completely skipped, so you do not even get here. }
    (not IsComponentSelected('Slasher')) and
    WizardForm.TasksList.Checked[0]; { You can also use WizardIsTaskSelected }
end;

procedure InitializeWizard;
begin
  Warning := TNewStaticText.Create(WizardForm);
  ...
  { Update Warning label visibility on task selection change }
  WizardForm.TasksList.OnClickCheck := @TasksListClickCheck
end;

procedure CurPageChanged(CurPageID: Integer);
begin
  if CurPageID = wpSelectTasks then
  begin
    { Update initial visibility }
    TasksListClickCheck(WizardForm.TasksList);
  end;
end;


旁注:

  • 不要将高度硬编码为固定 50。改为使用 DPI 缩放它:ScaleY(50).
  • 您应该设置 Warning.WordWrap := True,因为标题不适合页面宽度。
  • 您应该缩小 TasksList 的高度,因为标签不适合列表下方。您缺少 @TLama 代码中的 WizardForm.TasksList.Height := WizardForm.TasksList.Height - NoteHeight; 。再次注意他缺少 NoteHeight 的缩放比例。
const
  NoteHeight = 50;

procedure InitializeWizard;
begin
  WizardForm.TasksList.Height := WizardForm.TasksList.Height - ScaleY(NoteHeight);

  Warning := TNewStaticText.Create(WizardForm);
  Warning.Parent := WizardForm.SelectTasksPage;
  Warning.AutoSize := False;
  Warning.WordWrap := True;
  Warning.SetBounds(
    WizardForm.TasksList.Left,
    WizardForm.TasksList.Top + WizardForm.TasksList.Height,
    WizardForm.TasksList.Width,
    ScaleY(NoteHeight)
  );
  Warning.Font.Color := clRed;
  { Update Warning label visibility on task selection change }
  WizardForm.TasksList.OnClickCheck := @TasksListClickCheck
  Warning.Caption :=
    'Warning: This will result in a non-functional "Join in FrankenStein" button ' +
    'in the Tools Menu.';
end;