如何强制 Inno Setup 使用来自自定义向导页面的信息动态设置安装文件夹?
How to force Inno Setup to set the installation folder dynamically with info from custom wizard page?
在 Inno Setup 中,我添加了一个自定义向导页面,用户可以在其中输入应动态添加到默认目录的后缀代码。
标准 DefaultDirName
是 c:\MyApp
。
当用户在额外的自定义向导页面中添加后缀 01
时,DefaultDirName
应更改为 c:\MyApp01
。
如何做到这一点?显然我不能在 [Setup]
部分中使用代码,因为代码是在任何向导页面之前评估的。
离开 "suffix" 页面时,将后缀附加到安装路径。
另外你必须处理:
- 用户返回后缀页面并更改后缀
- re-installations(升级)-我的解决方案不允许更改 re-installations 的后缀(依赖于默认的 Inno Setup 行为,用户有一点机会更改安装路径)。
#define AppId "your-app-id"
#define SetupReg "Software\Microsoft\Windows\CurrentVersion\Uninstall\" + AppId + "_is1"
#define SetupAppPathReg "Inno Setup: App Path"
[Setup]
AppId={#AppId}
[Code]
function IsUpgrade: Boolean;
var S: string;
begin
Result :=
RegQueryStringValue(HKLM, '{#SetupReg}', '{#SetupAppPathReg}', S) or
RegQueryStringValue(HKCU, '{#SetupReg}', '{#SetupAppPathReg}', S);
end;
var
SuffixPage: TInputQueryWizardPage;
procedure InitializeWizard();
begin
if not IsUpgrade then
begin
SuffixPage := CreateInputQueryPage(wpWelcome, 'Select suffix', '', '');
SuffixPage.Add('Suffix', False);
end;
end;
function NextButtonClick(CurPageID: Integer): Boolean;
begin
{ Add suffix to path, when leaving "suffix" page }
if (SuffixPage <> nil) and (CurPageID = SuffixPage.ID) then
begin
WizardForm.DirEdit.Text := WizardForm.DirEdit.Text + SuffixPage.Values[0];
end;
Result := True;
end;
function BackButtonClick(CurPageID: Integer): Boolean;
var
Suffix: string;
P: Integer;
begin
{ When going back from "select dir" page }
if (CurPageID = wpSelectDir) and (SuffixPage <> nil) then
begin
Suffix := SuffixPage.Values[0];
P := Length(WizardForm.DirEdit.Text) - Length(Suffix) + 1;
{ ... and the path still contains the suffix [was not edited out by the user] ... }
if Copy(WizardForm.DirEdit.Text, P, Length(Suffix)) = Suffix then
begin
{ ... remove it form the path }
WizardForm.DirEdit.Text := Copy(WizardForm.DirEdit.Text, 1, P - 1);
end
else
{ if the suffix was edited out by the user, clear suffix box }
begin
SuffixPage.Values[0] := '';
end;
end;
Result := True;
end;
在 Inno Setup 中,我添加了一个自定义向导页面,用户可以在其中输入应动态添加到默认目录的后缀代码。
标准 DefaultDirName
是 c:\MyApp
。
当用户在额外的自定义向导页面中添加后缀 01
时,DefaultDirName
应更改为 c:\MyApp01
。
如何做到这一点?显然我不能在 [Setup]
部分中使用代码,因为代码是在任何向导页面之前评估的。
离开 "suffix" 页面时,将后缀附加到安装路径。
另外你必须处理:
- 用户返回后缀页面并更改后缀
- re-installations(升级)-我的解决方案不允许更改 re-installations 的后缀(依赖于默认的 Inno Setup 行为,用户有一点机会更改安装路径)。
#define AppId "your-app-id"
#define SetupReg "Software\Microsoft\Windows\CurrentVersion\Uninstall\" + AppId + "_is1"
#define SetupAppPathReg "Inno Setup: App Path"
[Setup]
AppId={#AppId}
[Code]
function IsUpgrade: Boolean;
var S: string;
begin
Result :=
RegQueryStringValue(HKLM, '{#SetupReg}', '{#SetupAppPathReg}', S) or
RegQueryStringValue(HKCU, '{#SetupReg}', '{#SetupAppPathReg}', S);
end;
var
SuffixPage: TInputQueryWizardPage;
procedure InitializeWizard();
begin
if not IsUpgrade then
begin
SuffixPage := CreateInputQueryPage(wpWelcome, 'Select suffix', '', '');
SuffixPage.Add('Suffix', False);
end;
end;
function NextButtonClick(CurPageID: Integer): Boolean;
begin
{ Add suffix to path, when leaving "suffix" page }
if (SuffixPage <> nil) and (CurPageID = SuffixPage.ID) then
begin
WizardForm.DirEdit.Text := WizardForm.DirEdit.Text + SuffixPage.Values[0];
end;
Result := True;
end;
function BackButtonClick(CurPageID: Integer): Boolean;
var
Suffix: string;
P: Integer;
begin
{ When going back from "select dir" page }
if (CurPageID = wpSelectDir) and (SuffixPage <> nil) then
begin
Suffix := SuffixPage.Values[0];
P := Length(WizardForm.DirEdit.Text) - Length(Suffix) + 1;
{ ... and the path still contains the suffix [was not edited out by the user] ... }
if Copy(WizardForm.DirEdit.Text, P, Length(Suffix)) = Suffix then
begin
{ ... remove it form the path }
WizardForm.DirEdit.Text := Copy(WizardForm.DirEdit.Text, 1, P - 1);
end
else
{ if the suffix was edited out by the user, clear suffix box }
begin
SuffixPage.Values[0] := '';
end;
end;
Result := True;
end;