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.
<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
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.