Public Class Form2
Private _dropHelper As ShellUtils.DropTarget
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
_dropHelper = New ShellUtils.DropTarget(ListView1.Handle)
End Sub
Private Sub ListView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop
_dropHelper.Drop(PointToClient(MousePosition), e.Effect, e.Data)
Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
For Each file As String In files
Dim ext As String = IO.Path.GetExtension(file)
If Not ImageList1.Images.ContainsKey(ext) Then
ImageList1.Images.Add(ext, Icon.ExtractAssociatedIcon(file))
End If
ListView1.Items.Add(file, ext)
Next
End Sub
Private Sub ListView1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = e.AllowedEffect
_dropHelper.DragEnter(ListView1.PointToClient(New Point(e.X, e.Y)), e.Effect, e.Data)
End If
End Sub
Private Sub ListView1_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DragLeave
_dropHelper.DragLeave()
End Sub
Private Sub ListView1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragOver
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = e.AllowedEffect
Dim pt As Point = ListView1.PointToClient(New Point(e.X, e.Y))
_dropHelper.DragOver(pt, e.Effect)
_dropHelper.Show(True)
End If
End Sub
Private Sub ListView1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles ListView1.ItemDrag
If e.Button = Windows.Forms.MouseButtons.Left Then
If ListView1.SelectedItems.Count > 0 Then
Dim files As New Specialized.StringCollection
For Each lvi As ListViewItem In ListView1.SelectedItems
files.Add(lvi.Text)
Next
Dim data As New ShellUtils.DataObjectEx
data.SetFileDropList(files)
ShellUtils.DragSource.DoDragDrop(data, ListView1, DragDropEffects.Copy Or DragDropEffects.Move, PointToClient(MousePosition))
End If
End If
End Sub
End Class