Drag and drop in file in VB.NET

zhaozj2021-02-17  48

Drag and drop in file in VB.NET

MONTAQUE

Summary:

This article describes how to implement drag and drop files in VB.NET, that is, when dragging and dropping from the Explorer to the application, the file is automatically obtained. The example in the article is a VB.NET instance program that accepts drag and drop file display file content.

introduction:

For files for text format, we can see content directly to Notepad; various types of pictures, drag to Photoshop, you can edit them directly. How do we implement the above effects in VB.NET?

Idea:

We know that every Windows application has a message queue, the main body of the program accepts the system's message, then distributes (gives an form, or a control), the recipient has the corresponding program to process the message. In the .NET's FORM, the program is not translated by default, that is, the default, our Class is a messaging pump that does not add an application. Can you add our Form Class to your application's message pump? can!

In .NET, any class that implements the iMessageFilter interface, can be added to the application's message pump to filter out or perform other operations before the message is scheduled to the control or form. Using the AddMessageFilter method in the Application class, you can add message filters to your application's message pump.

So we call Application.AddMessageFilter (me) when we load. However, by default, an FORM or control cannot accept the drag and drop file. We call a Win32 API DragAcceptFiles, which can set whether the corresponding control can accept the file. You can then use the DragQueryFile to query the drag and dropped file list, that is, drag and drop the file specific path and file name.

Code:

Imports system.Runtime.InteropServices

Public Class Form1

Inherits System.Windows.Forms.form

Implements IMESSAGEFILTER

'API declaration

Const wm_dropfiles = & h233 'Drag and drop message

Public Shared Sub Dragfinish (Byval HDROP AS Integer)

End Sub

public shared sub DragacceptFiles (byval hwnd as bug, byval faccect as boolean)

End Sub

public shared function Dragqueryfile (Byval HDrop as integer, byval lpstr as system.text.stringbuilder, byval chas "AS Integer

END FUNCTION

Private Sub Form1_Load (Byval E AS System.Object, Byval E AS System.Eventargs) Handles MyBase.LOAD

Application.AddMessageFilter (me)

DragacceptFiles (TextBox1.handle.Toint32, True)

End Subfunction PrefilterMessage (byref m as message) as boolean imports iMessageFilter.prefilterMessage

IF m.msg = wm_dropfiles then

'Set the action of dragging and drop

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 'records the first file name

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.crf & Sb.toString)

NEXT

Dragfinish (M.WParam.Toint32) 'Drag and drop completed

'Show file content

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 Is Nothing) THEN

Components.dispose ()

END IF

END IF

Application.RemoveMessageFilter (ME)

DragacceptFiles (TextBox1.handle.Toint32, False)

Mybase.dispose (Disposing)

End Sub

Note: Dragfinish releases memory after dragging and drop.

Reference: MS-Help: //ms.vscc/ms.msdnvs.2052/cpref/html/frlrfsystemwindowsformicsimsagefilterclasstopic.htm

转载请注明原文地址:https://www.9cbs.com/read-30867.html

New Post(0)