procedure TFrmDownLoad.DownCompelete(Sender: TFileDownLoadThread);
begin
Label4.Caption:='下载完毕';
BtnClose.Caption:='关 闭';
BtnOpen.Enabled:=True;;
BtnOpenPath.Enabled:=true;
if IsClose.Checked then
Self.Close;
end;
procedure TFrmDownLoad.DownFaild(Sender: TFileDownLoadThread;
Reason: Integer);
begin
Label4.Caption:='下载失败';
end;
procedure TFrmDownLoad.FormShow(Sender: TObject);
var
downthread:TFileDownLoadThread;
begin
try
FSuccessful:=False;
BtnOpen.Enabled:=False;
BtnOpenPath.Enabled:=False;
BtnClose.Caption:='取 消';
downthread:=TFileDownLoadThread.Create(Self.SourceURL,Self.SaveFileName,DownProgress,DownCompelete,DownFaild,False);
downthread.FreeOnTerminate:=True;
SavePathLb.Caption:=SaveFileName;
except
Self.Close;
end;
end;
procedure TFrmDownLoad.BtnCloseClick(Sender: TObject);
begin
end;
procedure TFrmDownLoad.BtnOpenClick(Sender: TObject);
var
openfile:String;
begin
openfile:=SaveFileName;
if FileExists(openfile) then
begin
if ShellExecute(0, 'Open', PChar(openfile), '', '', SW_SHOWNORMAL) > 32 then
Close
else
ShowMessage('打开文件失败');
end;
end;
procedure TFrmDownLoad.BtnOpenPathClick(Sender: TObject);
begin
if DirectoryExists( ExtractFilePath(SaveFileName)) then
ShellExecute(0, 'Open', PChar(ExtractFilePath(SaveFileName)), '', '', SW_SHOWNORMAL);
end;
constructor TDownLoadMonitor.Create(AThread: TFileDownLoadThread);
begin
inherited Create;
FThread:=AThread;
FShouldAbort:=False;
end;
function TDownLoadMonitor.GetBindInfo( out grfBINDF: DWORD; var bindinfo: TBindInfo ): HResult;
begin
result := S_OK;
end;
function TDownLoadMonitor.GetPriority( out nPriority ): HResult;
begin
Result := S_OK;
end;
function TDownLoadMonitor.OnDataAvailable( grfBSCF, dwSize: DWORD; formatetc: PFormatEtc; stgmed: PStgMedium ): HResult;
begin
Result := S_OK;
end;
function TDownLoadMonitor.OnLowResource( reserved: DWORD ): HResult;
begin
Result := S_OK;
end;
function TDownLoadMonitor.OnObjectAvailable( const iid: TGUID; punk: IInterface ): HResult;
begin
Result := S_OK;
end;
function TDownLoadMonitor.OnProgress( ulProgress, ulProgressMax, ulStatusCode: ULONG; szStatusText: LPCWSTR ): HResult;
begin
if FThread<>nil then
FThread.UpdateProgress(ulProgress,ulProgressMax,ulStatusCode,'');
if FShouldAbort then
Result := E_ABORT
else
Result := S_OK;
end;
function TDownLoadMonitor.OnStartBinding( dwReserved: DWORD; pib: IBinding ): HResult;
begin
Result := S_OK;
end;
function TDownLoadMonitor.OnStopBinding( hresult: HResult; szError: LPCWSTR ): HResult;
begin
Result := S_OK;
end;
{ TFileDownLoadThread }
constructor TFileDownLoadThread.Create( ASrcURL, ASaveFileName: string;AProgressEvent:TDownLoadProcessEvent ;
ACompleteEvent:TDownLoadCompleteEvent;AFailEvent:TDownLoadFailEvent; CreateSuspended: Boolean );
begin
if (@AProgressEvent=nil) or (@ACompleteEvent=nil) or (@AFailEvent=nil) then
CreateSuspended:=True;
inherited Create( CreateSuspended );
FSourceURL:=ASrcURL;
FSaveFileName:=ASaveFileName;
FOnProcess:=AProgressEvent;
FOnComplete:=ACompleteEvent;
FOnFail:=AFailEvent;
end;
procedure TFileDownLoadThread.DoUpdateUI;
begin
if Assigned(FOnProcess) then
FOnProcess(Self,FProgress,FProgressMax,FulStatusCode,FszStatusText);
end;
procedure TFileDownLoadThread.Execute;
var
DownRet:HRESULT;
begin
inherited;
FMonitor:=TDownLoadMonitor.Create(Self);
DownRet:= URLDownloadToFile( nil, PAnsiChar( FSourceURL ), PAnsiChar( FSaveFileName ), 0,FMonitor as IBindStatusCallback);
if DownRet=S_OK then
begin
if Assigned(FOnComplete) then
FOnComplete(Self);
end
else
begin
if Assigned(FOnFail) then
FOnFail(Self,DownRet);
end;
FMonitor:=nil;
end;
procedure TFileDownLoadThread.UpdateProgress(Progress, ProgressMax, StatusCode: Cardinal; StatusText: string);
begin
FProgress:=Progress;
FProgressMax:=ProgressMax;
Synchronize(DoUpdateUI);
if Terminated then
FMonitor.ShouldAbort:=True;
end;