HOW TO:从资源管理器中拖放文件到控件

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值