Public Class Form1
Private Dragging As Boolean = False
Private DragCopy As Boolean = False
Private PictureBox3 As PictureBox = Nothing
Private RotateFlip As Integer = 0
Private PictureBox1Image As Image 'to save image during testing
Private Function CanDrop() As Boolean
'Not needed but saves a lot of typing and makes the boolean expression easier to read
Dim pb2left As Integer = PictureBox2.Left
Dim pb2top As Integer = PictureBox2.Top
Dim pb2right As Integer = PictureBox2.Right
Dim pb2bottom As Integer = PictureBox2.Bottom
Dim pb3left As Integer = PictureBox3.Left
Dim pb3top As Integer = PictureBox3.Top
Dim pb3right As Integer = PictureBox3.Right
Dim pb3bottom As Integer = PictureBox3.Bottom
'IF any part of PictureBox3 is over PictureBox2 THEN return True ELSE return False
Return _
((pb3left >= pb2left And pb3left <= pb2right) OrElse _
(pb3right >= pb2left And pb3right <= pb2right)) _
AndAlso _
((pb3top >= pb2top And pb3top <= pb2bottom) OrElse _
(pb3bottom >= pb2top And pb3bottom <= pb2bottom))
End Function
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
If Not PictureBox1.Image Is Nothing Then
Dragging = True
With PictureBox1
PictureBox3 = New PictureBox
PictureBox3.Size = .Size
PictureBox3.Image = .Image
PictureBox3.SizeMode = .SizeMode
PictureBox3.Location = .Location
End With
Me.Controls.Add(PictureBox3)
End If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
Dim loc As Point
loc.X = e.X
loc.Y = e.Y
If Dragging Then
PictureBox3.Location = loc
PictureBox3.BringToFront()
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
If Dragging Then
Dragging = False
If CanDrop() Then
PictureBox2.Image = PictureBox1.Image
PictureBox2.SizeMode = PictureBox1.SizeMode
If Not DragCopy Then PictureBox1.Image = Nothing
DragCopy = False
End If
PictureBox3.SendToBack()
PictureBox3.Image = Nothing
PictureBox3 = Nothing
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PictureBox2.Image = Nothing
PictureBox1.Image = PictureBox1Image
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim i As Image
If Not PictureBox2.Image Is Nothing Then
i = PictureBox2.Image
i.RotateFlip(CType(RotateFlip, RotateFlipType))
PictureBox2.Image = i
i = Nothing
RotateFlip = (RotateFlip + 1) Mod 8
End If
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If Dragging Then
DragCopy = e.Control
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
PictureBox1Image = PictureBox1.Image
Me.KeyPreview = True
' *** Possibly use the following as a start point to offer the
' *** Rotation / Flip options on a right-click menu
'For a As Integer = 0 To 7
' ??? = CType(a, RotateFlipType).ToString()
'Next
End Sub
End Class