Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze VB.NET drag-and-drop files with examples

2025-04-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to use examples to analyze VB.NET drag-and-drop files, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail, people with this need can come to learn, I hope you can gain something.

VB.NET is quite common, so I studied VB.NET drag-and-drop files. Here's how to accept VB.NET drag-and-drop files in VB.NET.

VB.NET drag-and-drop files are obtained automatically since they were dragged and dropped into the application in Explorer. The example in this paper is a VB.NET instance program that accepts VB.NET drag-and-drop files to display the contents of files.

For text format files, we can drag directly to notepad to see the content; various types of pictures, drag into the Photoshop, you can edit them directly. How can we achieve the above effect in the program developed by VB.NET?

We know that every Windows application has a message queue, and the main body of the program receives the message from the system and then distributes it (to a form, or a control), and the recipient has a corresponding program to process the message. In the Form of .NET, the program does not translate these messages by default, which means that our Class is a message pump that does not join the application. Can we add our Form Class to the application's message pump? Sure!

In .NET, any class that implements the IMessageFilter interface can be added to the application's message pump to filter out a message or perform other actions before it is dispatched to a 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 the program loads. However, by default, a Form or control cannot accept VB.NET drag and drop files, so we call a WIN32 API DragAcceptFiles that sets whether the corresponding control can accept VB.NET drag and drop files. You can then use DragQueryFile to query the list of files you drag and drop to, that is, the specific path and file name of the file you dragged and dropped by VB.NET.

Imports System.Runtime.InteropServices

Public Class Form1

Inherits System.Windows.Forms.Form

Implements IMessageFilter

'API declares

Const WM_DROPFILES = & H233' drag and drop file message

Public Shared Sub DragFinish (ByVal hDrop As Integer)

End Sub

Public Shared Sub DragAcceptFiles

(ByVal hwnd As Integer, ByVal fAccept As Boolean)

End Sub

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 action of drag 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 'record * 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

'display the contents of the file

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

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report