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 |