VB.net - 拖&下载并获取文件路径? [英] VB.net - drag & drop and get file path?
问题描述
我想要将文件/可执行文件/快捷方式拖到Windows窗体应用程序中,并让应用程序确定删除文件的原始路径,然后将其作为字符串返回?
I'd like to be able to drag a file/executable/shortcut into a windows form application and have the application determine the original path of the dropped file then return it as a string?
例如将图像从桌面拖动到应用程序和消息框中,使图像的本地路径。
E.g. drag an image from the desktop into the app and messagebox up the local path of the image.
可以吗?有人可以给我一个例子吗?
Is that possible? Could someone provide me with an example maybe?
谢谢
推荐答案
这很容易只需通过设置 启用drap-and-drop AllowDrop
属性为 True
并处理 DragEnter
和 DragDrop
事件。
It's quite easy. Just enable drap-and-drop by setting the AllowDrop
property to True
and handle the DragEnter
and DragDrop
events.
在 DragEnter
事件处理程序中,您可以使用 DataFormats
课程。
In the DragEnter
event handler, you can check if the data is of the type you want using the DataFormats
class.
在 DragDrop
事件处理程序中,使用 数据
属性的 DataEventArgs
来接收实际数据。
In the DragDrop
event handler, use the Data
property of the DataEventArgs
to recieve the actual data.
示例:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.AllowDrop = True
End Sub
Private Sub Form1_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
For Each path In files
MsgBox(path)
Next
End Sub
Private Sub Form1_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
这篇关于VB.net - 拖&下载并获取文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!