Imports System.Runtime.InteropServices
Public Class Form1
Private Declare Function GetDoubleClickTime Lib "user32" () As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.AllowDrop = True
PictureBox2.AllowDrop = True
PictureBox3.AllowDrop = True
PictureBox4.AllowDrop = True
PictureBox5.AllowDrop = True
PictureBox6.AllowDrop = True
PictureBox7.AllowDrop = True
PictureBox8.AllowDrop = True
End Sub
Private Sub Panel1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel1.DragDrop
If e.Data.GetDataPresent(DataFormats.Bitmap) Then
Dim pb As New PictureBox With {.SizeMode = PictureBoxSizeMode.Normal, _
.Size = DirectCast(e.Data.GetData(DataFormats.Bitmap), Bitmap).Size, _
.Image = DirectCast(e.Data.GetData(DataFormats.Bitmap), Bitmap), _
.Location = Panel1.PointToClient(New Point(e.X, e.Y)), _
.allowdrop = True, _
.Tag = Environment.TickCount}
Me.Panel1.Controls.Add(pb)
pb.BringToFront()
AddHandler pb.DragOver, AddressOf PictureBoxes_DragOver
AddHandler pb.MouseDown, AddressOf PictureBoxes_MouseDown
AddHandler pb.GiveFeedback, AddressOf PictureBoxes_giveFeedback
If Not e.KeyState = 8 Then
source.Dispose()
End If
End If
End Sub
Private Sub PictureBoxes_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragOver, PictureBox2.DragOver, PictureBox3.DragOver, PictureBox4.DragOver, PictureBox5.DragOver, PictureBox6.DragOver, PictureBox7.DragOver, PictureBox8.DragOver, Panel1.DragOver
'make sure drag drop data can be used
If e.Data.GetDataPresent(DataFormats.Bitmap) And e.AllowedEffect = DragDropEffects.Move Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
Dim source As PictureBox
Dim image As Bitmap
Private Sub PictureBoxes_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown, PictureBox2.MouseDown, PictureBox3.MouseDown, PictureBox4.MouseDown, PictureBox5.MouseDown, PictureBox6.MouseDown, PictureBox7.MouseDown, PictureBox8.MouseDown
source = DirectCast(sender, PictureBox)
If Environment.TickCount - CInt(source.Tag) < GetDoubleClickTime Then
MsgBox("heyhey")
End If
source.Tag = Environment.TickCount
image = DirectCast(source.Image.Clone, Bitmap)
source.DoDragDrop(image, DragDropEffects.Move)
End Sub
Public Sub PictureBoxes_giveFeedback(ByVal sender As Object, ByVal e As System.Windows.Forms.GiveFeedbackEventArgs) Handles PictureBox1.GiveFeedback, PictureBox2.GiveFeedback, PictureBox3.GiveFeedback, PictureBox4.GiveFeedback, PictureBox5.GiveFeedback, PictureBox6.GiveFeedback, PictureBox7.GiveFeedback, PictureBox8.GiveFeedback, Panel1.GiveFeedback
e.UseDefaultCursors = False
Dim EffectCursor As New Cursor(New IO.MemoryStream(My.Resources.Move))
Dim img As New Bitmap(15 + image.Width, 26 + image.Height)
Dim gr As Graphics = Graphics.FromImage(img)
gr.FillRectangle(Brushes.Magenta, img.GetBounds(Drawing.GraphicsUnit.Pixel))
EffectCursor.Draw(gr, New Rectangle(0, 0, _
EffectCursor.Size.Width, EffectCursor.Size.Height))
gr.DrawImage(image, 15, 26, image.Width, image.Height)
img.MakeTransparent(Color.Magenta)
Cursor.Current = CreateCursor(img)
End Sub
#Region " CreateIconIndirect"
Private Structure IconInfo
Public fIcon As Boolean
Public xHotspot As Int32
Public yHotspot As Int32
Public hbmMask As IntPtr
Public hbmColor As IntPtr
End Structure
<DllImport("user32.dll", EntryPoint:="CreateIconIndirect")> _
Private Shared Function CreateIconIndirect(ByVal iconInfo As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function DestroyIcon(ByVal handle As IntPtr) As Boolean
End Function
<DllImport("gdi32.dll")> _
Public Shared Function DeleteObject(ByVal hObject As IntPtr) As Boolean
End Function
''' <summary>
''' CreateCursor
''' </summary>
''' <param name="bmp"></param>
''' <returns>custom Cursor</returns>
''' <remarks>creates a custom cursor from a bitmap</remarks>
Public Function CreateCursor(ByVal bmp As Bitmap) As Cursor
'Setup the Cursors IconInfo
Dim tmp As New IconInfo
tmp.xHotspot = 0
tmp.yHotspot = 0
tmp.fIcon = False
tmp.hbmMask = bmp.GetHbitmap()
tmp.hbmColor = bmp.GetHbitmap()
'Create the Pointer for the Cursor Icon
Dim pnt As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(tmp))
Marshal.StructureToPtr(tmp, pnt, True)
Dim curPtr As IntPtr = CreateIconIndirect(pnt)
'Clean Up
DestroyIcon(pnt)
DeleteObject(tmp.hbmMask)
DeleteObject(tmp.hbmColor)
Return New Cursor(curPtr)
End Function
#End Region
End Class