⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit_thread.pas

📁 学习delphi一段时间了
💻 PAS
字号:
unit Unit_thread;

interface
uses
    Windows,Classes,IdHTTP,SysUtils,IniFiles,ADODB,Unit_Const;
type
  TWeatherThread = class(TThread)
    protected
       procedure execute;override;
    private
       StrTemp : String;
          ustr : String;
        StrMsg : String;
        WeatherQuery : TADOQuery;
        IdHTTP1 : TIdHttp;
       function  ReadIP(IPUrl: String): String;
       procedure OpenURL;
       function GetURLStr(URLStr: String):String;
       function GetImgName(URLStr:String):String;
       function FilterStr(Str:string):String;
       procedure WriteHtmlTxt(HtmlStr: String);
    public
       Constructor Create();
       destructor destroy;override;
end;
implementation

uses Unit_Weather;

{ TWeatherThread }
function TWeatherThread.ReadIP(IPUrl: String): String;//得到外网ip然后得到城市名称
var
     IpStr,IPCityName : String;
     FindStr1,FindStr2:String;
     Pos1,Pos2: integer;
     WeatherIni : TIniFile;
begin
     try
       Frm_Weather.HintLab.Caption:='正在获取本地外网IP...';
       Frm_Weather.progressbar1.Position:=100;
       FindStr1:='您来自';
       FindStr2:=')';
       IpStr:=IdHttp1.Get(IPUrl);
       Pos1:=Pos(FindStr1,IpStr);
       Delete(IPStr,1,Pos1-1);
       Pos2:=Pos(FindStr2,IPStr);
       IPStr:=Copy(IpStr,Length(FindStr1)+1,Pos2-Length(FindStr1));  //得到ip地址

       Pos2:=Pos('(',IPStr);
       IP:=Copy(IPStr,1,Pos2-1);
       IPCityName:=Copy(IPStr,Pos2,Length(IPStr)-1);

       PostMessage(Frm_Weather.Handle,WM_WeatherMsg,0,1);
       //得到城市名称,写入配置文件
       WeatherIni:=TIniFile.Create(AppPath+'Weather.ini');
       WeatherIni.WriteString('Ip','Ip',IP);
       if CityName='' then
       begin
           with WeatherQuery do
           begin
                Close;
                Open;
                while not eof do
                begin
                     if (Pos(Fields[1].AsString,IPCityName)>0)or(Pos(Fields[2].AsString,IPCityName)>0) then
                     begin
                          CityName:=Fields[2].AsString;
                          WeatherIni.WriteString('city','cityname',CityName);
                          Break;
                     end;
                     Next;
                end;
                Close;
           end;
       end;
       WeatherIni.Free;
       Frm_Weather.progressbar1.Position:=0;
       IPStr:='';
     except
       PostMessage(Frm_Weather.Handle,WM_WeatherMsg,0,0);
       Frm_Weather.HintLab.Caption:='本地外网IP获取失败';
       Frm_Weather.progressbar1.Position:=0;
       IPStr:='';
     end;
end;

procedure TWeatherThread.OpenURL;
var
     SParams: TStringStream;
begin
     if CityName='' then
     begin
          PostMessage(Frm_Weather.Handle,WM_WeatherMsg,0,3);
          Exit;
     end;
     StrTemp:='';
     SParams := TStringStream.create('');
     Frm_Weather.HintLab.Caption:='正在读取天气信息';
     Frm_Weather.progressbar1.Position:=100;
     try
        IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
        SParams.WriteString('searchword='+CityName) ;
        StrMsg:=IdHTTP1.post(WeatherURL+CityName,SParams);
        UStr:=GetURLStr(StrMsg);
        if UStr<>'' then
        begin
             GetImgName(UStr);
             FilterStr(UStr);
             WriteHtmlTxt(Frm_Weather.memo1.Text+strtemp+' </body></html>');
        end;
        SParams.Free;
        Frm_Weather.HintLab.Caption:='读取天气信息完成';
        Frm_Weather.progressbar1.Position:=0;
        PostMessage(Frm_Weather.Handle,WM_WeatherMsg,0,4);
     except
        Frm_Weather.HintLab.Caption:='读取天气信息失败';
        Frm_Weather.progressbar1.Position:=0;
        SParams.Free;
     end;
end;
function TWeatherThread.GetURLStr(URLStr: String):String;
var
     strPos1,strpos2: Integer;
     BeginStr,EndStr:String;
begin
     Frm_Weather.HintLab.Caption:='正在提取天气信息';
     Frm_Weather.progressbar1.Position:=100;
     BeginStr:='<!--右开始-->';
     EndStr:='<!--右结束-->';
     try
        strpos1:=pos(BeginStr,URLStr);
        strpos2:=pos(EndStr,URLStr);
        Result:=Copy(URLStr,strpos1,strpos2-strpos1-1);
        Frm_Weather.progressbar1.Position:=0;
     except
        Frm_Weather.HintLab.Caption:='提取天气信息失败';
        Frm_Weather.progressbar1.Position:=0;
        Result:='';
     end;
end;
function TWeatherThread.FilterStr(str:string):String;
var
     getStr,FindStr:String;
     FStrPos : Integer;
     FstrLen : Integer;
begin
     Frm_Weather.HintLab.Caption:='正在提取有效信息';
     Frm_Weather.progressbar1.Position:=100;
     try
     getStr:='';
     FindStr:='/cma_new/tqyb/';
     FstrLen:=Length(str);
     FStrPos:=Pos(FindStr,str);
     if FStrPos<>0 then
     begin
          strtemp:=strtemp+copy(str,1,FStrPos-1);
          str:=Copy(str,FStrPos+Length(FindStr),FstrLen-FStrPos-Length(FindStr));
          FilterStr(str);
     end else
     begin
          strtemp:=strtemp+str;
          Result:=strtemp;
          Frm_Weather.HintLab.Caption:='提取有效信息完成';
          Frm_Weather.progressbar1.Position:=0;
     end;
     except
         Frm_Weather.HintLab.Caption:='提取有效信息失败';
         Frm_Weather.progressbar1.Position:=0;
     end;
end;
procedure TWeatherThread.WriteHtmlTxt(HtmlStr: String);
var
     HtmlTxtFile : TextFile;
begin
     Frm_Weather.HintLab.Caption:='正在保存天气信息';
     Frm_Weather.progressbar1.Position:=100;
     try
        DeleteFile(AppPath+'temp\weather.html');
        AssignFile(HtmlTxtFile,AppPath+'temp\temphtml.txt');
        ReWrite(HtmlTxtFile);
        Write(HtmlTxtFile,HtmlStr);
        CloseFile(HtmlTxtFile);
        CopyFile(PChar(AppPath+'\temp\temphtml.txt'),PChar(AppPath+'\temp\weather.html'),true);

        Frm_Weather.WebBrowser1.Navigate(AppPath+'temp\weather.html');

        Frm_Weather.HintLab.Caption:='保存天气信息完成';
        Frm_Weather.progressbar1.Position:=0;
     except
          Frm_Weather.HintLab.Caption:='保存天气信息失败';
          Frm_Weather.progressbar1.Position:=0;
     end;
end;
procedure TWeatherThread.execute;
begin
     try
        ReadIP(IPUrl);
        OpenURL;
        Sleep(10);
     except
        Self.destroy;
     end;
end;

constructor TWeatherThread.Create;
var
     DataSourceStr:String;
begin
     inherited Create(False);
     DataSourceStr:=AppPath+'Citydb.db';
     IdHttp1:=TIdHttp.Create(nil);
     IdHttp1.ReadTimeout:=30000; //超时设为30秒
     WeatherQuery:=TADOQuery.Create(nil);
     with WeatherQuery do
     begin
          ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='
                            +DataSourceStr+';Persist Security Info=False';
          SQL.Text:='select * from city order by id';
     end;
     FreeOnTerminate:=True;
end;

destructor TWeatherThread.destroy;
begin
     StrTemp:='';
     Ustr:='';
     StrMsg:='';
     IdHTTP1.FreeOnRelease; //释放销毁
     WeatherQuery.Free;
     inherited;
end;

function TWeatherThread.GetImgName(URLStr: String): String;  //得到图片并下载
var
     ImgName : String;
     FindStr1,FindStr2:String;
     ImgBeginPos:Integer;
     FStrPos1,FStrPos2:Integer;
     ImgStream:TMemoryStream;
begin
     Frm_Weather.HintLab.Caption:='正在保存图片信息';
     Frm_Weather.progressbar1.Position:=100;
     FindStr1:='/cma_new/tqyb/images/';
     FindStr2:='.gif';
     FStrPos1:=Pos(FindStr1,URLStr);
     FStrPos2:=Pos(FindStr2,URLStr);
     ImgBeginPos:=FStrPos1+Length(FindStr1);
     ImgName:=Copy(URLStr,ImgBeginPos,FStrPos2-ImgBeginPos)+'.gif';//得到图片名称
     
     if FStrPos2=0 then
     begin
          Result:='end';
          Frm_Weather.HintLab.Caption:='保存图片信息完成';
          Frm_Weather.progressbar1.Position:=0;
          Exit;
     end;
     if not FileExists(AppPath+'temp\images\'+ImgName) then //如果文件内无,则下载到本地
     begin
        ImgStream:=TMemoryStream.Create;
        try
          IdHTTP1.Get(ImgUrl+ImgName,ImgStream);//下载一个.gif文件
        except
          ImgStream.Free;
          Frm_Weather.HintLab.Caption:='保存图片信息失败';
          Frm_Weather.progressbar1.Position:=0;
          Exit;
        end;
        try
          ImgStream.SaveToFile(AppPath+'temp\images\'+ImgName);
        except
          ImgStream.Free;
        end;
     end;

     Delete(URLStr,1,FStrPos2+Length(FindStr2));
     GetImgName(URLStr); //递归调用
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -