HOW TO:從資源管理員中拖放檔案到控制項

來源:互聯網
上載者:User
控制項
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



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.