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

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 airon2002 的 blog

用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/
'
'============================================

本文关键:VB6,文件下载,异步,OCX,ActiveX
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top