带有单选按钮的 TInputDirWizardPage

TInputDirWizardPage with Radio Buttons

我需要一个带有两个单选按钮的设置页面。单击第二个按钮应启用输入以定义路径(就像在 TInputDirWizardPage 中所做的那样)。

是否可以根据我的需要自定义 TInputDirWizardPage

还是需要自己构建一个CustomPage然后自己定义控件? 如果第二个问题的回答是肯定的,我如何使用 "directory input"(来自 TInputDirWizardPage),或者是否也需要自己构建它?

如您所猜,您有多种选择:

  1. TWizardPage 开始,从头开始构建所有内容
  2. TInputDirWizardPage 开始并添加单选按钮
  3. TInputOptionWizardPage开始,添加编辑框

第二个选项可能涉及最少的定制。虽然它需要来自 Is it possible to allow a user to skip a TInputDirWizardPage in Inno Setup?

的 hack
{ WORKAROUND }
{ Checkboxes and Radio buttons created on runtime do }
{ not scale their height automatically. }
{ See  }
procedure ScaleFixedHeightControl(Control: TButtonControl);
begin
  Control.Height := ScaleY(Control.Height);
end;

var
  Page: TInputDirWizardPage;
  DefaultLocationButton: TRadioButton;
  CustomLocationButton: TRadioButton;
  OldNextButtonOnClick: TNotifyEvent;

procedure LocationButtonClick(Sender: TObject);
begin
  Page.Edits[0].Enabled := CustomLocationButton.Checked;
  Page.Buttons[0].Enabled := CustomLocationButton.Checked;
end;

procedure NextButtonOnClick(Sender: TObject);
var
  PrevDir: string;
begin
  { Do not validate, when "default location" is selected }
  if (WizardForm.CurPageID = Page.ID) and DefaultLocationButton.Checked then
  begin
    PrevDir := Page.Values[0];
    Page.Values[0] := GetWinDir; { Force value to pass validation }
    OldNextButtonOnClick(Sender);
    Page.Values[0] := PrevDir;
  end
    else
  begin
    OldNextButtonOnClick(Sender);
  end;
end;

procedure InitializeWizard();
begin
  Page := CreateInputDirPage(
    wpWelcome,
    'Select Personal Data Location', 'Where should personal data files be stored?',
    '', False, 'New Folder');
  Page.Add('');

  DefaultLocationButton := TRadioButton.Create(WizardForm);
  DefaultLocationButton.Parent := Page.Surface;
  DefaultLocationButton.Top := Page.Edits[0].Top;
  DefaultLocationButton.Caption := 'Use default location';
  DefaultLocationButton.Checked := True;
  DefaultLocationButton.OnClick := @LocationButtonClick;
  ScaleFixedHeightControl(DefaultLocationButton);
  
  CustomLocationButton := TRadioButton.Create(WizardForm);
  CustomLocationButton.Parent := Page.Surface;
  CustomLocationButton.Top :=
    DefaultLocationButton.Top + DefaultLocationButton.Height + ScaleY(8);
  CustomLocationButton.Caption := 'Use custom location';
  CustomLocationButton.OnClick := @LocationButtonClick;
  ScaleFixedHeightControl(DefaultLocationButton);

  Page.Buttons[0].Top :=
    Page.Buttons[0].Top +
    ((CustomLocationButton.Top + CustomLocationButton.Height + ScaleY(8)) -
      Page.Edits[0].Top);
  Page.Edits[0].Top :=
    CustomLocationButton.Top + CustomLocationButton.Height + ScaleY(8);
  Page.Edits[0].Left := Page.Edits[0].Left + ScaleX(16);
  Page.Edits[0].Width := Page.Edits[0].Width - ScaleX(16);
  Page.Edits[0].TabOrder := CustomLocationButton.TabOrder + 1;
  Page.Buttons[0].TabOrder := Page.Edits[0].TabOrder + 1;

  LocationButtonClick(nil); { Update edit for initial state of buttons }

  OldNextButtonOnClick := WizardForm.NextButton.OnClick;
  WizardForm.NextButton.OnClick := @NextButtonOnClick;
end;


关于该主题的更一般性问题: