Implement drag and drop of files in vb.net

Source: Internet
Author: User
Tags filter implement integer message queue tostring
This article describes how to implement a file that accepts drag-and-drop in vb.net, which automatically gets dragged and dropped files when dragged and dropped from explorer to the application. The example in this article is a vb.net instance program that accepts the contents of a drag-and-drop file display file.
Introduction:

For text-formatted files, we can drag them directly into Notepad to see the content, and the various types of pictures, dragged to Photoshop, can be edited directly. How do we develop programs in vb.net to achieve the above results?

Ideas:

We know that every Windows application has a message queue, the body of the program accepts the message from the system, and then sends it out (to a form, or to a control), and the recipient has the appropriate program to process the message. In the. NET form, the program does not translate the messages by default, which means that our class is the message pump that does not join the application. Can we add our form class to the application's message pump? OK!

In. NET, any class that implements the IMessageFilter interface can be added to the application's message pump to filter it out or perform other actions before the message is dispatched to the control or form. Using the Addmessagefilter method in the application class, you can add a message filter to the application's message pump.

So we call Application.addmessagefilter (Me) when the program loads. However, by default, a form or control cannot accept drag-and-drop files, and we call a WIN32 API DragAcceptFiles, which allows you to set whether the corresponding controls can accept drag-and-drop files. You can then use the Dragqueryfile query to drag and drop the file list, which is the specific path and file name of the file to drag and drop.



Code:

Imports System.Runtime.InteropServices

Public Class Form1

Inherits System.Windows.Forms.Form

Implements IMessageFilter

' API declaration

Const wm_dropfiles = &h233 ' drag-and-drop file message



<dllimport ("Shell32.dll") > Public Shared Sub dragfinish (ByVal hdrop as Integer)

End Sub

<dllimport ("Shell32.dll") > Public Shared Sub dragacceptfiles (ByVal hwnd as Integer, ByVal faccept as Boolean)

End Sub

<dllimport ("Shell32.dll") > Public Shared Function dragqueryfile (ByVal hdrop As Integer, ByVal UINT as Integer, ByVal LpStr as System.Text.StringBuilder, ByVal ch As Integer) As Integer

End Function



Private Sub Form1_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load

Application.addmessagefilter (Me)

DragAcceptFiles (TextBox1.Handle.ToInt32, True)

End Sub



Function Prefiltermessage (ByRef m as message) as Boolean Implements imessagefilter.prefiltermessage

If m.msg = Wm_dropfiles Then

' Set the drag-and-drop action

Dim Nfiles as Int16

Nfiles = Dragqueryfile (M.wparam.toint32,-1, nothing, 0)

Dim I as Int16

Dim SB as New System.Text.StringBuilder (256)

Dim Sfirstfilename as String ' record first filename



Textbox1.clear ()

For i = 0 to Nfiles-1

Dragqueryfile (M.wparam.toint32, I, SB, 256)

If i = 0 Then sfirstfilename = sb. Tostring

Textbox1.appendtext (Controlchars.crlf & sb.) ToString)

Next

Dragfinish (M.wparam.toint32) ' drag and drop complete



' Show file contents

Dim FS as New System.IO.FileStream (Sfirstfilename, IO. FileMode.Open)

Dim SR as New System.IO.StreamReader (FS, System.Text.Encoding.GetEncoding ("gb2312"))

Textbox1.appendtext (Controlchars.crlf & Sr. ReadToEnd (). ToString)

Fs. Close ()

Sr. Close ()



End If

Return False

End Function

Protected Overloads Overrides Sub Dispose (ByVal disposing as Boolean)

If disposing Then

If not (components are nothing) Then

Components. Dispose ()

End If

End If

Application.removemessagefilter (Me)

DragAcceptFiles (TextBox1.Handle.ToInt32, False)

Mybase.dispose (disposing)



End Sub


Note: When the drop ends, the call Dragfinish frees the memory.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.