在delphi 7中使用idhttp抓取网页,造成窗口无反应的假死状态。通过搜索获得两种方法。
1.写在线程中,但是调用比较麻烦
2.使用delphi 提供的idantifreeze(必须安装indy)。在indy misc中将idfreeantifreeze放入程序中,
将OnlyWhenIdle状态修改为False即可。方便简单。
=====================================
直接采用Delphi自带Control控件INDY组件为例.新建个工程,放上个TIdHTTPControl控件,个TIdAntiFreezeControl控件,个TProgressBar用于显示下载进度.最后放上个TButton用于开始执行我们命令.代码如下:
procedure TForm1.Button1Click(Sender: TObject);//点击按钮时候开始下载我们文件
var
MyStream:TMemoryStream;
begin
IdAntiFreeze1.OnlyWhenIdle:=False;//设置使有反应.
MyStream:=TMemoryStream.Create;
try
IdHTTP1.Get('http://www.138soft.com/download/Mp3ToExe.zip',MyStream);//下载我站点个ZIP文件
except//INDYControl控件般要使用这种try..except结构.
Showmessage('网络出错!');
MyStream.Free;
Exit;
end;
MyStream.SaveToFile('c:\Mp3ToExe.zip');
MyStream.Free;
Showmessage('OK');
end;
procedure TForm1.IdHTTP1WorkBegin(Sender: TObject; AWorkMode: TWorkMode;
const AWorkCountMax: Integer);//开始下载前,将ProgressBar1最大值设置为需要接收数据大小.
begin
ProgressBar1.Max:=AWorkCountMax;
ProgressBar1.Min:=0;
ProgressBar1.Position:=0;
end;
procedure TForm1.IdHTTP1Work(Sender: TObject; AWorkMode: TWorkMode;
const AWorkCount: Integer);//接收数据时候,进度将在ProgressBar1显示出来.
begin
ProgressBar1.Position:=ProgressBar1.Position+AWorkCount;
end;
IdHTTP1Get还有种形式就是获取串:例如,上面可以改写成:
procedure TForm1.Button1Click(Sender: TObject);
var
MyStr:String;
begin
IdAntiFreeze1.OnlyWhenIdle:=False;//设置使有反应.
try
MyStr:=IdHTTP1.Get('http://www.138soft.com/default.htm');
except
Showmessage('网络出错!');
Exit;
end;
Showmessage(MyStr);
end;
出处:http://hi.baidu.com/yuqingtan/blog/item/a9771c3d793cb6f73c6d9737.html
http://www.vckbase.com/module/articleContent.php?id=4385