Warum Panels mit Bildern verwenden?
Alles einfach in die PictureBox zeichnen.
Ein einfaches Beispiel mit verschieben und skalieren(Mausrad)
Public Class Form1
Dim WithEvents Pb As New PictureBox With {.Dock = DockStyle.Fill, .Parent = _
Me}
Dim hgrund As Bitmap = SystemIcons.Shield.ToBitmap
Dim picList As New List(Of MyPicture)
Dim loc, pos As Point, img As Int32, mov As Boolean
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.ClientSize = New Size(800, 800)
For y = 200 To 400 Step 100
picList.Add(New MyPicture(SystemIcons.Warning.ToBitmap, New _
Rectangle(225, y, 64, 64)))
picList.Add(New MyPicture(SystemIcons.Error.ToBitmap, New Rectangle( _
375, y, 64, 64)))
picList.Add(New MyPicture(Me.Icon.ToBitmap, New Rectangle(525, y, _
64, 64)))
Next
End Sub
Private Sub Pb_MouseDown(sender As Object, e As MouseEventArgs) Handles _
Pb.MouseDown
For i = picList.Count - 1 To 0 Step -1
If picList(i).Bounds.Contains(e.Location) Then
loc = e.Location
pos = New Point(picList(i).Bounds.X, picList(i).Bounds.Y)
picList.Add(picList(i)) 'aktuelles Bild =
picList.RemoveAt(i) 'Top-Position
img = picList.Count - 1
mov = True
Exit Sub
End If
Next
mov = False
End Sub
Private Sub Pb_MouseMove(sender As Object, e As MouseEventArgs) Handles _
Pb.MouseMove
If mov AndAlso e.Button = MouseButtons.Left Then
Dim re As Rectangle = picList(img).Bounds
picList(img) = New MyPicture(picList(img).Img, New Rectangle(pos.X _
+ e.X - loc.X, pos.Y + e.Y - loc.Y, re.Width, re.Height))
Pb.Invalidate()
End If
End Sub
Private Sub Pb_Paint(sender As Object, e As PaintEventArgs) Handles Pb.Paint
e.Graphics.InterpolationMode = _
Drawing2D.InterpolationMode.NearestNeighbor
e.Graphics.Clear(Color.Green)
e.Graphics.DrawImage(hgrund, 0, 0, Pb.Width, Pb.Height)
For Each pic In picList
e.Graphics.DrawImage(pic.Img, pic.Bounds)
Next
End Sub
Private Sub Pb_MouseWheel(sender As Object, e As MouseEventArgs) Handles _
Pb.MouseWheel
If Not mov Then Return 'kein Bild ausgewählt
Dim r As Rectangle = picList(img).Bounds
If e.Delta > 0 Then
picList(img) = New MyPicture(picList(img).Img, Rectangle.Inflate(r, _
4, 4))
Else
If r.Width <= 20 OrElse r.Height <= 20 Then Exit Sub ' Mindestgrösse!
picList(img) = New MyPicture(picList(img).Img, Rectangle.Inflate(r, _
-4, -4))
End If
Pb.Invalidate()
End Sub
Private Sub PB_MouseEnter(sender As Object, e As EventArgs) Handles _
Pb.MouseEnter
Pb.Focus() ' Für Pb.MouseWheel nötig!
End Sub
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
Pb.Invalidate()
End Sub
End Class
Public Class MyPicture
Private _img As Bitmap
Private _bounds As Rectangle
Public Sub New(Img As Bitmap, Rec As Rectangle)
_img = Img
_bounds = Rec
End Sub
Public Property Img As Bitmap
Get
Return _img
End Get
Set(value As Bitmap)
_img = Img
End Set
End Property
Public Property Bounds As Rectangle
Get
Return _bounds
End Get
Set(value As Rectangle)
_bounds = Bounds
End Set
End Property
End Class MfG GPM |