【问题标题】:Inno Setup MsgBox with three buttons and three outcomes带有三个按钮和三个结果的 Inno Setup MsgBox
【发布时间】:2014-08-26 21:42:19
【问题描述】:

我正在尝试创建一个带有三个按钮和三个结果的MsgBox,但我无法看到如何创建第三个结果?我目前有两个按钮MsgBox 的以下代码,效果很好:

if ((strExistingInstallPath <> '') and (strExistingVersion = '2.5.3')) then
begin
  if SuppressibleMsgBox('Setup has detected that ' + strMyAppName + ' ' + strExistingVersion + '.' + strExistingBuild + ' is installed.' + #13#10 + #13#10 +
    'The existing version must be removed before installing or upgrading to ' + strMyAppVersion + '.' + strMyAppBuild + '.' + #13#10 + #13#10 +
    'Would you like Setup to uninstall the existing version?',
    mbConfirmation, MB_YESNO, IDYES) = IDYES then
    begin
      Exec(GetUninstallString, '', '', SW_SHOW,
        ewWaitUntilTerminated, intResultCode);
      Result := True;
    end else
      begin
        MsgBox('The existing version must be removed first.' + #13#10 +
          'Setup is unable to continue. Setup will now exit.',
          mbError, MB_OK);
        Result := False;
      end;
end;

如果我将 MB_YESNO 更改为 MB_YESNOCANCEL,我现在会得到三个按钮,YesNoCancel。然而,由于if 语句被分配给MsgBox,我正在努力研究如何执行else if IDCANCEL then 类型的语句。我试图将 MsgBox 返回的 ID 常量分配给一个字符串,然后为字符串创建单独的 if 语句,使其等于每个 ID 常量,但这失败了。我在这里想念什么?理想情况下,我希望将三个按钮标记为 YesNoSilent,以便可以为第三个按钮指定 /silent参数以防止卸载提示。那么,是否也可以重命名按钮?

【问题讨论】:

  • MsgBox 返回一个整数,您可以在返回时使用“大小写”。
  • 那是我的问题。我没有意识到 MsgBox 返回了一个整数。 ID 常量看起来像字符串!

标签: inno-setup pascalscript


【解决方案1】:

您可以编写多个if 语句,但您必须将返回值存储到变量中并检查该变量值。但是正如@Sertac 在他的评论中提到的那样,您可以使用case 语句,它可以更好地描述您的代码中的目标,例如:

case SuppressibleMsgBox('Text', mbConfirmation, MB_YESNOCANCEL, IDYES) of
  IDYES:
  begin
    { user pressed Yes }
  end;
  IDNO:
  begin
    { user pressed No }
  end;
  IDCANCEL:
  begin
    { user pressed Cancel }
  end;
end;

出于对多个if 语句的好奇,它可能是:

var
  MsgResult: Integer;
begin
  MsgResult := SuppressibleMsgBox('Text', mbConfirmation, MB_YESNOCANCEL, IDYES);

  if MsgResult = IDYES then
  begin
    { user pressed Yes }
  end
  else
  if MsgResult = IDNO then
  begin
    { user pressed No }
  end
  else
  if MsgResult = IDCANCEL then
  begin
    { user pressed Cancel }
  end;
end;

【讨论】:

  • 谢谢。您使用 if 语句的第二个示例正是我之前所做的,但是使用了字符串!现在工作我意识到它应该是一个整数。
  • 您知道是否可以将按钮上的标签从取消更改为静音?
  • SuppressibleMsgBox(和MsgBox)函数后面是MessageBox Windows API 函数,它构建了对话框。并且该功能从当前选择的语言中获取按钮标题,并且无法使用自定义标题。但是,是的,我认为it should be possible,但你需要有一个带钩子的外部库。为此制作自定义表单会更容易。
  • 谢谢。但是,我现在实际上已经为我试图实现的目标找到了一个更优雅的解决方案,因为即使您需要第三个结果,我想在命令行中使用 /verysilent 开关时将其作为默认选项,您实际上不需要显示第三个按钮。你仍然可以有一个 MB_YESNO,但给它一个不同的默认值,例如IDIGNORE 并在静默运行时调用它。
【解决方案2】:

以下是最终代码,以防对其他人有用:

var
  intMsgBoxResult: Integer;
if ((strExistingInstallPath <> '') and (strExistingVersion = '2.5.3')) then
begin
  intMsgBoxResult := SuppressibleMsgBox('Setup has detected that ' + strMyAppName + ' ' + strExistingVersion + '.' + strExistingBuild + ' is installed.' + #13#10 + #13#10 +
    'The existing version must be removed before installing or upgrading to ' + strMyAppVersion + '.' + strMyAppBuild + '.' + #13#10 + #13#10 +
    'Would you like Setup to uninstall the existing version?',
    mbConfirmation, MB_YESNO, IDIGNORE);
  if intMsgBoxResult = IDYES then
    begin
      Exec(GetUninstallString, '/silent', '', SW_SHOW,
        ewWaitUntilTerminated, intResultCode);
      Result := True;
    end;
  if intMsgBoxResult = IDNO then 
    begin
      MsgBox('The existing version must be removed first.' + #13#10 +
        'Setup is unable to continue. Setup will now exit.',
        mbError, MB_OK);
      Result := False;
    end;
  if intMsgBoxResult = IDIGNORE then
    begin
      Exec(GetUninstallString, '', '', SW_SHOW,
        ewWaitUntilTerminated, intResultCode);
      Result := True;
    end; 
end;

【讨论】:

  • 这里的情况更好。但无论如何,现在您总是要测试 intMsgBoxResult 值 3 次。如果您真的想使用if,那么至少添加else(如我所示)因为如果intMsgBoxResult 的值将是IDYES,则无需测试IDNOIDIGNORE.
  • 好点。如果您认为这是一种更好的处理方式,将进行修改并可能改用使用案例。非常感谢您的帮助和建议。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 2017-03-02
  • 2017-09-16
  • 2021-02-11
相关资源
最近更新 更多