首页 | 社区 | 博客 | 招聘 | 文章 | 新闻 | 下载 | 读书 | 代码
亲,您未登录哦! 登录 | 注册

用vb6的ActiveX控件实现异步下载

打印文章

分享到:
序:笔者(airon,softWorker)注意到,在VB6中,要实现文件下载,一般用和方法都是使用第三方控件,比如IE控件呀,winscok呀,但在本文中,不用添加任何控件,也不引用任何object,就可实现文件下载,而且程序不支持文件下载进度,捕获下载错误,激活下载完成事件等。

具体方法:
1.新建一VB6工程(默认有一个Form1窗体)
2.选择工程菜单的“添加用户控件”来添加一个用户控件。
3.更改Activex用户控件的名称,更改为 Downloader  (此项可省) 。
4.输入代码:(在用户控件的代码窗口中)

Option Explicit
Event DownloadProgress(CurBytes As Long, MaxBytes As Long, SaveFile As String)
Event DownloadError(SaveFile As String)
Event DownloadComplete(MaxBytes As Long, SaveFile As String)
'Public downStat As Boolean


Public Function CancelAsyncRead() As Boolean
    On Error Resume Next
    UserControl.CancelAsyncRead
End Function
'Private Sub Timer1_Timer()
'    If Not downStat Then
'        Timer1.Enabled = False
'        Exit Sub
'    End If
'    Static Cs As Integer
'    If Cs > 2 Then Cs = 0
'    UserControl.Picture = P1(Cs).Picture
'    Cs = Cs + 1
'    DoEvents
'End Sub
Private Sub UserControl_AsyncReadComplete(AsyncProp As AsyncProperty)
    On Error Resume Next
    Dim f() As Byte, fn As Long
    If AsyncProp.BytesMax <> 0 Then
        fn = FreeFile
        f = AsyncProp.Value
        Open AsyncProp.PropertyName For Binary Access Write As #fn
        Put #fn, , f
        Close #fn
    Else
        RaiseEvent DownloadError(AsyncProp.PropertyName)
    End If
    RaiseEvent DownloadComplete(CLng(AsyncProp.BytesMax), AsyncProp.PropertyName)
    downStat = False
End Sub
Private Sub UserControl_AsyncReadProgress(AsyncProp As AsyncProperty)
    On Error Resume Next
    If AsyncProp.BytesMax <> 0 Then
        RaiseEvent DownloadProgress(CLng(AsyncProp.BytesRead), CLng(AsyncProp.BytesMax), AsyncProp.PropertyName)
        downStat = True: Timer1.Enabled = True
    End If
End Sub
'Private Sub UserControl_Resize()
'    SizeIt
'End Sub
Public Sub BeginDownload(url As String, SaveFile As String)
    On Error GoTo ErrorBeginDownload
    downStat = True
    UserControl.AsyncRead url, vbAsyncTypeByteArray, SaveFile, vbAsyncReadForceUpdate
    Timer1.Enabled = True
    Exit Sub
ErrorBeginDownload:
    downStat = False
    MsgBox Err & "开始下载数据失败!" _
    & vbCrLf & vbCrLf & "错误:" & Err.Description, vbCritical, "错误"
End Sub
'Public Sub SizeIt()
'    On Error GoTo ErrorSizeIt
'    With UserControl
'        .Width = ScaleX(32, vbPixels, vbTwips)
'        .Height = ScaleY(32, vbPixels, vbTwips)
'    End With
'    Exit Sub
'ErrorSizeIt:
'End Sub
'Public Sub kill()
'    downStat = False
'    Dim m As AsyncProperty
'    MsgBox m.Value
'End Sub

-----------------------------------------------------
程序说明:

本文采取VB6中OCX中的 异步获取方法来下载文件。
用到 AsyncRead(异步读取)

文中带注解的部分为下载界面控制,在下载时,会有像 FlashGet一样的有动画图标在动,要添加此功能,请在用户控件上添加三个image,(image上要带图片)


-----------------------------------------------------

4.关闭用户控件的代码与设置窗口,回到 Form1
5.这时你会看到在左边的工具栏下多了一用户控件,把它添加到窗体上。命名为 Downloader1
6.在窗体上添加一 command 控钮,命名为 Command1
7.在窗体 Form1的代码窗口输入代码:

Option Explicit
'============================================
'
'   程序编写, airon,softWoker  2004-02-20
'   vborg@sohu.com  airon888@hotmail.com
'   Http://www.eyes3.net/
'
'============================================
Private Sub Command1_Click()
    Downloader1.BeginDownload url, SaveFile
    '请把 URL 替代为 Http://文件路径
    '请把 savefile 替代为下载到本地文件的路径与名称。
End Sub
    
Private Sub Downloader1_DownloadComplete(MaxBytes As Long, SaveFile As String)
    MsgBox "文件下载完成,保存文件名为:" & SaveFile, vbInformation Or vbOKOnly, "提示:"
End Sub

Private Sub Downloader1_DownloadError(SaveFile As String)
    MsgBox "下载发生错误!", vbExclamation Or vbOKOnly, "错误:"
End Sub

Private Sub Downloader1_DownloadProgress(CurBytes As Long, MaxBytes As Long, SaveFile As String)
    '在这里添加进度第的代码
End Sub

最后申明:  本文为 airon,softWorker 原作,未经本人同意,不作出版,(CSDN 除外),但可以转载,转载时请注明作者。  如需在源程序的请mail联系我, vbprg@sohu.com 。希望本文能给广大读者带来帮助。

本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )

编程爱好者论坛

本栏最新文章