how to:從資源管理器中拖放文件到控件
當(dāng)然,這里所謂的文件是指完整的文件名稱,至于文件的內(nèi)容,須按實(shí)際情況進(jìn)一步的操作。
我這里的控件為一個(gè)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
 
重寫這個(gè)how to,主要的是看到有人用api實(shí)現(xiàn),代碼如下:
 
    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