仅当在 Inno setup 中选择特定组件时显示自定义页面并将输入保存到文件

Display custom page and save inputs to a file only when specific component is selected in Inno setup

我正在尝试为我的应用程序构建一个设置,它包含两部分:服务器和客户端。客户端部分需要有一个用户输入的 IP 地址。我正在使用自定义页面来提示输入 IP 地址。但是我需要显示自定义页面,只有当用户选择 "Client" 组件时。

[Components]
Name: "Serveur"; Description: "Server installation"; Types: Serveur; Flags: exclusive; 
Name: "Client"; Description: "Client installation"; Types: Client; Flags: exclusive

[Types]
Name: "Serveur"; Description: "Server Installation"
Name: "Client"; Description: "Client Installation"
[Code]                                                                                                                                    
var
  Page: TInputQueryWizardPage;
  ip: String;

procedure InitializeWizard();
begin
  Page := CreateInputQueryPage(wpWelcome,
    'IP Adresse du serveur', 'par exemple : 192.168.1.120',
    'Veuillez introduire l''adresse IP du serveur :');

  Page.Add('IP :', False);

  Page.Values[0] := ExpandConstant('192.168.x.x');
end;

function NextButtonClick(CurPageID: Integer): Boolean;
begin
  if (CurPageID = Page.ID) then
  begin
    ip := Page.Values[0];
    SaveStringToFile('C:\Program Files\AppClient\ipAddress.txt', ip, False);
  end;

  Result := True;
end;
  1. 您的自定义页面必须仅在 "Select Components" 页面之后,因此您需要将 wpSelectComponents 传递给 CreateInputQueryPage:

    var
      Page: TInputQueryWizardPage;
    
    procedure InitializeWizard();
    begin
      Page :=
        CreateInputQueryPage(
          wpSelectComponents, 'IP Adresse du serveur', 'par exemple : 192.168.1.120',
          'Veuillez introduire l''adresse IP du serveur :');
      Page.Add('IP :', False);
      Page.Values[0] := '192.168.x.x';
    end;
    

    (另请注意,在不包含任何常量的字符串文字上调用 ExpandConstant 是没有意义的)。

  2. 跳过自定义页面,未选中"Client"组件时:

    function IsClient: Boolean;
    begin
      Result := IsComponentSelected('Client');
    end;
    
    function ShouldSkipPage(PageID: Integer): Boolean;
    begin
      Result := False;
      if PageID = Page.ID then
      begin
        Result := not IsClient;
      end;
    end;
    

    另见 Skipping custom pages based on optional components in Inno Setup

  3. 在用户最终确认安装之前,行为良好的安装程序不应对系统进行任何修改。因此,仅在安装真正开始后进行任何更改,而不是当用户在自定义页面上单击 "Next" 时。

    此外,您不能硬编码文件路径,请使用 {app} 常量。

    procedure CurStepChanged(CurStep: TSetupStep);
    var
      IP: string;
    begin
      if (CurStep = ssInstall) and IsClient() then
      begin
        IP := Page.Values[0];
        SaveStringToFile(ExpandConstant('{app}\ipAddress.txt'), IP, False);
      end;
    end;