具体思路方法:
1.新建VB6工程(默认有个Form1窗体)
2.选择工程菜单“添加用户Control控件”来添加个用户Control控件
3.更改Activex用户Control控件名称更改为 Downloader (此项可省)
4.输入代码:(在用户Control控件代码窗口中)
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样有动画图标在动要添加此功能请在用户Control控件上添加 3个image(image上要带图片)
-----------------------------------------------------
4.关闭用户Control控件代码和设置窗口回到 Form1
5.这时你会看到在左边工具栏下多了用户Control控件把它添加到窗体上命名为 Downloader1
6.在窗体上添加 command 控钮命名为 Command1
7.在窗体 Form1代码窗口输入代码:
Option Explicit
'
'
' 编写 airon,softWoker 2004-02-20
' [email protected] [email protected]
' 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联系我 [email protected] 希望本文能给广大读者带来帮助
最新评论