Imports System.Drawing.Drawing2D
Imports System.Runtime.InteropServices
Public Class Form1
Dim bmp As Bitmap
Dim Blocation As Rectangle
Dim g As Graphics
Dim Dragging As Boolean = False
Dim OffsetX, OffsetY As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
bmp = New Bitmap(My.Resources.tree1_1)
bmp.MakeTransparent(Color.White)
Blocation = New Rectangle(10, 10, bmp.Width, bmp.Height)
AddHandler Panel2.Paint, AddressOf Panel2_Paint
AddHandler Panel2.MouseMove, AddressOf Panel2_MouseMove
AddHandler Panel2.MouseDown, AddressOf Panel2_MouseDown
AddHandler Panel2.MouseUp, AddressOf Panel2_MouseUp
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
bmp = New Bitmap(My.Resources.tree1_2)
bmp.MakeTransparent(Color.White)
Blocation = New Rectangle(30, 30, bmp.Width, bmp.Height)
End Sub
Private Sub Panel2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel2.Paint
e.Graphics.DrawImage(bmp, Blocation)
End Sub
Private Sub Panel2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel2.MouseMove
' See if we're dragging.
If (Dragging) Then
' We're dragging the image. Move it.
Blocation.X = e.X + OffsetX
Blocation.Y = e.Y + OffsetY
' Redraw.
Panel2.Invalidate()
Else
' We're not dragging the image. See if we're over it.
Dim new_cursor As Cursor = Cursors.Default
If (PointIsOverPicture(e.X, e.Y)) Then
new_cursor = Cursors.Hand
End If
If (Panel2.Cursor <> new_cursor) Then
Panel2.Cursor = new_cursor
End If
End If
End Sub
Private Sub Panel2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel2.MouseDown
' See if we're over the picture.
If (PointIsOverPicture(e.X, e.Y)) Then
' Start dragging.
Dragging = True
' Record the offset from the mouse to the picture's origin.
OffsetX = Blocation.X - e.X
OffsetY = Blocation.Y - e.Y
End If
End Sub
Private Sub Panel2_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel2.MouseUp
Dragging = False
End Sub
Private Function PointIsOverPicture(ByVal x As Integer, ByVal y As Integer) As Boolean
' See if it's over the picture's bounding rectangle.
If ((x < Blocation.Left) OrElse _
(y < Blocation.Top) OrElse _
(x >= Blocation.Right) _
OrElse (y >= Blocation.Bottom)) _
Then Return False
' See if the point is above a non-transparent pixel.
Dim i As Integer = x - Blocation.X
Dim j As Integer = y - Blocation.Y
Return (bmp.GetPixel(i, j).A > 0)
End Function
End Class
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.DoubleBuffered = True
bmp = New Bitmap(My.Resources.tree1_1)
bmp.MakeTransparent(Color.White)
Blocation = New Rectangle(10, 10, bmp.Width, bmp.Height)
AddHandler Panel2.Paint, AddressOf Panel2_Paint
AddHandler Panel2.MouseMove, AddressOf Panel2_MouseMove
AddHandler Panel2.MouseDown, AddressOf Panel2_MouseDown
AddHandler Panel2.MouseUp, AddressOf Panel2_MouseUp
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
bmp = New Bitmap(My.Resources.tree1_1)
bmp.MakeTransparent(Color.White)
Blocation = New Rectangle(10, 10, bmp.Width, bmp.Height)
Panel2.Invalidate()
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
bmp = New Bitmap(My.Resources.tree1_2)
bmp.MakeTransparent(Color.White)
Blocation = New Rectangle(30, 30, bmp.Width, bmp.Height)
Panel2.Invalidate()
End Sub