在欢迎页面显示长 运行 操作的进度

Display progress of long running operation on Welcome page

我想在安装过程中检查我的应用程序更新,同时在 Inno Setup 的表单上显示该过程。

所以基本上是这样的:

WizardForm.WelcomeLabel2.Caption := 'Checking for update ...';
DownloadFile(VersionURL, LatestVersion)
WizardForm.WelcomeLabel2.Caption := 'New version available.';

下面是一个工作代码,除了当调用 CurPageChanged 时,表单还没有准备好。在完成新版本的检查之前,它不会正确显示。请参阅屏幕截图。使用调试器单步执行代码时工作正常。

我错过了什么?任何解决方法?
主要目的是避免在没有任何反馈的情况下等待版本检查过程,因为这感觉是不负责任的。

两张图片。

#if defined UNICODE
  ; signed 64bit integer, but using only positive numbers, 0 .. 9223372036854775807
  ; examples of version string:
  ; 0..255 . 0..255 . 0..255 . 0..255 . 0..255 . 0..255 . 0..255 . 0..127
  ; 0..65535 . 0..65535 . 0..65535 . 0..32767
  #define Int64_OR_LongWord "Int64"
#else
  ; LongWord, 0 .. 4294967295
  ; 0..255 . 0..255 . 0..255 . 0..255
  ; 0..65535 . 0..65535
  #define Int64_OR_LongWord "LongWord"
#endif

[Setup]
AppName=Check Version
AppVersion=1.2.3
DefaultDirName={pf}\My Program

[Code]
const
  SetupURL = 'http://hu.hu/huhu.exe';
  VersionURL = 'http://hu.hu/version.txt';
  sizeNumOfSubsets=4;
  sizeSubset=256;

function VerStrToNum(const strVersion: String): {#Int64_OR_LongWord};
var strVersionRemaining: String;
    tmpIntSubset: Integer;
    i: Byte;
begin
  // add period at the end, so that any valid number should end with a period
  // empty strVersionRemaining means all numbers are read and the rest are zeroes
  // v1.2.3 = v1.2.3.0.0
  strVersionRemaining := AddPeriod(strVersion);
  for i := 1 to sizeNumOfSubsets do begin
    if strVersionRemaining <> '' then begin
      tmpIntSubset := StrToIntDef(Copy(strVersionRemaining,1, Pos('.',strVersionRemaining)-1), -1);
      strVersionRemaining := Copy(strVersionRemaining,Pos('.',strVersionRemaining)+1,Length(strVersionRemaining));
    end
    else tmpIntSubset := 0;   
    if tmpIntSubset <> -1 then begin
      Result := Result * sizeSubset + tmpIntSubset;
    end                                                
    else begin
      // if version string format invalid, just return 0,
      // error would just confuse user. RaiseException('Invalid format of version string');
      Result := 0;
      Exit;
    end;
  end;
end;

function DownloadFile(const URL: string; var Response: string): Boolean;
var
  WinHttpRequest: Variant;
begin
  Result := True;
  try
    WinHttpRequest := CreateOleObject('WinHttp.WinHttpRequest.5.1');
    WinHttpRequest.Open('GET', URL, False);
    WinHttpRequest.Send;
    Response := WinHttpRequest.ResponseText;
  except
    Result := False;
    Response := GetExceptionMessage;
  end;
end;


// --- to show correct text when comming back to Welcome Page by pressing PREV button
var oldWelcomeCaption: String;

function NextButtonClick(CurPageID: Integer): Boolean;
begin
  if CurPageID = wpWelcome then begin
    WizardForm.WelcomeLabel2.Caption := oldWelcomeCaption;
  end;
  Result := true
end;

var PanelDownloadButton: TPanel;

procedure CurPageChanged(CurPageID: Integer);
var LatestVersion: string;
begin
  case CurPageID of
    wpWelcome: begin
      // the Welcome Page was still hidden at this point
      WizardForm.Visible := True;
      // THIS IS WHAT I WAS MISSING:
      WizardForm.Repaint;
      oldWelcomeCaption := WizardForm.WelcomeLabel2.Caption;
      WizardForm.WelcomeLabel2.Caption := oldWelcomeCaption + #13 + #10 + #13 + #10 + #13 + #10 + #13 + #10 + #13 + #10 + #13 + #10 + 'Checking for update ...';
      if DownloadFile(VersionURL, LatestVersion) then begin
        if VerStrToNum(LatestVersion) > VerStrToNum('{#SetupSetting('AppVersion')}') then begin
          WizardForm.WelcomeLabel2.Caption := oldWelcomeCaption + #13 + #10 + #13 + #10 + #13 + #10 + #13 + #10 + #13 + #10 + #13 + #10 + 'New version available.';
          // PanelDownloadButton.Visible := True;
          // xxx Run downloaded setup, exit this one
        end
        else WizardForm.WelcomeLabel2.Caption := oldWelcomeCaption + #13 + #10 + #13 + #10 + #13 + #10 + #13 + #10 + #13 + #10 + #13 + #10 + 'No new versions.';
      end
      else WizardForm.WelcomeLabel2.Caption := WizardForm.WelcomeLabel2.Caption + #13 + #10 + Copy(LatestVersion,Pos(':',LatestVersion)+2,Length(LatestVersion));
    end;
  end;
end;

编辑: 上面的代码现已修复,以防有人想使用版本检查。
我将在带有按钮的最终代码中添加一个 link 并稍后下载功能。

调用WizardForm.Repaint强制表格完全绘制:

WizardForm.Visible := True;
WizardForm.Repaint;

https://docwiki.embarcadero.com/Libraries/en/Vcl.Controls.TControl.Repaint

Forces the control to repaint its image on the screen.

Call Repaint to force the control to repaint its image immediately.