在 Inno 中动态设置按钮的宽度?

Dynamically setting the width of the buttons in Inno?

到目前为止,这是我的自定义页面定义:

var

  // Backup Settings Page & Controls
  pageAutoBackup: TWizardPage;
  btnSelectBackupFolder: TNewButton;
  textBackupFolder: TNewEdit;
  lblWhereToBackup: TLabel;
  lblPromptMode: TLabel;
  comboPromptMode: TNewComboBox;
  lblReminder: TLabel;
  radioBackupModeManual: TNewRadioButton;
  radioBackupModeAuto: TNewRadioButton;
  lblHowToBackup: TLabel;
  radioBackupHowEssential: TNewRadioButton;
  radioBackupHowComplete: TNewRadioButton;
  radioBackupHowNone: TNewRadioButton;
  lblWhatToBackup: TLabel;
  panelWhatToPackup: TPanel;
  panelHowToPackup: TPanel;

function pageAutoBackup_CreatePage(PreviousPageId: Integer): Integer;
begin
    pageAutoBackup := CreateCustomPage(PreviousPageId, 'Automatic Backup', 'Configure automatic backup settings.');

    { panelWhatToPackup (TPanel) }
    panelWhatToPackup := TPanel.Create(pageAutoBackup);
    panelWhatToPackup.Caption := '';
    panelWhatToPackup.BevelOuter := bvNone;
    panelWhatToPackup.BevelInner := bvNone; { default }
    panelWhatToPackup.BevelKind := bkNone;
    panelWhatToPackup.Parent := pageAutoBackup.Surface;
    panelWhatToPackup.Enabled := True;
    panelWhatToPackup.Visible := True;
    panelWhatToPackup.Left := ScaleX(0);
    panelWhatToPackup.Top := ScaleY(0);
    panelWhatToPackup.Width := pageAutoBackup.Surface.Width;
    panelWhatToPackup.Height := ScaleY(90);
    panelWhatToPackup.Anchors := [akLeft, akRight];

    { lblWhatToBackup (TLabel) }
    lblWhatToBackup := TLabel.Create(pageAutoBackup);
    lblWhatToBackup.Parent := panelWhatToPackup;
    lblWhatToBackup.Enabled := True;
    lblWhatToBackup.Visible := True;
    lblWhatToBackup.Width := panelWhatToPackup.Width;
    lblWhatToBackup.Font.Color := [=10=]0000;
    lblWhatToBackup.Color := $F0F0F0;
    lblWhatToBackup.Caption := 'What to backup:';

    { radioBackupHowNone (TNewRadioButton) }
    radioBackupHowNone := TNewRadioButton.Create(pageAutoBackup);
    radioBackupHowNone.Parent := panelWhatToPackup;
    radioBackupHowNone.Enabled := True;
    radioBackupHowNone.Visible := True;
    radioBackupHowNone.Top := lblWhatToBackup.Top + lblWhatToBackup.Height + ScaleY(2);
    radioBackupHowNone.Width := panelWhatToPackup.Width;
    radioBackupHowNone.Checked := False;
    radioBackupHowNone.Font.Color := [=10=]0000;
    radioBackupHowNone.Color := $F0F0F0;
    radioBackupHowNone.Caption := 'Don''t perform any backup when the program shuts down';

    { radioBackupHowComplete (TNewRadioButton) }
    radioBackupHowComplete := TNewRadioButton.Create(pageAutoBackup);
    radioBackupHowComplete.Parent := panelWhatToPackup;
    radioBackupHowComplete.Enabled := True;
    radioBackupHowComplete.Visible := True;
    radioBackupHowComplete.Top := radioBackupHowNone.Top + radioBackupHowNone.Height + ScaleY(2);
    radioBackupHowComplete.Width := panelWhatToPackup.Width;
    radioBackupHowComplete.Checked := False;
    radioBackupHowComplete.Font.Color := [=10=]0000;
    radioBackupHowComplete.Color := $F0F0F0;
    radioBackupHowComplete.Caption := 'Make a complete backup when the program shuts down';

    { radioBackupHowEssential (TNewRadioButton) }
    radioBackupHowEssential := TNewRadioButton.Create(pageAutoBackup);
    radioBackupHowEssential.Parent := panelWhatToPackup;
    radioBackupHowEssential.Enabled := True;
    radioBackupHowEssential.Visible := True;
    radioBackupHowEssential.Top :=  radioBackupHowComplete.Top + radioBackupHowComplete.Height + ScaleY(2);
    radioBackupHowEssential.Width := panelWhatToPackup.Width;
    radioBackupHowEssential.Checked := False;
    radioBackupHowEssential.Font.Color := [=10=]0000;
    radioBackupHowEssential.Color := $F0F0F0;
    radioBackupHowEssential.Caption := 'Only make an essential backup when the program shuts down';

    { panelHowToPackup (TPanel) }
    panelHowToPackup := TPanel.Create(pageAutoBackup);
    panelHowToPackup.Caption := '';
    panelHowToPackup.BevelOuter := bvNone;
    panelHowToPackup.BevelInner := bvNone; { default }
    panelHowToPackup.BevelKind := bkNone;
    panelHowToPackup.Parent := pageAutoBackup.Surface;
    panelHowToPackup.Enabled := True;
    panelHowToPackup.Visible := True;
    panelHowToPackup.Top := panelWhatToPackup.Top + panelWhatToPackup.Height + ScaleY(2);
    panelHowToPackup.Width := pageAutoBackup.Surface.Width;
    panelHowToPackup.Height := ScaleY(100);
    panelHowToPackup.Anchors := [akLeft, akRight];

    { lblHowToBackup (TLabel) }
    lblHowToBackup := TLabel.Create(pageAutoBackup);
    lblHowToBackup.Parent := panelHowToPackup;
    lblHowToBackup.Enabled := True;
    lblHowToBackup.Visible := True;
    lblHowToBackup.Width := panelHowToPackup.Width;
    lblHowToBackup.Font.Color := [=10=]0000;
    lblHowToBackup.Color := $F0F0F0;
    lblHowToBackup.Caption := 'How to backup:';

    { radioBackupModeAuto (TNewRadioButton) }
    radioBackupModeAuto := TNewRadioButton.Create(pageAutoBackup);
    radioBackupModeAuto.Parent := panelHowToPackup;
    radioBackupModeAuto.Enabled := True;
    radioBackupModeAuto.Visible := True;
    radioBackupModeAuto.Left := ScaleX(0);
    radioBackupModeAuto.Top := lblHowToBackup.Top + lblHowToBackup.Height + ScaleY(2);
    radioBackupModeAuto.Width := panelHowToPackup.Width;
    radioBackupModeAuto.Checked := False;
    radioBackupModeAuto.Font.Color := [=10=]0000;
    radioBackupModeAuto.Color := $F0F0F0;
    radioBackupModeAuto.Caption := 'Perform automatically when the program is shut down';

    { radioBackupModeManual (TNewRadioButton) }
    radioBackupModeManual := TNewRadioButton.Create(pageAutoBackup);
    radioBackupModeManual.Parent := panelHowToPackup;
    radioBackupModeManual.Enabled := True;
    radioBackupModeManual.Visible := True;
    radioBackupModeManual.Top := radioBackupModeAuto.Top + radioBackupModeAuto.Height + ScaleY(2);
    radioBackupModeManual.Width := panelHowToPackup.Width;
    radioBackupModeManual.Checked := False;
    radioBackupModeManual.Font.Color := [=10=]0000;
    radioBackupModeManual.Color := $F0F0F0;
    radioBackupModeManual.Caption := 'Prompt the user when the program is shut down';

    { lblPromptMode (TLabel) }
    lblPromptMode := TLabel.Create(pageAutoBackup);
    lblPromptMode.Parent := panelHowToPackup;
    lblPromptMode.Enabled := True;
    lblPromptMode.Visible := True;
    lblPromptMode.Top := radioBackupModeManual.Top + radioBackupModeManual.Height + ScaleY(2);
    lblPromptMode.Width := panelHowToPackup.Width;
    lblPromptMode.Font.Color := [=10=]0000;
    lblPromptMode.Color := $F0F0F0;
    lblPromptMode.Caption := 'Also prompt to backup at the following intervals while the application is running:';

    { comboPromptMode (TNewComboBox) }
    comboPromptMode := TNewComboBox.Create(pageAutoBackup);
    comboPromptMode.Parent := panelHowToPackup;
    comboPromptMode.Style := csDropDownList;
    comboPromptMode.Enabled := True;
    comboPromptMode.Visible := True;
    comboPromptMode.Top := lblPromptMode.Top + lblPromptMode.Height + ScaleY(2);
    comboPromptMode.Width := panelHowToPackup.Width;
    comboPromptMode.Font.Color := [=10=]0000;
    comboPromptMode.ItemIndex := 0;
    comboPromptMode.Color := $FFFFFF;
    comboPromptMode.Items.Add('Never prompt to backup');
    comboPromptMode.Items.Add('Prompt to backup everyday');
    comboPromptMode.Items.Add('Prompt to backup once a week');
    comboPromptMode.Items.Add('Prompt to backup once a month');
    comboPromptMode.Anchors := [akLeft, akRight];

    { lblWhereToBackup (TLabel) }
    lblWhereToBackup := TLabel.Create(pageAutoBackup);
    lblWhereToBackup.Parent := pageAutoBackup.Surface;
    lblWhereToBackup.Enabled := True;
    lblWhereToBackup.Visible := True;
    lblWhereToBackup.Top := panelHowToPackup.Top + panelHowToPackup.Height + ScaleY(2);
    lblWhereToBackup.Width := panelHowToPackup.Width;
    lblWhereToBackup.Font.Color := [=10=]0000;
    lblWhereToBackup.Color := $F0F0F0;
    lblWhereToBackup.Caption := 'Where to backup:';
    lblWhereToBackup.Anchors := [akLeft, akRight, akBottom];

    { textBackupFolder (TNewEdit) }
    textBackupFolder := TNewEdit.Create(pageAutoBackup);
    textBackupFolder.Parent := pageAutoBackup.Surface;
    textBackupFolder.Enabled := True;
    textBackupFolder.Visible := True;
    textBackupFolder.Top := lblWhereToBackup.Top + lblWhereToBackup.Height + ScaleY(2);
    textBackupFolder.Width := ScaleX(335);
    textBackupFolder.Font.Color := [=10=]0000;
    textBackupFolder.Color := $F0F0F0;
    textBackupFolder.Anchors := [akLeft, akRight, akBottom];

    { btnSelectBackupFolder (TNewButton) }
    btnSelectBackupFolder := TNewButton.Create(pageAutoBackup);
    btnSelectBackupFolder.Parent := pageAutoBackup.Surface;
    btnSelectBackupFolder.Enabled := True;
    btnSelectBackupFolder.Visible := True;
    btnSelectBackupFolder.Left := ScaleX(340);
    btnSelectBackupFolder.Top := textBackupFolder.Top;
    btnSelectBackupFolder.Width := ScaleX(75);
    btnSelectBackupFolder.Caption := 'Browse ...';
    btnSelectBackupFolder.Anchors := [akRight, akBottom];
    btnSelectBackupFolder.OnClick := @btnSelectBackupFolder_Click;

    Result := pageAutoBackup.ID;

end;

这是最小尺寸的样子:

由于有限 space 我决定删除其中一个静态标签,因为它并不是真正需要的。

这是调整大小后的样子:

看起来不错。我的查询涉及一些特定大小的控件:

由于我要对页面进行本地化,因此我无法假定 "Browse ..." 按钮的指定宽度。我们如何设置它,以便此按钮位于右侧,采用最终自定义消息所需的任何宽度,并在其左侧相应地调整文本控件的大小?

我认为 Windows 根据标题文本调整控件的大小并不常见。只需使按钮足够大以适应所有翻译即可。 Imo,一般规则是在带有长字幕的控件的原始英文文本之上添加 50%,在带有短字幕的控件(如您的)上添加 100%。

或者,由于您的按钮 one-word 与其他标准 Inno Setup 按钮一样,您可以使用相同的宽度以保持一致性。

btnSelectBackupFolder.Width := WizardForm.CancelButton.Width;

如果您真的想自动调整大小,则没有直接的方法来计算标题宽度,因为 Inno Setup 不会公开控件的 canvas。

您可以通过以下任一方式破解它:

  • 使用AutoSize := True创建临时TLabel,设置标题并查询标签宽度。
  • 创建临时 TBitmapImage 并使用其 TBitmapImage.Bitmap.Canvas.TextWidth 计算文本宽度。

在这两种情况下,请确保控件的 Font 和 parent 与按钮的字体和 parent 匹配。要删除临时控件,请调用其 .Free 方法。