使用组合框 select 要使用的路径和注册表项

Using combo box to select what paths and registry keys to use

用途:

我想使用 Inno Setup 在安装向导中创建一个带有下拉列表的安装程序。

向导中的选择定义了 2 个变量:
一个用于文件夹位置,另一个用于注册表中的位置。

问题:

变量在 [Code] 中定义为全局变量,但在 [Files][Registry]

中未使用

代码:

两个变量是:strData1 & strData2

我试图用以下函数检索它们:strData1returner & strData2returner

[Setup]
...

[Dirs]    
Name: "C:\Program Files\CompanyName\{code:strData1returner}"

[Files]
Source: "C:\SomeDll.dll"; \
  DestDir: "C:\Program Files\CompanyName\{code:strData1returner}\"; Flags: ignoreversion;

[Registry]
Root: HKCU; subkey: "{code:strData2returner}"; flags: createvalueifdoesntexist

[Code]
var
  Button: TNewButton;
  ComboBox: TNewComboBox;
  CustomPage: TWizardPage;     
  strData1: string;
  strData2: string;

procedure ComboBoxChange(Sender: TObject);
begin
  case ComboBox.ItemIndex of
    0:
    begin
      strData1 := 'Subfolder';
      strData2 := 'SOFTWARE\CompanyName';
    end;
  end;
end;

procedure InitializeWizard;
var
  DescLabel: TLabel;
begin
  CustomPage := CreateCustomPage(wpSelectDir, 'Caption', 'Description');

  DescLabel := TLabel.Create(WizardForm);
  DescLabel.Parent := CustomPage.Surface;
  DescLabel.Left := 0;
  DescLabel.Top := 0;
  DescLabel.Caption := 'Select an item...';

  ComboBox := TNewComboBox.Create(WizardForm);
  ComboBox.Parent := CustomPage.Surface;
  ComboBox.Left := 0;
  ComboBox.Top := DescLabel.Top + DescLabel.Height + 6;  
  ComboBox.Width := 220;
  ComboBox.Style := csDropDownList;
  ComboBox.Items.Add('Choice1');
  ComboBox.ItemIndex := 0;
  ComboBox.OnChange := @ComboBoxChange;
end;

function strData1returner(Param: String): String;
begin
  Result :=  strData1;
end;

function strData2returner(Param: String): String;
begin
  Result :=  strData2;
end;

只有一个选项:"Choice1" - 所以我在索引 1 下添加了第二个 (Choice2) 和第二个选项的替代值。

您还应该在 InitializeWizard 中初始化这两个变量,以防用户不更改组合框值。 ComboBoxChange 过程仅在值实际更改时执行,而不是在未通过任何操作接受默认值时执行。

此外,在尝试重现您的问题时,我注意到一个语法问题导致 createvalueifdoesntexist createvalueifdoesntexist 出现编译错误 - 当然,您只需要其中之一。

[Dirs]    
Name: "C:\Program Files\CompanyName\{code:strData1returner}"

[Files]
Source: "C:\SomeDll.dll"; DestDir: "C:\Program Files\CompanyName\{code:strData1returner}\"; Flags: ignoreversion;

[Registry]
Root: HKCU; subkey:"{code:strData2returner}"; flags: createvalueifdoesntexist

[Code]
var
  Button: TNewButton;
  ComboBox: TNewComboBox;
  CustomPage: TWizardPage;     
  strData1: string;
  strData2: string;

procedure ComboBoxChange(Sender: TObject);
begin
  case ComboBox.ItemIndex of
    0:
    begin
      strData1 := 'Subfolder';
      strData2 := 'SOFTWARE\CompanyName';
    end;
    1:
    begin
      strData1 := 'Subfolder2';
      strData2 := 'SOFTWARE\CompanyName2';
    end;
  end;
end;

procedure InitializeWizard;
var
  DescLabel: TLabel;
begin
  CustomPage := CreateCustomPage(wpSelectDir, 'Caption', 'Description');

  DescLabel := TLabel.Create(WizardForm);
  DescLabel.Parent := CustomPage.Surface;
  DescLabel.Left := 0;
  DescLabel.Top := 0;
  DescLabel.Caption := 'Select an item...';

  ComboBox := TNewComboBox.Create(WizardForm);
  ComboBox.Parent := CustomPage.Surface;
  ComboBox.Left := 0;
  ComboBox.Top := DescLabel.Top + DescLabel.Height + 6;  
  ComboBox.Width := 220;
  ComboBox.Style := csDropDownList;
  ComboBox.Items.Add('Choice1');
  ComboBox.Items.Add('Choice2');
  ComboBox.ItemIndex := 0;
  ComboBox.OnChange := @ComboBoxChange;
  strData1 := 'Subfolder';
  strData2 := 'SOFTWARE\CompanyName';
end;

function strData1returner(Param: String): String;
begin
  Result :=  strData1;
end;

function strData2returner(Param: String): String;
begin
  Result :=  strData2;
end;

上面的代码可以正常工作,正如我认为的那样。

您可以像这样创建全局变量:

#define CustomVarName "Value"

关于这里的更多具体信息:#define

实际上,您尝试使用全局变量只会使代码复杂化。

直接从 scripted constant 实现访问 ComboBox.ItemIndex,例如:

[Dirs]    
Name: "{app}\{code:GetSelectedSubfolder}"

[Files]
Source: "C:\SomeDll.dll"; DestDir: "{app}\{code:GetSelectedSubfolder}"

[Registry]
Root: HKCU; Subkey: "{code:GetSelectedSubkey}"

[Code]

var
  ComboBox: TNewComboBox;

procedure InitializeWizard;
{ ... }
begin
  { ... }
  ComboBox := TNewComboBox.Create(WizardForm);
  { ... }
  ComboBox.Items.Add('Choice1');
  ComboBox.Items.Add('Choice2');
  ComboBox.ItemIndex := 0;
end;

function GetSelectedSubfolder(Param: String): String;
begin
  case ComboBox.ItemIndex of
    0: Result := 'Subfolder1';
    1: Result := 'Subfolder2';
  end;
end;

function GetSelectedSubkey(Param: String): String;
begin
  case ComboBox.ItemIndex of
    0: Result := 'SOFTWARE\CompanyName1';
    1: Result := 'SOFTWARE\CompanyName2';
  end;
end;