在完成页面启用 Inno Setup 的 close/cancel 按钮

Enable close/cancel button of Inno Setup on Finished page

是否可以在 Inno Setup 表单的最后一页启用关闭按钮,并添加退出行为?

启用关闭按钮很容易,使用EnableMenuItem WinAPI function. See also Inno Setup Disable close button (X)

困难在于让关闭按钮真正起作用。 Inno Setup window 未设计为在 "Finished" 页面上关闭。唯一的方法可能是使用 ExitProcess WinAPI function. See Exit from Inno Setup Installation from [code].

强行中止进程

完整代码为:

function GetSystemMenu(hWnd: THandle; bRevert: Boolean): THandle;
  external 'GetSystemMenu@user32.dll stdcall';

function EnableMenuItem(hMenu: UINT; uIDEnableItem, uEnable: UINT): Boolean;
  external 'EnableMenuItem@user32.dll stdcall';

const
  MF_BYCOMMAND = [=10=];
  SC_CLOSE = $F060;

procedure ExitProcess(exitCode:integer);
  external 'ExitProcess@kernel32.dll stdcall';

procedure FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Log('Exiting by user after installation');
  ExitProcess(1);
end;

procedure CurPageChanged(CurPageID: Integer);
var
  Menu: THandle;
begin
  if CurPageID = wpFinished then
  begin
    { Enable "close" button }
    Menu := GetSystemMenu(WizardForm.Handle, False);
    EnableMenuItem(Menu, SC_CLOSE, MF_BYCOMMAND);
    { Make the "close" button working }
    WizardForm.OnClose := @FormClose;
  end;
end;