ech dzień dziecka:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtActns, StdCtrls, ComCtrls;
type
TOnProgressEvent = procedure(Sender: TObject; Progress, ProgressMax: Cardinal) of object;
TThreadDownloader = class(TThread)
private
fDownloader: TDownloadURL;
fCancel: Boolean;
fProgress, fProgressMax: Cardinal;
fOnProgressEvent: TOnProgressEvent;
procedure DoOnProgress;
procedure OnProgress(Sender: TDownLoadURL; Progress, ProgressMax: Cardinal;
StatusCode: TURLDownloadStatus; StatusText: String; var Cancel: Boolean);
protected
procedure Execute; override;
public
constructor Create(Src, Dest: string; Progress: TOnProgressEvent);
destructor Destroy; override;
procedure Start;
procedure Cancel;
end;
TForm1 = class(TForm)
btnStart: TButton;
btnStop: TButton;
ProgressBar1: TProgressBar;
procedure FormCreate(Sender: TObject);
procedure btnStartClick(Sender: TObject);
procedure btnStopClick(Sender: TObject);
private
{ Private declarations }
fThreadDownloader: TThreadDownloader;
procedure OnDownloadProgress(Sender: TObject; Progress, ProgressMax: Cardinal);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TThreadDownloader.DoOnProgress;
begin
if Assigned(fOnProgressEvent) then
fOnProgressEvent(Self, fProgress, fProgressMax);
end;
procedure TThreadDownloader.OnProgress(Sender: TDownLoadURL; Progress, ProgressMax: Cardinal;
StatusCode: TURLDownloadStatus; StatusText: String; var Cancel: Boolean);
begin
Cancel:= fCancel;
fProgress:= Progress;
fProgressMax:= ProgressMax;
Synchronize(DoOnProgress);
end;
procedure TThreadDownloader.Execute;
begin
fDownloader.ExecuteTarget(nil);
end;
procedure TThreadDownloader.Cancel;
begin
fCancel:= True;
end;
procedure TThreadDownloader.Start;
begin
Resume;
end;
constructor TThreadDownloader.Create(Src, Dest: string; Progress: TOnProgressEvent);
begin
inherited Create(True);
FreeOnTerminate:= True;
fCancel:= False;
fOnProgressEvent:= Progress;
fDownloader:= TDownloadURL.Create(nil);
fDownloader.URL:= Src;
fDownloader.Filename:= Dest;
fDownloader.OnDownloadProgress:= OnProgress;
end;
destructor TThreadDownloader.Destroy;
begin
fDownloader.Free;
inherited Destroy;
end;
//====================== TForm ==========================
procedure TForm1.OnDownloadProgress(Sender: TObject; Progress, ProgressMax: Cardinal);
begin
ProgressBar1.Max:= ProgressMax;
ProgressBar1.Position:= Progress;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
fThreadDownloader:= TThreadDownloader.Create('http://cost_tam', 'D:\jakis_plik', OnDownloadProgress);
end;
procedure TForm1.btnStartClick(Sender: TObject);
begin
fThreadDownloader.Start;
end;
procedure TForm1.btnStopClick(Sender: TObject);
begin
fThreadDownloader.Cancel;
end;
end.
Oczywiście przydało by się dodać zdarzenia OnBeginDownload i OnEndDownload ale to już twoja działka.