Greets to all. For years I have visited the forum looking for answers and have never needed to ask a question as it was always asked before with great answers! I'm used to database applications and GUIs. However my latest project I need to start playing with painting and graphics.
In my project I have a picture box representing the floor. Then multiple picture boxes inside it representing shelves. Now I need to be able to rotate the shelves to any user defined angle.
What I have tried:
I looked at this:
Rotating PictureBox Control[
^]
but it creates a transparent control that is bigger than the picture. This will mess with my collision checking so I can't put two angled shelves touching each other.
What I have:
I created a class 'MyPicturebox' inherits picturebox and set custom properties:
Public Class MyPictureBox
Inherits PictureBox
Private _ShelfID As Integer
Private _ShelfRotation As Single
<CategoryAttribute("Custom"), _
Browsable(True), _
[ReadOnly](False), _
DescriptionAttribute("Angle of rotation.")> _
Public Property ShelfRotation() As Single
Get
Return _ShelfRotation
End Get
Set(ByVal value As Single)
_ShelfRotation = value
End Set
End Property
Private Sub MyPictureBox_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim g As Graphics = e.Graphics
Dim thisWidth, thisHeight As Integer
If _shelfBBorderBrush = "Solid" Then
Dim BorderBrush As New SolidBrush(_ShelfBorderColor)
g.FillRectangle(BorderBrush, 0, 0, thisWidth, thisHeight)
ElseIf _shelfBBorderBrush = "Gradient" Then
Dim BorderBrush As New System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, _ShelfBorderColor, _ShelfBorderColor2, System.Drawing.Drawing2D.LinearGradientMode.Vertical)
g.FillRectangle(BorderBrush, 0, 0, thisWidth, thisHeight)
End If
End Sub
So far all this works. I can create, edit, paint and move the controls without any problem. On my form I do collision detection with:
For Each picItem As MyPictureBox In picFloor.Controls
If sender.Bounds.IntersectsWith(picItem.Bounds) Then
If picItem.Tag = sender.Tag Then
Else
sender.left = oldX
sender.top = oldY
End If
End If
Next
Private Sub cmdAddShelf1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddShelf1.Click
ReDim Preserve cmdShelf(shelfCount)
cmdShelf(shelfCount - 1) = New MyPictureBox
With cmdShelf(shelfCount - 1)
.Tag = shelfCount - 1
.ShelfName = "Shelf_" & shelfCount - 1
.Size = New Size((numDWidth.Value * mScale), (numDDepth.Value * mScale))
.Location = New Point(DropPannel.HorizontalScroll.Value + BorderBuffer, DropPannel.VerticalScroll.Value + BorderBuffer)
End With
AddHandler cmdShelf(shelfCount - 1).MouseWheel, AddressOf PictureBox_MouseWheel
AddHandler cmdShelf(shelfCount - 1).MouseMove, AddressOf PictureBox_MouseMove
AddHandler cmdShelf(shelfCount - 1).MouseClick, AddressOf PictureBox_Click
AddHandler cmdShelf(shelfCount - 1).MouseDown, AddressOf Button_Down
AddHandler cmdShelf(shelfCount - 1).MouseUp, AddressOf Button_Up
AddHandler cmdShelf(shelfCount - 1).MouseEnter, AddressOf MEnter
AddHandler cmdShelf(shelfCount - 1).MouseLeave, AddressOf MLeave
End Sub
So how can I rotate the control without changing the size or visibility?
My next option is to get rid of the picture boxes and only work with graphics on a single picturebox. But then I will loose all my event handlers(I think) and it will take a lot of code rewriting. Before I start doing that, I thought I'll ask the pro's!