用TClientSocket做HTTP文件下载(Norton LiveUpdate,C++Builder)

  1. //---------------------------------------------------------------------------
  2. #ifndef Unit1H
  3. #define Unit1H
  4. //---------------------------------------------------------------------------
  5. #include <Classes.hpp>
  6. #include <Controls.hpp>
  7. #include <StdCtrls.hpp>
  8. #include <Forms.hpp>
  9. #include <ScktComp.hpp>
  10. #include <Buttons.hpp>
  11. #include <Registry.hpp>
  12. #include <ExtCtrls.hpp>
  13. //---------------------------------------------------------------------------
  14. class TForm1 : public TForm
  15. {
  16. __published:    // IDE-managed Components
  17.         TClientSocket *ClientSocket1;
  18.         TMemo *Memo1;
  19.         TPanel *Panel1;
  20.         TButton *Button1;
  21.         TBitBtn *BitBtn1;
  22.         TTimer *Timer1;
  23.         TLabel *Label1;
  24.         TLabel *Label2;
  25.         void __fastcall FormCreate(TObject *Sender);
  26.         void __fastcall ClientSocket1Read(TObject *Sender,
  27.           TCustomWinSocket *Socket);
  28.         void __fastcall ClientSocket1Connect(TObject *Sender,
  29.           TCustomWinSocket *Socket);
  30.         void __fastcall Button1Click(TObject *Sender);
  31.         void __fastcall ClientSocket1Error(TObject *Sender,
  32.           TCustomWinSocket *Socket, TErrorEvent ErrorEvent,
  33.           int &ErrorCode);
  34.         void __fastcall FormCloseQuery(TObject *Sender, bool &CanClose);
  35.         void __fastcall ClientSocket1Disconnect(TObject *Sender,
  36.           TCustomWinSocket *Socket);
  37.         void __fastcall ClientSocket1Connecting(TObject *Sender,
  38.           TCustomWinSocket *Socket);
  39.         void __fastcall ClientSocket1Lookup(TObject *Sender,
  40.           TCustomWinSocket *Socket);
  41.         void __fastcall BitBtn1Click(TObject *Sender);
  42.         void __fastcall Timer1Timer(TObject *Sender);
  43. private:    // User declarations
  44.         AnsiString SourceHTML;
  45.         int Type;
  46.         int height;
  47.         int width;
  48.         TMemoryStream *DownMEM;
  49.         AnsiString Url;
  50.         AnsiString FileName;
  51.         __int64 HeadLength;
  52.         __int64 DataLength;
  53.         __int64 Current;
  54.         __int64 Just;
  55.         TDateTime sTime;
  56.         TDateTime eTime;
  57.         TDateTime mTime;
  58.         FILETIME LastWriteTime;
  59.         AnsiString caption;
  60.         bool EndMe;
  61.         bool IsAuto;
  62.         bool Flg_Q;
  63.         bool __fastcall CheckHead();
  64.         bool __fastcall CheckHead2();
  65.         void __fastcall LStrToTime(AnsiString);
  66. public:     // User declarations
  67.         __fastcall TForm1(TComponent* Owner);
  68.         __fastcall ~TForm1();
  69. };
  70. //---------------------------------------------------------------------------
  71. extern PACKAGE TForm1 *Form1;
  72. //---------------------------------------------------------------------------
  73. #endif
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Unit1.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma resource "*.dfm"
  8. TForm1 *Form1;
  9. //---------------------------------------------------------------------------
  10. bool __fastcall FileExists(AnsiString FileName)
  11. {
  12.    HANDLE FileHandle = CreateFile(FileName.c_str(),
  13.                                   NULL,
  14.                                   FILE_SHARE_READ |
  15.                                   FILE_SHARE_WRITE,
  16.                                   NULL,
  17.                                   OPEN_EXISTING,
  18.                                   FILE_ATTRIBUTE_ARCHIVE |
  19.                                   FILE_ATTRIBUTE_HIDDEN |
  20.                                   FILE_ATTRIBUTE_NORMAL |
  21.                                   FILE_ATTRIBUTE_READONLY |
  22.                                   FILE_ATTRIBUTE_SYSTEM,
  23.                                   NULL
  24.                                  );
  25.    if (FileHandle == INVALID_HANDLE_VALUE)
  26.       return false;
  27.    CloseHandle(FileHandle);
  28.    return true;
  29. }
  30. __int64 SwapPos(AnsiString strSource,AnsiString strFind)
  31. {
  32.    __int64 pPos,pSLen,pFLen;
  33.    pPos=0;
  34.    pSLen=strSource.Length();
  35.    pFLen=strFind.Length();
  36.    if(pSLen==pFLen&&strSource==strFind)
  37.       return 1;
  38.    if(pSLen<pFLen)
  39.       return 0;
  40.    for(__int64 i=0;i<pSLen;i++)
  41.    {
  42.       AnsiString strTmp=strSource.SubString(pSLen-pFLen+1-i,pFLen);
  43.       if(strTmp==strFind)
  44.       {
  45.          pPos=pSLen-pFLen+1-i;
  46.          break;
  47.       }
  48.    }
  49.    return pPos;
  50. }
  51. __int64 HttpDataPos(TStream *strm)
  52. {
  53.    __int64 Pos=strm->Position;
  54.    char ch[5];
  55.    ch[0]='/0';
  56.    ch[1]='/0';
  57.    ch[2]='/0';
  58.    ch[3]='/0';
  59.    ch[4]='/0';
  60.    for(__int64 i=0;i<strm->Size;i++)
  61.    {
  62.       strm->Position=i;
  63.       strm->Read(ch,4);
  64.       if(AnsiString(ch)=="/r/n/r/n")
  65.       {
  66.          strm->Position=Pos;
  67.          return i;
  68.       }
  69.    }
  70.    return 0;
  71. }
  72. __fastcall TForm1::TForm1(TComponent* Owner)
  73.         : TForm(Owner)
  74. {
  75. }
  76. __fastcall TForm1::~TForm1()
  77. {
  78.    DownMEM->Free();
  79. }
  80. //---------------------------------------------------------------------------
  81. void __fastcall TForm1::FormCreate(TObject *Sender)
  82. {
  83.    SourceHTML="";
  84.    Type=0;
  85.    IsAuto=false;
  86.    caption=Caption;
  87.    width=GetSystemMetrics(SM_CXSCREEN);
  88.    height=GetSystemMetrics(SM_CYSCREEN);
  89.    DownMEM=new TMemoryStream();
  90.    if(ParamCount()>0)
  91.       IsAuto=ParamStr(1).UpperCase()=="-A"||ParamStr(1).UpperCase()=="/A";
  92.    if(ParamCount()>1)
  93.       Flg_Q=ParamStr(2).UpperCase()=="-Q"||ParamStr(2).UpperCase()=="/Q";
  94.    if(IsAuto)
  95.    {
  96.       SetWindowPos(Handle,HWND_TOPMOST,
  97.                   0,0,GetSystemMetrics(SM_CXSCREEN),56,SWP_DRAWFRAME);
  98.       Button1->Click();
  99.    }
  100. }
  101. //---------------------------------------------------------------------------
  102. void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
  103.       TCustomWinSocket *Socket)
  104. {
  105.    TMemoryStream *tmpStrm;
  106.    TFileStream *tmpFStrm;
  107.    __int64 lSize,RecvCount;
  108.    int len=Socket->ReceiveLength();
  109.    if(len<=1)
  110.    {
  111.       AnsiString ssss=Socket->ReceiveText();
  112.       return;
  113.    }
  114.    switch(Type)
  115.    {
  116.       case 1:
  117.            DownMEM->Clear();
  118.            DownMEM->SetSize((int)len);
  119.            HeadLength=0;
  120.            DataLength=0;
  121.            FileName="";
  122.            Url="";
  123.            RecvCount = Socket->ReceiveBuf(DownMEM->Memory,len);
  124.            DownMEM->SetSize((int)RecvCount);
  125.            HeadLength=HttpDataPos(DownMEM);
  126.            if(HeadLength>0)
  127.               CheckHead();
  128.            if(HeadLength>0&&DownMEM->Size>=HeadLength+4+DataLength)
  129.               Type=0;
  130.            else
  131.               Type++;
  132.            break;
  133.       case 2:
  134.            lSize=DownMEM->Size;
  135.            DownMEM->Position=lSize;
  136.            tmpStrm=new TMemoryStream();
  137.            try
  138.            {
  139.               tmpStrm->SetSize(len);
  140.               __int64 iRecvd = Socket->ReceiveBuf(tmpStrm->Memory,len);
  141.               tmpStrm->Position = 0;
  142.               DownMEM->CopyFrom(tmpStrm,iRecvd);
  143.            }
  144.            __finally
  145.            {
  146.               tmpStrm->Free();
  147.            }
  148.            if(HeadLength==0)
  149.               HeadLength=HttpDataPos(DownMEM);
  150.            if(HeadLength>0&&DataLength<=0)
  151.               CheckHead();
  152.            if(HeadLength>0)
  153.            {
  154.               if(DownMEM->Size>=HeadLength+4+DataLength)
  155.               {
  156.                  Type=0;
  157.                  DownMEM->Position=0;
  158.                  TStringList *tls;
  159.                  tls=new TStringList();
  160.                  try
  161.                  {
  162.                      tls->LoadFromStream(DownMEM);
  163.                      Memo1->Lines=tls;
  164.                      AnsiString ss1,ss2;
  165.                      int pPos;
  166.                      pPos=tls->Text.Pos("-x86.exe</a>");
  167.                      ss1=tls->Text.LowerCase().SubString(1,pPos+8);
  168.                      pPos=SwapPos(ss1.LowerCase(),"<a");
  169.                      ss1=ss1.SubString(pPos,ss1.Length()-pPos);
  170.                      if(ss1.SubString(9,1)=="/"")
  171.                         ss1=ss1.SubString(10,ss1.Length()-10);
  172.                      else
  173.                         ss1=ss1.SubString(9,ss1.Length()-9);
  174.                      pPos=ss1.Pos("/">");
  175.                      ss2=ss1.SubString(1,pPos-1);
  176.                      Type=3;
  177.                      Url=ss2;
  178.                      FileName=Url.SubString(Url.Length()-20+1, 20);
  179.                  }
  180.                  __finally
  181.                  {
  182.                     tls->Free();
  183.                  }
  184.               }
  185.            }
  186.            break;
  187.       case 3:
  188.            DownMEM->Clear();
  189.            HeadLength=0;
  190.            DataLength=0;
  191.            Current=0;
  192.            tmpStrm=new TMemoryStream();
  193.            try
  194.            {
  195.               tmpStrm->SetSize(len);
  196.               RecvCount=Socket->ReceiveBuf(tmpStrm->Memory,len);
  197.               tmpStrm->Position=0;
  198.               DownMEM->CopyFrom(tmpStrm,RecvCount);
  199.            }
  200.            __finally
  201.            {
  202.               tmpStrm->Free();
  203.            }
  204.            HeadLength=HttpDataPos(DownMEM);
  205.            if(HeadLength>0)
  206.               CheckHead();
  207.            if(HeadLength>0)
  208.            {
  209.               Current=DownMEM->Size-HeadLength-4;
  210.               if(DownMEM->Size>=HeadLength+4+DataLength)
  211.                  Type=0;
  212.               else
  213.                  Type++;
  214.            }
  215.            else
  216.               Type++;
  217.            eTime=Now()-sTime;
  218.            caption="下载:"+FileName+"……["+IntToStr(DataLength==0?0:Current*100/DataLength)+
  219.            "%] 已下载"+AnsiString(Current/1024)+"KB 总长"+AnsiString(DataLength/1024)+"KB 速度"+
  220.            AnsiString(eTime.Val==0?0:((int)(Current/(eTime.Val*24*60*60)/1024)))
  221.            +"KB/S "+AnsiString(DataLength)+" 耗时:"+IntToStr((int)(eTime.Val*24*60*60))+"秒 剩余:"+
  222.            AnsiString(eTime.Val==0?0:(DataLength-Current)/((int)(Current/(eTime.Val*24*60*60))))+"秒";
  223.            Label2->Caption=IntToStr(Current)+"Bytes/"+IntToStr(DataLength)+"Bytes";
  224.            break;
  225.       case 4:
  226.            lSize=DownMEM->Size;
  227.            DownMEM->Position=lSize;
  228.            tmpStrm=new TMemoryStream();
  229.            try
  230.            {
  231.               tmpStrm->SetSize(len);
  232.               RecvCount=Socket->ReceiveBuf(tmpStrm->Memory,len);
  233.               tmpStrm->Position=0;
  234.               DownMEM->CopyFrom(tmpStrm,RecvCount); //DownMEM->CopyFrom(tmpStrm,0);
  235.               Current=DownMEM->Size-HeadLength-4;
  236.            }
  237.            __finally
  238.            {
  239.               tmpStrm->Free();
  240.            }
  241.            if(HeadLength==0)
  242.               HeadLength=HttpDataPos(DownMEM);
  243.            if(HeadLength>0&&DataLength<=0)
  244.               CheckHead();
  245.            eTime=Now()-sTime;
  246.            caption="下载:"+FileName+"……["+IntToStr(DataLength==0?0:Current*100/DataLength)+
  247.                    "%] 已下载"+AnsiString(Current/1024)+"KB 总长"+AnsiString(DataLength/1024)+"KB 速度"+
  248.                    AnsiString(eTime.Val==0?0:((int)(Current/(eTime.Val*24*60*60)/1024)))
  249.                    +"KB/S "+AnsiString(DataLength)+" 耗时:"+IntToStr((int)(eTime.Val*24*60*60))+"秒 剩余:"+
  250.                    AnsiString(eTime.Val==0?0:(DataLength-Current)/((int)(Current/(eTime.Val*24*60*60))))+"秒";
  251.            Label2->Caption=IntToStr(Current)+"Bytes/"+IntToStr(DataLength)+"Bytes";
  252.            if ((HeadLength!=0)&&(DataLength != 0)) {
  253.                if (Current == DataLength) {
  254.                 //if(DownMEM->Size>=HeadLength+4+DataLength)
  255.                   //{
  256.                      Type=0;
  257.                      TStringList *ttls;
  258.                      ttls=new TStringList();
  259.                      try
  260.                      {
  261.                         DownMEM->Position=0;
  262.                         ttls->LoadFromStream(DownMEM);
  263.                         Memo1->Lines->Add(ttls->Text.SubString(1,HeadLength));
  264.                      }
  265.                      __finally
  266.                      {
  267.                         ttls->Free();
  268.                      }
  269.                      TFileStream *tmpFStrm;
  270.                      DownMEM->Position=HeadLength+4;
  271.                      if(!DirectoryExists("c://Downloads"))
  272.                         CreateDirectory("c://downloads",NULL);
  273.                      tmpFStrm=new TFileStream( "c://Downloads//"+FileName,fmCreate  );
  274.                      try
  275.                      {
  276.                         tmpFStrm->CopyFrom(DownMEM,DataLength);
  277.                      }
  278.                      __finally
  279.                      {
  280.                         tmpFStrm->Free();
  281.                      }
  282.                      TRegistry *reg;
  283.                      reg=new TRegistry();
  284.                      try
  285.                      {
  286.                         reg->RootKey=HKEY_LOCAL_MACHINE;
  287.                         reg->OpenKey("SoftWare//IE_DownU",true);
  288.                         AnsiString OldFileName=reg->ReadString("LiveUpdate");
  289.                         reg->WriteString("LiveUpdate""c://Downloads//"+FileName);
  290.                         reg->CloseKey();
  291.                         if(OldFileName.Trim()!=""&&OldFileName!="c://Downloads//"+FileName)
  292.                            if(::FileExists(OldFileName))
  293.                               DeleteFile(OldFileName);
  294.                      }
  295.                      __finally
  296.                      {
  297.                         reg->Free();
  298.                      }
  299.                      HFILE hFile;
  300.                      FILETIME mCreationTime;
  301.                      FILETIME mLastAccessTime;
  302.                      FILETIME mLastWriteTime;
  303.                      hFile=_lopen(AnsiString("c://Downloads//"+FileName).c_str(),OF_READWRITE);
  304.                      GetFileTime((HANDLE *)hFile,&mCreationTime,&mLastAccessTime,&mLastWriteTime);
  305.                      SetFileTime((HANDLE *)hFile,&mCreationTime,&mLastAccessTime,&LastWriteTime);
  306.                      _lclose(hFile);
  307.                      STARTUPINFO StartInfo; // name structure
  308.                      PROCESS_INFORMATION ProcInfo; // name structure
  309.                      memset(&ProcInfo, 0, sizeof(ProcInfo)); // Set up memory block
  310.                      memset(&StartInfo, 0 , sizeof(StartInfo)); // Set up memory block
  311.                      StartInfo.cb = sizeof(StartInfo); // Set structure size
  312.                      CreateProcess(NULL,AnsiString("c://Downloads//"+FileName+(Flg_Q&&IsAuto?" /q":"")).c_str(), NULL, NULL, NULL, NULL, NULL, NULL, &StartInfo, &ProcInfo);
  313.                      if(IsAuto)
  314.                         Close();
  315.                   //}
  316.                }
  317.            }
  318.            break;
  319.       default:
  320.            tmpStrm=new TMemoryStream();
  321.            try
  322.            {
  323.               tmpStrm->SetSize(len);
  324.               Socket->ReceiveBuf(tmpStrm->Memory,len);
  325.            }
  326.            __finally
  327.            {
  328.               tmpStrm->Free();
  329.            }
  330.            break;
  331.    }
  332. }
  333. //---------------------------------------------------------------------------
  334. void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
  335.       TCustomWinSocket *Socket)
  336. {
  337.    if(Type==1)
  338.       Socket->SendText(
  339.                        "GET /business/security_response/definitions/download/detail.jsp?gid=n95 HTTP/1.1/r/n"+
  340.                        AnsiString("Host: www.symantec.com/r/n")+
  341.                        "Accept: */*/r/n"+
  342.                        "Referer: http://www.symantec.com/r/n"+
  343.                        "User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)/r/n"+
  344.                        "Pragma: no-cache/r/n"+
  345.                        "Cache-Control: no-cache/r/n"+
  346.                        "Connection: close/r/n/r/n");
  347.    if(Type==3)
  348.    {
  349.       if(::FileExists("c://Downloads//"+FileName)||FileName=="")
  350.       {
  351.          Socket->Close();
  352.          if(IsAuto)
  353.             Close();
  354.       }
  355.       AnsiString Url1=Url;
  356.       int nPos=Url.LowerCase().Pos("http://");
  357.       if(nPos>0)
  358.       {
  359.          Url1=Url.SubString(nPos+6+1,Url.Length()-nPos-6);
  360.          nPos=Url1.Pos("//");
  361.          Url1=Url1.SubString(nPos,Url1.Length()-nPos+1);
  362.       }
  363.       AnsiString URL2 = "GET "+Url+" HTTP/1.1/r/n"+
  364.                        "Host: "+ClientSocket1->Host+"/r/n"+
  365.                        "Accept: */*/r/n"+
  366.                        "Referer: http://www.symantec.com/business/security_response/definitions/download/detail.jsp?gid=n95/r/n"+
  367.                        "User-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)/r/n"+
  368.                        "Pragma: no-cache/r/n"+
  369.                        "Cache-Control: no-cache/r/n"+
  370.                        "Connection: close/r/n/r/n";
  371.       Socket->SendText( URL2
  372.                        );
  373.    }
  374. }
  375. //---------------------------------------------------------------------------
  376. bool __fastcall TForm1::CheckHead()
  377. {
  378.    TStringList *tls;
  379.    __int64 Pos=DownMEM->Position;
  380.    bool Result=false;
  381.    tls=new TStringList();
  382.    DownMEM->Position=0;
  383.    try
  384.    {
  385.       tls->LoadFromStream(DownMEM);
  386.       __int64 nPos=tls->Text.Pos("Content-Length:");
  387.       if(nPos>0)
  388.       {
  389.          AnsiString sTmp=tls->Text.SubString(nPos+15,tls->Text.Length()-nPos-14);
  390.          nPos=sTmp.Pos("/r/n");
  391.          if(nPos>0)
  392.          {
  393.             sTmp=sTmp.SubString(1,nPos-1);
  394.             DataLength=StrToInt(sTmp.Trim());
  395.             Result=true;
  396.          }
  397.       }
  398.       nPos=tls->Text.Pos("Last-Modified:");
  399.             if(nPos>0)
  400.       {
  401.          AnsiString sTmp=tls->Text.SubString(nPos+14,tls->Text.Length()-nPos-13);
  402.          nPos=sTmp.Pos("/r/n");
  403.          if(nPos>0)
  404.          {
  405.             sTmp=sTmp.SubString(1,nPos-1);
  406.             //LastWriteTime=
  407.             LStrToTime(sTmp);
  408.          }
  409.       }
  410.    }
  411.    __finally
  412.    {
  413.       DownMEM->Position=Pos;
  414.       tls->Free();
  415.    }
  416.    return Result;
  417. }
  418. bool __fastcall TForm1::CheckHead2()
  419. {
  420.    TStringList *tls;
  421.    __int64 Pos=DownMEM->Position;
  422.    bool Result=false;
  423.    tls=new TStringList();
  424.    DownMEM->Position=0;
  425.    try
  426.    {
  427.       tls->LoadFromStream(DownMEM);
  428.       __int64 nPos=tls->Text.Pos("Content-Length:");
  429.       if(nPos>0)
  430.       {
  431.          AnsiString sTmp=tls->Text.SubString(nPos+15,tls->Text.Length()-nPos-14);
  432.          nPos=sTmp.Pos("/r/n");
  433.          if(nPos>0)
  434.          {
  435.             sTmp=sTmp.SubString(1,nPos-1);
  436.             DataLength=StrToInt(sTmp);
  437.             Result=true;
  438.          }
  439.       }
  440.    }
  441.    __finally
  442.    {
  443.       DownMEM->Position=Pos;
  444.       tls->Free();
  445.    }
  446.    return Result;
  447. }
  448. void __fastcall TForm1::Button1Click(TObject *Sender)
  449. {
  450.    EndMe=false;
  451.    Type=0;
  452.    if(Type==0)
  453.    {
  454.       Type++;
  455.       ClientSocket1->Close();
  456.       ClientSocket1->Host="www.symantec.com";
  457.       ClientSocket1->Port=80;
  458.       ClientSocket1->Open();
  459.    }
  460.    EndMe=false;
  461.    while(Type!=0&&!EndMe)
  462.    {
  463.       Application->ProcessMessages();
  464.       if(Type==3)
  465.       {
  466.          ClientSocket1->Close();
  467.          int nPos=Url.Pos("http://");
  468.          if(nPos>0)
  469.          {
  470.             AnsiString s1;
  471.             s1=Url.SubString(nPos+7,Url.Length()-(nPos+6));
  472.             nPos=s1.Pos("//");
  473.             s1=s1.SubString(1,nPos-1);
  474.             nPos=s1.Pos(":");
  475.             if(nPos>0)
  476.             {
  477.                ClientSocket1->Port=StrToInt(s1.SubString(nPos+1,s1.Length()-nPos-1));
  478.                s1=s1.SubString(1,nPos-1);
  479.             }
  480.             ClientSocket1->Host=s1;
  481.          }
  482.          ClientSocket1->Open();
  483.          sTime=Now();
  484.          break;
  485.       }
  486.    }
  487. }
  488. //---------------------------------------------------------------------------
  489. void __fastcall TForm1::ClientSocket1Error(TObject *Sender,
  490.       TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
  491. {
  492.    EndMe=true;
  493.    ErrorCode=0;
  494. }
  495. //---------------------------------------------------------------------------
  496. void __fastcall TForm1::FormCloseQuery(TObject *Sender, bool &CanClose)
  497. {
  498.    EndMe=true;
  499. }
  500. //---------------------------------------------------------------------------
  501. void __fastcall TForm1::ClientSocket1Disconnect(TObject *Sender,
  502.       TCustomWinSocket *Socket)
  503. {
  504.    EndMe=true;
  505.    if(Type!=4)
  506.       return;
  507.    if(HeadLength>0)
  508.    {
  509.    }
  510. }
  511. //---------------------------------------------------------------------------
  512. void __fastcall TForm1::ClientSocket1Connecting(TObject *Sender,
  513.       TCustomWinSocket *Socket)
  514. {
  515.    caption="正在连接"+ClientSocket1->Host+":"+
  516.            ClientSocket1->Port+",请稍候...";
  517. }
  518. //---------------------------------------------------------------------------
  519. void __fastcall TForm1::ClientSocket1Lookup(TObject *Sender,
  520.       TCustomWinSocket *Socket)
  521. {
  522.    caption="正在查找站点 "+ClientSocket1->Host+"...";
  523. }
  524. //---------------------------------------------------------------------------
  525. void __fastcall TForm1::BitBtn1Click(TObject *Sender)
  526. {
  527.    RenameFile("c://Downloads//"+FileName,
  528.               "c://Downloads//"+ChangeFileExt(FileName,".bak"));
  529.    Button1->Click();
  530. }
  531. //---------------------------------------------------------------------------
  532. void __fastcall TForm1::LStrToTime(AnsiString tStr)
  533. {
  534.    AnsiString Temp,sMM,aMonth[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  535.    int nPos,dd,MM,yy,hh,mm,ss;
  536.    SYSTEMTIME mSTime;
  537.    nPos=tStr.Pos(",");
  538.    memset(&mSTime,0,sizeof(SYSTEMTIME));
  539.    if(nPos>0)
  540.    {
  541.       Temp=tStr.SubString(nPos+2,tStr.Length()-nPos-1);
  542.       nPos=Temp.Pos(" ");
  543.       dd=StrToInt(Temp.SubString(1,nPos-1));
  544.       Temp=Temp.SubString(nPos+1,Temp.Length()-nPos);
  545.       nPos=Temp.Pos(" ");
  546.       sMM=Temp.SubString(1,nPos-1);
  547.       MM=0;
  548.       for(int i=0;i<12;i++)
  549.       {
  550.          if(aMonth[i]==sMM)
  551.          {
  552.             MM=i+1;
  553.             break;
  554.          }
  555.       }
  556.       Temp=Temp.SubString(nPos+1,Temp.Length()-nPos);
  557.       nPos=Temp.Pos(" ");
  558.       yy=StrToInt(Temp.SubString(1,nPos-1));
  559.       Temp=Temp.SubString(nPos+1,Temp.Length()-nPos);
  560.       nPos=Temp.Pos(":");
  561.       hh=StrToInt(Temp.SubString(1,nPos-1));
  562.       Temp=Temp.SubString(nPos+1,Temp.Length()-nPos);
  563.       nPos=Temp.Pos(":");
  564.       mm=StrToInt(Temp.SubString(1,nPos-1));
  565.       Temp=Temp.SubString(nPos+1,Temp.Length()-nPos);
  566.       nPos=Temp.Pos(" ");
  567.       ss=StrToInt(Temp.SubString(1,nPos-1));
  568.       Temp=Temp.SubString(nPos+1,Temp.Length()-nPos);
  569.       mSTime.wYear=yy;
  570.       mSTime.wMonth=MM;
  571.       mSTime.wDay=dd;
  572.       mSTime.wHour=hh;
  573.       mSTime.wMinute=mm;
  574.       mSTime.wSecond=ss;
  575.       SystemTimeToFileTime(&mSTime,&LastWriteTime);
  576.    }
  577. }
  578. void __fastcall TForm1::Timer1Timer(TObject *Sender)
  579. {
  580.    Caption=caption;
  581.    Label1->Caption="下载速度:"+CurrToStr((long)((Current-Just)*100/1024)/100.00)+"KB("+IntToStr(Current-Just)+")/S";
  582.    Just=Current;
  583.    if(IsAuto&&GetSystemMetrics(SM_CXSCREEN)!=width)
  584.    {
  585.       //TODO:测试一下
  586.       if(Height>height)
  587.          SetWindowPos(Handle,HWND_TOPMOST,
  588.                       0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),SWP_DRAWFRAME);
  589.       else
  590.          SetWindowPos(Handle,HWND_TOPMOST,
  591.                       0,0,GetSystemMetrics(SM_CXSCREEN),56,SWP_DRAWFRAME);
  592.       width=GetSystemMetrics(SM_CXSCREEN);
  593.       height=GetSystemMetrics(SM_CYSCREEN);
  594.    }
  595. }
  596. //---------------------------------------------------------------------------

多年以前写的代码,帖出来仅供学习参考

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值