![]() |
![]() |
Private Sub Form_Load() cboURL.ListIndex = 0 End Sub |
Private Sub cmdBegin_Click() With DL .FileURL = cboURL.Text ’設置bkDLControl的下載地址 .SaveFilePath = App.Path ’設置下載后的保存路徑,這里設置為當前程序所在文件夾 LogItem "請求下載" & cboURL.Text ’在lstOut中添加下載狀態說明,這里使用了一個自定義過程,該過程的代碼將在下面第四步說明 .BeginDownload ’發出開始下載命令 End With cmdCancel.Enabled = True ’設置cmdCancel屬性為True,使下載過程中可以中止下載 End Sub Private Sub cmdCancel_Click() DL.CancelDownload ’發出取消下載命令 End Sub |
Private Sub DL_DLBeginDownload() LogItem "開始下載從" & DL.FileURL End Sub Private Sub DL_DLCanceled() LogItem "取消下載" End Sub |
Private Sub DL_DLComplete(Bytes As Long) ’下載完成的事件 cmdCancel.Enabled = False If Bytes > 0& Then ’如果下載的不是零字節,則顯示相關信息 LogItem "完成" & SizeString(Bytes) & "下載并保存到" & DL.SaveFileName ’下面添加實現文件更新的代碼 …… Else LogItem "下載失敗" End If End Sub |
Private Sub DL_DLConnected(ConnAddr As String) LogItem "連接到 " & ConnAddr ’當連接成功時返回IP地址 End Sub Private Sub DL_DLError(E As bkDLError, Error As String) Dim strErrType As String ’下載錯誤時的事件 Select Case E Case bkDLEUnavailable strErrType = "不可下載文件" Case bkDLERedirect strErrType = "重定向" Case bkDLEZeroLength strErrType = "沒有字節返回" Case bkDLESaveError strErrType = "文件保存錯誤" Case bkDLEUnknown strErrType = "不明錯誤" End Select LogItem "錯誤 - " & strErrType & ": " & Error End Sub |
Private Sub DL_DLFileSize(Bytes As Long) ’當連接后返回文件的大小,單位為字節,我們通過自定義的函數對單位進行必要轉換 LogItem "文件大小為" & SizeString(Bytes) & " (" & CStr(Bytes) & " bytes)" End Sub Private Sub DL_DLMIMEType(MIMEType As String) LogItem "MIME類型是 " & MIMEType End Sub Private Sub DL_DLProgress(Percent As Single, BytesRead As Long, TotalBytes As Long) ’下載過程中的事件,返回下載百分比和已下載字節數。我們通過下面一行代碼在lstOut中顯示 lblProg.Caption = Format(Percent, "0%") & " of " & SizeString(TotalBytes) End Sub Private Sub DL_DLRedirect(ConnAddr As String) ’返回地址如果重定向 LogItem Index, "重定向到" & ConnAddr End Sub |
Private Sub LogItem(strItem As String) With lstOut .AddItem "> " & strItem If .NewIndex > .TopIndex + 17 Then .TopIndex = .NewIndex - 16 End If End With End Sub |
Private Function SizeString(lBytes As Long) As String If lBytes < &H400& Then SizeString = CStr(lBytes) & "b" ElseIf lBytes < &H100000 Then SizeString = CStr(lBytes \ 1024) & "k" ElseIf lBytes < &H20000000 Then SizeString = Replace$(Format$((lBytes \ 1024) / 1024, "0.0"), ".0", vbNullString) & "M" Else SizeString = Replace$(Format$((lBytes \ (1024 ^ 2)) / 1024, "#,##0.0"), ".0", vbNullString) & "G" End If End Function |
![]() |
原文轉自:http://www.anti-gravitydesign.com