Selaa lähdekoodia

Show how to do a 'Stop download' button.

Martijn Laan 5 vuotta sitten
vanhempi
commit
a7e27749e1
2 muutettua tiedostoa jossa 67 lisäystä ja 34 poistoa
  1. 66 33
      Examples/CodeDownloadFiles.iss
  2. 1 1
      whatsnew.htm

+ 66 - 33
Examples/CodeDownloadFiles.iss

@@ -28,21 +28,37 @@ Name: "{group}\My Program"; Filename: "{app}\MyProg.exe"
 var
 var
   DownloadStatusLabel, DownloadFilenameLabel: TNewStaticText;
   DownloadStatusLabel, DownloadFilenameLabel: TNewStaticText;
   DownloadProgressBar: TNewProgressBar;
   DownloadProgressBar: TNewProgressBar;
+  DownloadAbortButton: TNewButton;
+  DownloadControls: array of TControl;
+  NeedToAbortDownload: Boolean;
 
 
 procedure SetupDownloadControl(const Dest, Src: TControl; const Parent: TWinControl);
 procedure SetupDownloadControl(const Dest, Src: TControl; const Parent: TWinControl);
+var
+  N: Integer;
 begin
 begin
-  Dest.Left := Src.Left;
-  Dest.Top := Src.Top;
-  Dest.Width := Src.Width;
-  Dest.Height := Src.Height;
-  if Src is TNewStaticText then
-    TNewStaticText(Dest).Anchors := TNewStaticText(Src).Anchors
-  else if Src is TNewProgressBar then
-    TNewProgressBar(Dest).Anchors := TNewProgressBar(Src).Anchors;
+  N := GetArrayLength(DownloadControls);
+  SetArrayLength(DownloadControls, N+1);
+  DownloadControls[N] := Dest;
+
+  if Src <> nil then begin
+    Dest.Left := Src.Left;
+    Dest.Top := Src.Top;
+    Dest.Width := Src.Width;
+    Dest.Height := Src.Height;
+    if Src is TNewStaticText then
+      TNewStaticText(Dest).Anchors := TNewStaticText(Src).Anchors
+    else if Src is TNewProgressBar then
+      TNewProgressBar(Dest).Anchors := TNewProgressBar(Src).Anchors;
+  end;
   Dest.Visible := False;
   Dest.Visible := False;
   Dest.Parent := Parent;
   Dest.Parent := Parent;
 end;
 end;
 
 
+procedure DownloadAbortButtonClick(Sender: TObject);
+begin
+  NeedToAbortDownload := MsgBox('Are you sure you want to stop the download?', mbConfirmation, MB_YESNO) = IDYES;
+end;
+
 procedure CreateDownloadControls;
 procedure CreateDownloadControls;
 var
 var
   Page: TWizardPage;
   Page: TWizardPage;
@@ -55,6 +71,15 @@ begin
   SetupDownloadControl(DownloadFilenameLabel, WizardForm.FilenameLabel, Page.Surface);
   SetupDownloadControl(DownloadFilenameLabel, WizardForm.FilenameLabel, Page.Surface);
   DownloadProgressBar:= TNewProgressBar.Create(Page);
   DownloadProgressBar:= TNewProgressBar.Create(Page);
   SetupDownloadControl(DownloadProgressBar, WizardForm.ProgressGauge, Page.Surface);
   SetupDownloadControl(DownloadProgressBar, WizardForm.ProgressGauge, Page.Surface);
+  DownloadAbortButton := TNewButton.Create(Page);
+  SetupDownloadControl(DownloadAbortButton, nil, Page.Surface);
+
+  DownloadAbortButton.Caption := '&Stop download';
+  DownloadAbortButton.Top := DownloadProgressBar.Top + DownloadProgressBar.Height + ScaleY(8);
+  DownloadAbortButton.Height := WizardForm.CancelButton.Height;
+  DownloadAbortButton.Width := WizardForm.CalculateButtonWidth([DownloadAbortButton.Caption]);
+  DownloadAbortButton.Anchors := [akLeft, akTop];
+  DownloadAbortButton.OnClick := @DownloadAbortButtonClick;
 end;
 end;
 
 
 procedure InitializeWizard;
 procedure InitializeWizard;
@@ -64,40 +89,48 @@ end;
 
 
 function OnDownloadProgress(const Url, FileName: String; const Progress, ProgressMax: Int64): Boolean;
 function OnDownloadProgress(const Url, FileName: String; const Progress, ProgressMax: Int64): Boolean;
 begin
 begin
-  if ProgressMax <> 0 then
-    Log(Format('  %d of %d bytes done.', [Progress, ProgressMax]))
-  else
-    Log(Format('  %d bytes done.', [Progress]));
-  
-  DownloadFilenameLabel.Caption := Url;
-  DownloadFilenameLabel.Update;
-
-  if ProgressMax <> 0 then begin
-    DownloadProgressBar.Style := npbstNormal;
-    DownloadProgressBar.Max := ProgressMax;
-    DownloadProgressBar.Position := Progress;
-  end else
-    DownloadProgressBar.Style := npbstMarquee;
-  DownloadProgressBar.Update;
-
-  Result := True;
+  if NeedToAbortDownload then begin
+    Log('Need to abort download.');
+    Result := False;
+  end else begin
+    if ProgressMax <> 0 then
+      Log(Format('  %d of %d bytes done.', [Progress, ProgressMax]))
+    else
+      Log(Format('  %d bytes done.', [Progress]));
+    
+    DownloadFilenameLabel.Caption := Url;
+    DownloadFilenameLabel.Update;
+
+    if ProgressMax <> 0 then begin
+      DownloadProgressBar.Style := npbstNormal;
+      DownloadProgressBar.Max := ProgressMax;
+      DownloadProgressBar.Position := Progress;
+    end else
+      DownloadProgressBar.Style := npbstMarquee;
+    DownloadProgressBar.Update;
+
+    Result := True;
+  end;
+end;
+
+procedure ShowDownloadControls(const AVisible: Boolean);
+var
+  I: Integer;
+begin
+  for I := 0 to GetArrayLength(DownloadControls)-1 do
+    DownloadControls[I].Visible := AVisible;
 end;
 end;
 
 
 procedure DownloadFiles;
 procedure DownloadFiles;
 begin
 begin
   try
   try
-    DownloadStatusLabel.Visible := True;
     DownloadStatusLabel.Caption := 'Downloading additional files...';
     DownloadStatusLabel.Caption := 'Downloading additional files...';
-    DownloadStatusLabel.Update;
-    DownloadFilenameLabel.Visible := True;
-    DownloadProgressBar.Visible := True;
-    
+    ShowDownloadControls(True);
+    NeedToAbortDownload := False;
     DownloadTemporaryFile('https://jrsoftware.org/download.php/is.exe', 'innosetup-latest.exe', '', @OnDownloadProgress);
     DownloadTemporaryFile('https://jrsoftware.org/download.php/is.exe', 'innosetup-latest.exe', '', @OnDownloadProgress);
     DownloadTemporaryFile('https://jrsoftware.org/download.php/iscrypt.dll', 'ISCrypt.dll', '2f6294f9aa09f59a574b5dcd33be54e16b39377984f3d5658cda44950fa0f8fc', @OnDownloadProgress);
     DownloadTemporaryFile('https://jrsoftware.org/download.php/iscrypt.dll', 'ISCrypt.dll', '2f6294f9aa09f59a574b5dcd33be54e16b39377984f3d5658cda44950fa0f8fc', @OnDownloadProgress);
   finally
   finally
-    DownloadStatusLabel.Visible := False;
-    DownloadFilenameLabel.Visible := False;
-    DownloadProgressBar.Visible := False;
+    ShowDownloadControls(False);
   end;
   end;
 end;
 end;
 
 

+ 1 - 1
whatsnew.htm

@@ -52,7 +52,7 @@ For conditions of distribution and use, see <a href="https://jrsoftware.org/file
 <ul>
 <ul>
   <li>Added new <tt>DownloadTemporaryFile</tt> support function to download files without using a third-party tool:
   <li>Added new <tt>DownloadTemporaryFile</tt> support function to download files without using a third-party tool:
   <ul>
   <ul>
-    <li>Allows you <a href="https://i.imgur.com/diBUvLn.png">show the download progress</a> to the user. See the new <i>CodeDownloadFiles.iss</i> example script for an example.</li>
+    <li>Allows you to <a href="https://i.imgur.com/ZLmiS3t.png">show the download progress</a> to the user. See the new <i>CodeDownloadFiles.iss</i> example script for an example.</li>
     <li>Supports HTTPS (but not expired or self-signed certificates) and HTTP.</li>
     <li>Supports HTTPS (but not expired or self-signed certificates) and HTTP.</li>
     <li>Redirects are automatically followed and proxy settings are automatically used.</li>
     <li>Redirects are automatically followed and proxy settings are automatically used.</li>
     <li>Safe to use from services unlike existing third-party tools.</li>
     <li>Safe to use from services unlike existing third-party tools.</li>