首页 > 解决方案 > 从注册表中检索组合框的值,并通过覆盖安装程序第二次运行的默认值来填充它

问题描述

我已经有combobox一个TInputQueryWizardPage页面,但问题是我不知道如何在第一次运行写入后从注册表中检索选定的值。

我的组合框代码是:

  AuthComboBox := TNewComboBox.Create(ReportPage);
  AuthComboBox.Parent := ReportPage.Edits[1].Parent;
  AuthComboBox.Left := ReportPage.Edits[1].Left;
  AuthComboBox.Top := ReportPage.Edits[1].Top;
  AuthComboBox.Width := ReportPage.Edits[1].Width;
  AuthComboBox.Height := ReportPage.Edits[1].Height;
  AuthComboBox.TabOrder := ReportPage.Edits[1].TabOrder;
  AuthComboBox.Items.Add('Password Authentication');          
  AuthComboBox.Items.Add('Windows Authentication');
  AuthComboBox.ItemIndex := 0;
  { Hide the original edit box }
  ReportPage.PromptLabels[1].FocusControl := AuthComboBox;
  ReportPage.Edits[1].Visible := False;
  AuthComboBox.OnChange := @ComboBoxChange;

背后的价值观AuthComboBox.Items.Add是:

function GetAuthCombo(Param: String): String;
begin
  case AuthComboBox.ItemIndex of
    0: Result := 'False';
    1: Result := 'True';
  end;
end;

我使用以下代码将它们写入注册表:

if (CurStep=ssPostInstall) then 
  begin
     RegWriteStringValue(HKEY_LOCAL_MACHINE, 'Software\RiskValue',
    'ReportProdAuthType', ExpandConstant('{code:GetAuthCombo}'));
  end;

如果我从中选择第二个选项 Windows 身份验证,combobox那么当我第二次运行安装程序时,我希望现在具有与默认值相同的值(Windows 身份验证)。

标签: inno-setuppascalscript

解决方案


替换这个:

  AuthComboBox.ItemIndex := 0;

和:

var
  S: string;
begin
  { ... }
  if RegQueryStringValue(HKLM, 'Software\RiskValue', 'ReportProdAuthType', S) and
     SameText(S, 'True') then
  begin
    AuthComboBox.ItemIndex := 1;
  end
    else
  begin
    AuthComboBox.ItemIndex := 0;
  end;
  { ... }
end;

此外,使用ExpandConstant来获取注册表项的值是过度设计的。

[Registry]部分使用它(脚本常量的用途):

[Registry]
Root: HKLM; Subkey: "Software\RiskValue"; ValueType: string; \
    ValueName: "ReportProdAuthType"; ValueData: "{code:GetAuthCombo}"

或者,如果你想使用 Pascal 脚本,GetAuthCombo直接使用:

if (CurStep=ssPostInstall) then 
begin
  RegWriteStringValue(HKEY_LOCAL_MACHINE, 'Software\RiskValue',
    'ReportProdAuthType', GetAuthCombo(''));
end;

然后,您甚至可以删除Param: String,甚至GetAuthCombo完全内联该函数,除非您在其他地方使用它。

var
  S: string;
begin
  { ... }
  if (CurStep=ssPostInstall) then 
  begin
    case AuthComboBox.ItemIndex of
      0: S := 'False';
      1: S := 'True';
    end;
    RegWriteStringValue(HKEY_LOCAL_MACHINE, 'Software\RiskValue', 'ReportProdAuthType', S);
  end;
end;

推荐阅读