首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

vb怎么使用UrlDownloadtoFile下载文件 同时显示 进度条 大小 下载速度

2012-03-31 
vb如何使用UrlDownloadtoFile下载文件 同时显示 进度条 大小 下载速度?VB codePrivate Declare Function U

vb如何使用UrlDownloadtoFile下载文件 同时显示 进度条 大小 下载速度?

VB code
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As LongSub Command1_Click()   '后台下载r = URLDownloadToFile(0, "http://www.54nanren.com.cn/en-us/music/Music_Upload/mianhuatang.wma", "c:\8.Wma", 0, 0)If r = 0 ThenMsgBox "下载完毕"ElseMsgBox "下载失败"End IfEnd Sub'上面的能否得到进程条, 文件大小, 下载速度?'下面的能否不显示下载对话框.只要得到进程条, 文件大小, 下载速度 等信息呢?Private Sub Command2_Click()  'API系统对话框On Error GoTo ERR_OUTDoFileDownload StrConv("http://www.54nanren.com.cn/en-us/music/Music_Upload/mianhuatang.wma", vbUnicode)Exit SubERR_OUT:MsgBox "There is an error accoured"Exit SubEnd Sub






[解决办法]
http://www.mvps.org/emorcillo/download/vb6/tl_ole.zip
引用这个 olelib.tlb
,直接让你的类(窗体)继承 IBindStatusCallback,该对象的指针就可以作为 URLDownloadToFile 的最后一个参数。
VB code
'FormOption ExplicitImplements IBindStatusCallbackSub Command1_Click()    Dim r As Long    r = URLDownloadToFileW(Me, "http://www.54nanren.com.cn/en-us/music/Music_Upload/mianhuatang.wma", "c:\8.Wma", 0, Me)    If r = 0 Then        MsgBox "下载完毕"    Else        MsgBox "下载失败"    End IfEnd SubPrivate Sub IBindStatusCallback_GetBindInfo(grfBINDF As olelib.BINDF, pbindinfo As olelib.BINDINFO)End SubPrivate Function IBindStatusCallback_GetPriority() As LongEnd FunctionPrivate Sub IBindStatusCallback_OnDataAvailable(ByVal grfBSCF As olelib.BSCF, ByVal dwSize As Long, pformatetc As olelib.FORMATETC, pStgmed As olelib.STGMEDIUM)End SubPrivate Sub IBindStatusCallback_OnLowResource(ByVal reserved As Long)End SubPrivate Sub IBindStatusCallback_OnObjectAvailable(riid As olelib.UUID, ByVal pUnk As stdole.IUnknown)End SubPrivate Sub IBindStatusCallback_OnProgress(ByVal ulProgress As Long, ByVal ulProgressMax As Long, ByVal ulStatusCode As olelib.BINDSTATUS, ByVal szStatusText As Long)    Dim sStatus As String    Dim sText As String    Dim bUsePercent As Boolean    Select Case ulStatusCode        Case BINDSTATUS_FINDINGRESOURCE            sStatus = "Finding resource"        Case BINDSTATUS_CONNECTING            sStatus = "Connecting"        Case BINDSTATUS_REDIRECTING            sStatus = "Redirecting"        Case BINDSTATUS_BEGINDOWNLOADDATA            sStatus = "Begin download data"            bUsePercent = True        Case BINDSTATUS_DOWNLOADINGDATA            sStatus = "Downloading data"            bUsePercent = True        Case BINDSTATUS_ENDDOWNLOADDATA            sStatus = "End download data"            bUsePercent = True        Case BINDSTATUS_USINGCACHEDCOPY            sStatus = "Using cached copy"        Case BINDSTATUS_SENDINGREQUEST            sStatus = "Sending request"        Case BINDSTATUS_CLASSIDAVAILABLE            sStatus = "Object CLSID"        Case BINDSTATUS_MIMETYPEAVAILABLE, BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE            sStatus = "Mime type available"        Case BINDSTATUS_CACHEFILENAMEAVAILABLE            sStatus = "Cache filename"        Case BINDSTATUS_BEGINSYNCOPERATION            sStatus = "Begin Sync operation"        Case BINDSTATUS_ENDSYNCOPERATION            sStatus = "End Sync operation"        Case BINDSTATUS_BEGINUPLOADDATA            sStatus = "Begin uploading data"        Case BINDSTATUS_UPLOADINGDATA            sStatus = "Uploading data"        Case BINDSTATUS_ENDUPLOADDATA            sStatus = "End uploading data"        Case BINDSTATUS_PROTOCOLCLASSID            sStatus = "Protocol CLSID"        Case BINDSTATUS_ENCODING            sStatus = "Encoding"        Case BINDSTATUS_COOKIE_SENT            sStatus = "Cookie sent"        Case BINDSTATUS_P3P_HEADER            sStatus = "P3P Header"        Case BINDSTATUS_POLICY_HREF            sStatus = "Policy HREF"        Case BINDSTATUS_DECODING            sStatus = "Decoding"        Case BINDSTATUS_COOKIE_STATE_ACCEPT            sStatus = "Cookie Accept"        Case BINDSTATUS_COOKIE_STATE_LEASH            sStatus = "Cookie state leash"        Case BINDSTATUS_PROXYDETECTING            sStatus = "Proxy detecting"        Case BINDSTATUS_ACCEPTRANGES            sStatus = "Accept ranges"        Case Else            sStatus = "ID:" & CStr(ulStatusCode)    End Select    If bUsePercent And (ulProgressMax <> 0) Then        sStatus = FormatPercent(ulProgress / ulProgressMax, 0, vbTrue)    ElseIf szStatusText <> 0 Then        sStatus = sStatus & ": " & SysAllocString(szStatusText)    End If    Label1 = sStatusEnd Sub 

热点排行