Inno Setup - Create a dynamic list of components/types from external source (file or folder contents)

♀尐吖头ヾ 提交于 2019-12-03 20:31:59

You cannot create the components dynamically on runtime (you can on compile-time).


But it's not difficult to implement a custom dynamic components-like page using the CreateCustomPage and the TNewCheckListBox.

Then in the CurStepChanged(ssInstall) you process the selected files/components as your need.

[Code]

const
  SourcePath = 'C:\somepath';

var
  CustomSelectTasksPage: TWizardPage;
  ComponentsList: TNewCheckListBox;

procedure InitializeWizard();
var
  FindRec: TFindRec;
  SelectComponentsLabel: TNewStaticText;
begin
  CustomSelectTasksPage :=
    CreateCustomPage(
      wpSelectComponents, SetupMessage(msgWizardSelectComponents),
      SetupMessage(msgSelectComponentsDesc));

  SelectComponentsLabel := TNewStaticText.Create(WizardForm);
  SelectComponentsLabel.Parent := CustomSelectTasksPage.Surface;
  SelectComponentsLabel.Top := 0;
  SelectComponentsLabel.Left := 0;
  SelectComponentsLabel.Width := CustomSelectTasksPage.Surface.Width;
  SelectComponentsLabel.AutoSize := False;
  SelectComponentsLabel.ShowAccelChar := False;
  SelectComponentsLabel.WordWrap := True;
  SelectComponentsLabel.Caption := SetupMessage(msgSelectComponentsLabel2);
  WizardForm.AdjustLabelHeight(SelectComponentsLabel);

  ComponentsList := TNewCheckListBox.Create(WizardForm);
  ComponentsList.Parent := CustomSelectTasksPage.Surface;
  ComponentsList.Top :=
    SelectComponentsLabel.Top + SelectComponentsLabel.Height + ScaleY(8);
  ComponentsList.Left := 0;
  ComponentsList.Width := CustomSelectTasksPage.Surface.Width;
  ComponentsList.Height := CustomSelectTasksPage.Surface.Height - ComponentsList.Top;

  if FindFirst(ExpandConstant(AddBackslash(SourcePath) + '*.dat'), FindRec) then
  begin
    try
      repeat
        ComponentsList.AddCheckBox(FindRec.Name, '', 0, False, True, False, False, nil);
      until not FindNext(FindRec);
    finally
      FindClose(FindRec);
    end;
  end;
end;

procedure CurStepChanged(CurStep: TSetupStep);
var
  I: Integer;
  FileName: string;
  SourceFilePath: string;
  TargetFilePath: string;
begin
  if CurStep = ssInstall then
  begin
    for I := 0 to ComponentsList.Items.Count - 1 do
    begin
      if ComponentsList.Checked[I] then
      begin
        FileName := ComponentsList.Items[I];
        SourceFilePath := AddBackslash(SourcePath) + FileName;
        TargetFilePath := AddBackslash(ExpandConstant('{app}')) + FileName;
        if FileCopy(SourceFilePath, TargetFilePath, False) then
        begin
          Log(Format('Installed "%s".', [FileName]));
        end
          else
        begin
          Log(Format('Failed to install "%s".', [FileName]));
        end;
      end;
    end;
  end;
end;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!