源码分享网络编程

VB 纯源码无控件使用 URLDownloadToFile 实现文件下载(带进度与下载速度显示)

‘文件下载类DEMO

‘输入下载地址,目标路径,即开始下载.
‘下载进度事件中可得到下载速度,进度等详细信息.

‘BY 嗷嗷叫的老马

纯源码实现,无注册控件,

源码来自 newxing 下载站,侵删

Option Explicit
'文件下载类DEMO
'
'输入下载地址,目标路径,即开始下载.
'下载进度事件中可得到下载速度,进度等详细信息.
'
'BY 嗷嗷叫的老马
'download by http://www.NewXing.com

Dim WithEvents oDownload As CFileDownload

Private Sub oDownload_OnProgress(ByVal lProgress As Long, ByVal lMaxProgress As Long, ByVal lSpeed As Long, ByVal lStatusCode As Long, ByVal sStatusText As String)
    Dim I As Single
    
    If lProgress = 0 Or lMaxProgress = 0 Then Exit Sub
    
    I = lProgress / lMaxProgress * 30
    labProgress.Caption = String(I, "■")
    Me.Caption = "文件下载DEMO(" & Format(I, "0.00") & "%,速度 = " & lSpeed & "KB/S)"
End Sub

Private Sub cmdDownload_Click()
    '开始下载
    '
    '注意StartDownloading过程是阻塞的.
    
    If oDownload.StartDownloading(txtURL.Text, AddStrToStr(App.Path, "/") & GetFileNameInPath(txtURL.Text)) Then
        MsgBox "下载成功!", vbOKOnly Or vbInformation
    Else
        MsgBox "下载失败!", vbOKOnly Or vbInformation
    End If
End Sub

Private Sub cmdCancel_Click()
    '取消下载.
    
    Call oDownload.AbortDownloading
End Sub

Private Sub Form_Load()
    Set oDownload = New CFileDownload
    
    '    txtURL.Text = "http://www.m5home.com/soft/vb_link.rar"                      'VB函数添加大师
    txtURL.Text = "https://dldir1.qq.com/WechatWebDev/release/be1ec64cf6184b0fa64091919793f068/wechat_devtools_1.06.2405010_win32_x64.exe" '微信开发者工具
    labProgress.Caption = ""
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Call oDownload.AbortDownloading
    Set oDownload = Nothing
End Sub

立即下载

Views: 101

Hi, I’m 邓伟

本来无一物,何处惹尘埃

发表回复