Drag and drop files in VB. NET

Source: Internet
Author: User
Tags message queue
This article describes how to implement drag-and-drop files in VB. NET, that is, to automatically obtain the drag-and-drop files when being dragged from the resource manager to the application. The example in this article is a VB. NET instance program that accepts drag-and-drop files to display the file content.
Introduction:
For text files, you can drag them to notepad to view the content. For various types of images, drag them to Photoshop to edit them directly. How can we achieve the above results in programs developed in VB. NET?
Ideas:
We know that every Windows application has a message queue. The main body of the program receives messages from the system and then delivers the messages (to a form or control ), the receiver has a program to process messages. In. NET Form, the program does not translate these messages by default, that is, our Class is the message pump that does not join the application by default. Can we add our Form Class to the message pump of the application? Yes!
In. NET, any class that implements the IMessageFilter interface can be added to the message pump of the application to filter messages or perform other operations before they are scheduled to the control or form. Use the AddMessageFilter method in the Application class to add the message filter to the message pump of the Application.
So when the program is loaded, we call Application. AddMessageFilter (Me ). However, by default, a Form or control cannot accept drag-and-drop files. We call a WIN32 API called DragAcceptFiles. This API can be used to set whether the corresponding control can accept drag-and-drop files. Then, you can use DragQueryFile to query the list of drag-and-drop files, that is, the path and file name of the drag-and-drop files.

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 messages

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

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.