L
lkjxlove
Unregistered / Unconfirmed
GUEST, unregistred user!
大家好,小弟有事请教各位,下面是我写的一个关于IdHttp下载,下载过程中程序窗口不能移动,看了许多网站说要创建线程,但我对线程不是太了解,麻烦大家帮个忙,要怎么样改才能使用线程下载?谢谢大家
unit MyThreadPro;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, jpeg, ExtCtrls, Buttons, ComCtrls, IdBaseComponent,
IdComponent, IdTCPConnection, IdTCPClient, IdHTTP,IniFiles;
type
TForm1 = class(TForm)
Image1: TImage;
GroupBox1: TGroupBox;
FillBar: TProgressBar;
AllBar: TProgressBar;
StartDown: TBitBtn;
StopDown: TBitBtn;
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
IdHTTP1: TIdHTTP;
do
wnlist: TListBox;
BitBtn1: TBitBtn;
procedure Button1Click(Sender: TObject);
procedure StartDownClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
DispStr:String;
//显示正在执行哪个动作的信息
function GetURLFileName(aURL: string): string;
functiondo
wnLoad(aURL:string): string;
public
{ Public declarations }
end;
var
Form1: TForm1;
DispStr:String;
//显示正在执行哪个动作的信息
afile,aFileName:string;
Apath:string;
do
wnList:TStringList;
ini:TInifile;
implementation
{$R *.dfm}
//获取文件名
function TForm1.GetURLFileName(aURL: string): string;
var
i: integer;
s: string;
begin
s := aURL;
i := Pos('/', s);
while i <> 0do
//去掉"/"前面的内容剩下的就是文件名了
begin
Delete(s, 1, i);
i := Pos('/', s);
end;
Result := s;
end;
//加载信息
procedure TForm1.FormCreate(Sender: TObject);
var
ini:TInifile;
i:integer;
begin
Apath:=ExtractFilePath(Paramstr(0));
ini:=TInifile.Create(ExtractFilePath(Paramstr(0))+'Update.ini');
try
if not FileExists(ExtractFilePath(Paramstr(0))+'Update.ini') then
begin
ini.WriteInteger('Form','top',self.Top);
ini.WriteInteger('Form','left',self.Left);
ini.WriteInteger('Form','width',self.Width);
ini.WriteInteger('Form','height',self.Height);
end
else
begin
self.Top:=ini.ReadInteger('Form','top',100);
self.Left:=ini.ReadInteger('Form','left',100);
self.Width:=ini.ReadInteger('Form','width',460);
self.Height:=ini.ReadInteger('Form','height',340);
for i:=0 to ini.ReadInteger('list','count',0)-1do
do
wnlist.Items.Add(ini.ReadString('list',inttostr(i),''));
end;
finally
ini.Free;
end;
end;
//开始下载
procedure TForm1.StartDownClick(Sender: TObject);
var
i:integer;
hThread:Thandle;//定义一个句柄
ThreadIDWord;
begin
try
Refresh;
except
on E:Exceptiondo
begin
Application.MessageBox(PChar('获取升级配置文件失败,请梢候重试'+#13+#13+E.Message),PChar('系统提示'),MB_OK+MB_ICONERROR);
Exit;
end;
end;
StopDown.Enabled:=true;
do
wnlist.Clear;
hthread:=CreateThread(nil,0,@TForm1.GetURLFileName,nil,0,ThreadID);
if hThread=0 then
messagebox(Handle,'Didn’t Create a Thread',nil,MB_OK);
Apath:=ExtractFilePath(Paramstr(0));
ini:=TInifile.Create(ExtractFilePath(Paramstr(0))+'Update.ini');
Label3.Caption:=Format(DispStr,[aFile]);
Refresh;
for i:=0 to ini.ReadInteger('list','count',0)-1do
do
wnLoad(ini.ReadString('list',inttostr(i),''));
Refresh;
StopDown.Caption:='退出';
Application.MessageBox(PChar('OK,程序升级成功!'),PChar('系统提示'),MB_OK+MB_ICONINFORMATION);
end;
//下载程序
function TForm1.DownLoad(aURL:string): string;
var
do
wnLoadFile:TfileStream;
f: file of Byte;
begin
aFile := GetURLFileName(aURL);
Apath:=ExtractFilePath(Paramstr(0));
DispStr:='正在下载:'+aFile;
aFileName:=copy(aFile,Pos('/',aFile)+1,Length(aFile));
Label3.Caption:=Format(DispStr,[aFileName]);
Refresh;
do
wnLoadFile:=TFileStream.Create(Apath+aFile,fmCreate);
IdHTTP1.Get(aURL,DownLoadFile);
IdHTTP1.Free;
application.ProcessMessages;
//释放内存
do
wnLoadFile.Free;
AssignFile(f,Apath+aFile);
Reset(f);
application.ProcessMessages;
CloseFile(f);
Refresh;
//下载菜单显示下载完成的文件名
do
wnList.Items.Add(aFile+'已下载成功');
Refresh;
//备份文件
copyfile(pchar(Apath+aFile), pchar(Apath + 'bak/' +'[备份文件]' +aFile), false);
end;
end.
unit MyThreadPro;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, jpeg, ExtCtrls, Buttons, ComCtrls, IdBaseComponent,
IdComponent, IdTCPConnection, IdTCPClient, IdHTTP,IniFiles;
type
TForm1 = class(TForm)
Image1: TImage;
GroupBox1: TGroupBox;
FillBar: TProgressBar;
AllBar: TProgressBar;
StartDown: TBitBtn;
StopDown: TBitBtn;
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
IdHTTP1: TIdHTTP;
do
wnlist: TListBox;
BitBtn1: TBitBtn;
procedure Button1Click(Sender: TObject);
procedure StartDownClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
DispStr:String;
//显示正在执行哪个动作的信息
function GetURLFileName(aURL: string): string;
functiondo
wnLoad(aURL:string): string;
public
{ Public declarations }
end;
var
Form1: TForm1;
DispStr:String;
//显示正在执行哪个动作的信息
afile,aFileName:string;
Apath:string;
do
wnList:TStringList;
ini:TInifile;
implementation
{$R *.dfm}
//获取文件名
function TForm1.GetURLFileName(aURL: string): string;
var
i: integer;
s: string;
begin
s := aURL;
i := Pos('/', s);
while i <> 0do
//去掉"/"前面的内容剩下的就是文件名了
begin
Delete(s, 1, i);
i := Pos('/', s);
end;
Result := s;
end;
//加载信息
procedure TForm1.FormCreate(Sender: TObject);
var
ini:TInifile;
i:integer;
begin
Apath:=ExtractFilePath(Paramstr(0));
ini:=TInifile.Create(ExtractFilePath(Paramstr(0))+'Update.ini');
try
if not FileExists(ExtractFilePath(Paramstr(0))+'Update.ini') then
begin
ini.WriteInteger('Form','top',self.Top);
ini.WriteInteger('Form','left',self.Left);
ini.WriteInteger('Form','width',self.Width);
ini.WriteInteger('Form','height',self.Height);
end
else
begin
self.Top:=ini.ReadInteger('Form','top',100);
self.Left:=ini.ReadInteger('Form','left',100);
self.Width:=ini.ReadInteger('Form','width',460);
self.Height:=ini.ReadInteger('Form','height',340);
for i:=0 to ini.ReadInteger('list','count',0)-1do
do
wnlist.Items.Add(ini.ReadString('list',inttostr(i),''));
end;
finally
ini.Free;
end;
end;
//开始下载
procedure TForm1.StartDownClick(Sender: TObject);
var
i:integer;
hThread:Thandle;//定义一个句柄
ThreadIDWord;
begin
try
Refresh;
except
on E:Exceptiondo
begin
Application.MessageBox(PChar('获取升级配置文件失败,请梢候重试'+#13+#13+E.Message),PChar('系统提示'),MB_OK+MB_ICONERROR);
Exit;
end;
end;
StopDown.Enabled:=true;
do
wnlist.Clear;
hthread:=CreateThread(nil,0,@TForm1.GetURLFileName,nil,0,ThreadID);
if hThread=0 then
messagebox(Handle,'Didn’t Create a Thread',nil,MB_OK);
Apath:=ExtractFilePath(Paramstr(0));
ini:=TInifile.Create(ExtractFilePath(Paramstr(0))+'Update.ini');
Label3.Caption:=Format(DispStr,[aFile]);
Refresh;
for i:=0 to ini.ReadInteger('list','count',0)-1do
do
wnLoad(ini.ReadString('list',inttostr(i),''));
Refresh;
StopDown.Caption:='退出';
Application.MessageBox(PChar('OK,程序升级成功!'),PChar('系统提示'),MB_OK+MB_ICONINFORMATION);
end;
//下载程序
function TForm1.DownLoad(aURL:string): string;
var
do
wnLoadFile:TfileStream;
f: file of Byte;
begin
aFile := GetURLFileName(aURL);
Apath:=ExtractFilePath(Paramstr(0));
DispStr:='正在下载:'+aFile;
aFileName:=copy(aFile,Pos('/',aFile)+1,Length(aFile));
Label3.Caption:=Format(DispStr,[aFileName]);
Refresh;
do
wnLoadFile:=TFileStream.Create(Apath+aFile,fmCreate);
IdHTTP1.Get(aURL,DownLoadFile);
IdHTTP1.Free;
application.ProcessMessages;
//释放内存
do
wnLoadFile.Free;
AssignFile(f,Apath+aFile);
Reset(f);
application.ProcessMessages;
CloseFile(f);
Refresh;
//下载菜单显示下载完成的文件名
do
wnList.Items.Add(aFile+'已下载成功');
Refresh;
//备份文件
copyfile(pchar(Apath+aFile), pchar(Apath + 'bak/' +'[备份文件]' +aFile), false);
end;
end.