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
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这是一个下载的Activex OCX,不用任何控件就可从HTTP上下载文件,(一定要写在VB 的Activex OCX中!), 支持进度条。 但出错后就不能中止异步,或异步执行不知如何中止,望高手指点!!!!!!!!!!!!!!!!!

解决方案 »

  1.   

    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
    加入:此代码无用,只是在下载时显示图片,好像其它下载软件一样,下地时有动画效果·
      

  2.   

    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
    加入:此代码无用,只是在下载时显示图片,好像其它下载软件一样,下地时有动画效果·