如何在安装期间为可选文件添加CheckBox? (innosetup)

时间:2012-11-21 21:29:49

标签: inno-setup

我正在尝试在我的自定义页面中创建一个自定义复选框(因为它是一个单页安装程序),只需要一个没有对话框或任何内容的复选框,我正在尝试编译的安装程序非常线性和简单。

我想以这种方式在复选框上绑定“FILE3.EXE”:如果选中复选框,则复制DestDir中的文件(FILE3.EXE),否则如果取消选中复选框,则在安装期间跳过文件(FILE3.EXE)。

这是我使用的代码,显然缺少复选框代码,因为我无法做到这一点

[Files]
Source: FILE1.EXE; DestDir: {app};
Source: FILE2.EXE; DestDir: {app};
Source: FILE3.EXE; DestDir: {app}; //OPTIONAL

[Code]
procedure ExitProcess(uExitCode: UINT);
external 'ExitProcess@kernel32.dll stdcall';


var
MainPage : TWizardPage;
FolderToInstall : TEdit;
InstallLocation : String;


procedure CancelClick(Sender: TObject);
begin
if ExitSetupMsgBox then
begin
ExitProcess(0);
end;
end;


procedure BrowseClick(Sender : TObject);
var
Dir : String;

begin
Dir := FolderToInstall.Text;
if BrowseForFolder('Browse',Dir,false) then
FolderToInstall.Text := Dir;
WizardForm.DirEdit.Text := Dir;
end;


procedure InitializeWizard();
var
LabelFolder : TLabel;


MainPage := CreateCustomPage(wpWelcome,'','');
LabelFolder := TLabel.Create(MainPage);
LabelFolder.Parent := WizardForm;
LabelFolder.Top := 164;
LabelFolder.Left := 6;
LabelFolder.Caption := 'Directory:'


FolderToInstall := TEdit.Create(MainPage);
FolderToInstall.Parent := WizardForm;
FolderToInstall.Top := 182;
FolderToInstall.Left := 85;
FolderToInstall.Width := 380;
FolderToInstall.Text :=  WizardDirValue;
FolderToInstall.ReadOnly := True;
end;

4 个答案:

答案 0 :(得分:18)

您不必为此手动创建CheckBoxes。让用户选择安装内容的标准方法是使用脚本文件的[Types][Components]部分。

查看位于iss安装文件夹\ examples中的Componens.iss脚本。

; -- Components.iss --
; Demonstrates a components-based installation.

; SEE THE DOCUMENTATION FOR DETAILS ON CREATING .ISS SCRIPT FILES!

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
DefaultGroupName=My Program
UninstallDisplayIcon={app}\MyProg.exe
OutputDir=userdocs:Inno Setup Examples Output

[Types]
Name: "full"; Description: "Full installation"
Name: "compact"; Description: "Compact installation"
Name: "custom"; Description: "Custom installation"; Flags: iscustom

[Components]
Name: "program"; Description: "Program Files"; Types: full compact custom; Flags: fixed
Name: "help"; Description: "Help File"; Types: full
Name: "readme"; Description: "Readme File"; Types: full
Name: "readme\en"; Description: "English"; Flags: exclusive
Name: "readme\de"; Description: "German"; Flags: exclusive

[Files]
Source: "MyProg.exe"; DestDir: "{app}"; Components: program
Source: "MyProg.chm"; DestDir: "{app}"; Components: help
Source: "Readme.txt"; DestDir: "{app}"; Components: readme\en; Flags: isreadme
Source: "Readme-German.txt"; DestName: "Liesmich.txt"; DestDir: "{app}"; Components: readme\de; Flags: isreadme

[Icons]
Name: "{group}\My Program"; Filename: "{app}\MyProg.exe"

在运行时,安装程​​序在向导中显示此对话框:

Components dialog

答案 1 :(得分:13)

您需要创建一个Check函数,该函数将从脚本的[Code]部分返回复选框的状态。像这样的东西可能会做你想要的,但在代码脚本之前,我会在下面纠正你:

  • 尽可能使用 TNew ... 类,因此在您的情况下使用TNewEdit代替TEdit
  • 如果您想在页面上设置某个组件,请使用TWizardPage.Surface作为Parent(此处我不确定这是否是您的意图,只是指出了这一点: - )
  • 格式化您的代码,它不需要如此平坦

在下面的示例中,我使用Check函数InstallHelpFile来有条件地安装某个文件,在本例中为MyProg.chmCheck功能很简单;当你返回True函数时,文件被处理,跳过是你返回False。

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
OutputDir=userdocs:Inno Setup Examples Output

[Files]
Source: "MyProg.exe"; DestDir: "{app}"
Source: "MyProg.chm"; DestDir: "{app}"; Check: InstallHelpFile;

[Code]
var
  InstallHelpCheckBox: TNewCheckBox;  

procedure InitializeWizard;
var  
  LabelFolder: TLabel;  
  MainPage: TWizardPage;  
  FolderToInstall: TNewEdit;  
begin
  MainPage := CreateCustomPage(wpWelcome, '', '');
  LabelFolder := TLabel.Create(MainPage);
  LabelFolder.Parent := WizardForm;
  LabelFolder.Top := 164;
  LabelFolder.Left := 6;
  LabelFolder.Caption := 'Directory:'

  FolderToInstall := TNewEdit.Create(MainPage);
  FolderToInstall.Parent := MainPage.Surface;
  FolderToInstall.Top := 182;
  FolderToInstall.Left := 85;
  FolderToInstall.Width := 380;
  FolderToInstall.Text :=  WizardDirValue;
  FolderToInstall.ReadOnly := True;

  InstallHelpCheckBox := TNewCheckBox.Create(MainPage);
  InstallHelpCheckBox.Parent := MainPage.Surface;
  InstallHelpCheckBox.Top := FolderToInstall.Top + FolderToInstall.Height + 8;
  InstallHelpCheckBox.Left := FolderToInstall.Left;
  InstallHelpCheckBox.Width := FolderToInstall.Width;
  InstallHelpCheckBox.Caption := 'Install help file';
end;

function InstallHelpFile: Boolean;
begin
  // here is the Check function used above; if you return True to this
  // function, the file will be installed, when False, the file won't 
  // be installed
  Result := InstallHelpCheckBox.Checked;
end;

答案 2 :(得分:3)

使用CreateInputOptionPage()可以更轻松地完成。请参阅Inno Setup帮助中的详细信息。 http://www.jrsoftware.org/ishelp/index.php?topic=scriptpages

答案 3 :(得分:0)

// Create:
for i := 0 to g_SetupX_Count do begin
    WizardForm.ComponentsList.AddCheckBox(g_SetupX_Name[i], '', 0, g_SetupX_Active[i], true, false, false, nil);
    g_SetupX_CompListIndex[i] := nextPosi;
    nextPosi := nextPosi + 1;
end;

// Evaluate:
for i := 0 to g_SetupX_Count do begin
    g_SetupX_Active[i] := WizardForm.ComponentsList.Checked[g_SetupX_CompListIndex[i]];
end;