序:筆者(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 。希望本文能給廣大讀者帶來幫助。
|