VB Inte控件 仿FlagshGet下载软件

来源:互联网 发布:创新软件股票行情 编辑:程序博客网 时间:2024/06/05 08:12

原文地址:http://hi.baidu.com/qqool/blog/item/2bce35fa0617301aa9d311f8.html

 

'添加inet控件和进度条控件:microsoft Intenet Transfer control和microsoft Windows common control选上
Dim FileSize As Double
Dim Buffers() As Byte

Private Sub Command1_Click()
Command4.Enabled = False
Command1.Enabled = False
Command2.Enabled = True
Label6.Caption = ""
Label7.Caption = ""
Label10.Caption = ""
Label11.Caption = ""
Inet1.Cancel
Inet1.Protocol = icHTTP '设置传输协议为HTTP
Inet1.Execute Text1(0).Text, , "GET"    '向服务器发送Get命令,获得被下载文件的长度等信息
End Sub

Private Sub Command2_Click()
Command2.Enabled = False
Command3.Enabled = True
Inet1.Cancel
Label3.Caption = "下载过程被用户中止!"
Close #1
Command1.Enabled = True
End Sub

Private Sub Command3_Click()
Form_Load
End Sub

Private Sub Command4_Click()
With CommonDialog1
     .DialogTitle = "保存文件"
     .FileName = Text1(1).Text
     .InitDir = "d:/"
     Dim strPath As String
     strPath = Mid(Trim(Text1(1).Text), InStr(1, Trim(Text1(1).Text), ".") + 1, Len(Trim(Text1(1).Text)) - InStr(1, Trim(Text1(1).Text), ".") + 1)
     .Filter = strPath & "文件[*." & strPath & "]|*." & strPath '"所有文件[*.*]|*.*|MP3文件[*.mp3]|*.mp3|rm文件[*.rm]|*.rm|avi文件[*.avi]|*.avi|mpeg文件[*.mpeg]|*.mpeg|rar文件[*.rar]|*.rar"
     .DefaultExt = Mid(Trim(Text1(1).Text), InStr(1, Trim(Text1(1)), ".") + 1, Len(Text1(1).Text) - InStr(1, Trim(Text1(1)), ".") + 1)
     .Action = 2 '等价于ShowSave
End With
Text1(2).Text = ""
Exit Sub
End Sub

Private Sub Form_Load()
Label3.Caption = ""
Label6.Caption = ""
Label7.Caption = ""
Label10.Caption = ""
Label11.Caption = ""
ProgressBar1.Value = 0
Text1(0).Text = ""
Text1(1).Text = ""
Command1.Enabled = False
Command2.Enabled = False
Command3.Enabled = False
Command4.Enabled = False
Timer1.Enabled = False
End Sub

Private Sub Inet1_StateChanged(ByVal State As Integer)
Command1.Enabled = False
Select Case State
   Case 3 'state为3时,表示Inet控件正在服务
     Label3.Caption = "正在与主机连接..."
   Case 8 '表示已与服务器连接成功
     Label3.Caption = "已成功地接收到主机的响应"
     FileSize = Inet1.GetHeader("Content-Length")
     Label7.Caption = Inet1.GetHeader("Content-Type")
     Label6.Caption = FileSize
   Case 12 '表示读取的数据已到达本机
     Timer1.Enabled = True
     Dim vtData As Variant
     Dim bDone As Boolean
     bDone = False
     Open CommonDialog1.FileName For Binary Access Write As #1
     Do While Not bDone
       Label3.Caption = "正在下载..."
       vtData = Inet1.GetChunk(1024, icByteArray) '从本机的缓冲区读取数据
       Buffers() = vtData '保存到写入文件的临时变量
       Put #1, , Buffers()
       If Len(vtData) = 0 Then '判断是非是否下载完毕
         bDone = True
       End If
       Label10.Caption = LOF(1) '在label10中显示已下载的字节数
       Label11.Caption = Label6.Caption - Label10.Caption '剩余字节数
       ProgressBar1.Value = (LOF(1) * ProgressBar1.Max) / FileSize
       DoEvents
     Loop
     Close #1 '下完
     Label3.Caption = "下载完毕!"
     Command2.Enabled = False
     Command3.Enabled = True
   End Select
End Sub

Private Sub Text1_Change(Index As Integer)
For Index = 0 To 1
   If Text1(Index) = "" Then
     Command1.Enabled = False
     Command4.Enabled = False
   Else
     Command1.Enabled = True
     Command4.Enabled = True
   End If
Next
End Sub

Private Sub Timer1_Timer()
Label14.Caption = "已下载 " & Format(Val(Label10.Caption) / FileSize * 100, "####.##") & "%"
End Sub

 

 

原创粉丝点击