快乐的小牛虻 發表於 2014-7-17 10:28:14

Delphi用TActionList实现下载文件的方法

<p>Delphi中的TActionList有个标准动作TDownLoadURL,内部是使用的URLDownloadToFile,它下载文件时会定时产生OnDownloadProgress 事件,这样就可以用进度条显示。<br />
<br />
本文讲述了Delphi用TActionList实现下载文件的方法,实现代码如下所示:</p>
<div class="jb51code">
<pre class="brush:delphi;">
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtActns, ActnList, StdCtrls, ComCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
ActionList1: TActionList;
ProgressBar1: TProgressBar;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure URL_OnDownloadProgress
       (Sender: TDownLoadURL;
       Progress, ProgressMax: Cardinal;
       StatusCode: TURLDownloadStatus;
       StatusText: String; var Cancel: Boolean) ;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure Tform1.URL_OnDownloadProgress;
begin
ProgressBar1.Max:= ProgressMax;
ProgressBar1.Position:= Progress;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
with TDownloadURL.Create(self) do
try
   URL:='https://www.jb51.net/images/logo.gif';
   FileName := 'logo.gif';
   OnDownloadProgress := URL_OnDownloadProgress;
   ExecuteTarget(nil) ;
finally
   Free;
end;
showMessage('OK');
ProgressBar1.Max := 0;
end;
</pre>
</div>
                           
                            <div class="art_xg">
                              <b>您可能感兴趣的文章:</b><ul><li>Delphi实现木马文件传输代码实例</li><li>Delphi中判断文件是否为文本文件的函数</li><li>Delphi建立、读取、存贮INI文件的方法《一》</li><li>delphi建立、读取、存贮INI文件的方法《二》</li><li>delphi建立、读取、存贮INI文件的方法《三》</li><li>delphi制作wav文件的方法</li></ul>
                            </div>

                        </div>
                        <!--endmain-->
頁: [1]
查看完整版本: Delphi用TActionList实现下载文件的方法