欢迎光临
我们一直在努力

HOW TO:从资源管理器中拖放文件到控件-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

how to:从资源管理器中拖放文件到控件

当然,这里所谓的文件是指完整的文件名称,至于文件的内容,须按实际情况进一步的操作。

我这里的控件为一个listbox。代码如下:

    private sub form1_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load

        me.listbox1.allowdrop = true

    end sub

 

    private sub listbox1_dragenter(byval sender as object, byval e as system.windows.forms.drageventargs) handles listbox1.dragenter

        dim o as object = e.data.getdata(dataformats.filedrop)

        if not o is nothing then

            e.effect = dragdropeffects.copy

        end if

    end sub

 

    private sub listbox1_dragdrop(byval sender as object, byval e as system.windows.forms.drageventargs) handles listbox1.dragdrop

        dim filenames as string() = ctype(e.data.getdata(dataformats.filedrop), string())

        me.listbox1.items.addrange(filenames)

end sub

 

重写这个how to,主要的是看到有人用api实现,代码如下:

 

    private const wm_dropfiles as integer = 563

    private declare function dragacceptfiles lib “shell32.dll” (byval hwnd as intptr, byval accept as boolean) as long

    private declare function dragqueryfile lib “shell32.dll” (byval hdrop as intptr, byval file as integer, byval filename as system.text.stringbuilder, byval size as int32) as int32

    private declare sub dragfinish lib “shell32.dll” (byval hdrop as intptr)

 

    protected overrides sub wndproc(byref m as system.windows.forms.message)

        if m.msg = wm_dropfiles then

            dim inumoffiles as int32 = dragqueryfile(m.wparam, &hffffffff, nothing, 0)

            dim ipnt as int32

            for ipnt = 0 to inumoffiles – 1

                dim sb as new system.text.stringbuilder(256)

                dim iret as int32 = dragqueryfile(m.wparam, ipnt, sb, sb.capacity)

                listbox1.items.add(sb.tostring)

            next

            dragfinish(m.wparam)

        else

            mybase.wndproc(m)

        end if

    end sub

 

    private sub form1_load(byval sender as object, byval e as system.eventargs) handles mybase.load

        dragacceptfiles(listbox1.handle, true)

    end sub

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » HOW TO:从资源管理器中拖放文件到控件-.NET教程,Asp.Net开发
分享到: 更多 (0)