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



目的:

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

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

问题:

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

代码:

这两个变量是:strData1&strData2

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

[Setup]
...
[Dirs]    
Name: "C:Program FilesCompanyName{code:strData1returner}"
[Files]
Source: "C:SomeDll.dll"; 
  DestDir: "C:Program FilesCompanyName{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 := 'SOFTWARECompanyName';
    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过程仅在值实际更改时执行,而不是在未通过任何操作接受默认值时执行。

此外,在试图重现您的问题时,我注意到一个语法问题,该问题会导致createvalueif不存在的编译错误createvalueif也不存在——当然,您只需要其中一个。

[Dirs]    
Name: "C:Program FilesCompanyName{code:strData1returner}"
[Files]
Source: "C:SomeDll.dll"; DestDir: "C:Program FilesCompanyName{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 := 'SOFTWARECompanyName';
    end;
    1:
    begin
      strData1 := 'Subfolder2';
      strData2 := 'SOFTWARECompanyName2';
    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 := 'SOFTWARECompanyName';
end;
function strData1returner(Param: String): String;
begin
  Result :=  strData1;
end;
function strData2returner(Param: String): String;
begin
  Result :=  strData2;
end;

上面的代码正在按我所期望的方式工作。

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

直接从脚本化常量实现访问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 := 'SOFTWARECompanyName1';
    1: Result := 'SOFTWARECompanyName2';
  end;
end;

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

#define CustomVarName "Value"

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

相关内容

  • 没有找到相关文章

最新更新