Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB.NET
Public Class MyPictureBox
    Inherits PictureBox

    'Shelf Position and Orientation
    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
 'And a bunch of other properties

'I have a custom paint event:
  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:

VB.NET
                For Each picItem As MyPictureBox In picFloor.Controls
                    If sender.Bounds.IntersectsWith(picItem.Bounds) Then
                        If picItem.Tag = sender.Tag Then
                            'Colliding with self
                        Else
                            'Collision!!!
                            sender.left = oldX
                            sender.top = oldY
                        End If
                    End If
                Next
'My class is added to the floor on a button click: 
  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!
Posted
Updated 18-Aug-16 10:58am
v3

Your question is about an article on CP, it is better to leave a comment on bottom of article
Rotating PictureBox Control (Comments)[^]
Advantage, the author will be noticed.
I checked, the author is still active on the site.
 
Share this answer
 
No suggestions?
Well I found one possible solution. Its not really rotating the control but gives the same effect.
You can create a custom shape control. See here: Shaped Windows Forms and Controls in Visual Studio .NET.

Create a four point polygon,(or any other shape you want) then just recalculate the points based on the rotation angle. See VB Helper: HowTo: Rotate the points in a polygon[^].
However, I decided to go with the graphics matrix option, using regions for interaction. Lot of code to rewrite, but drawing 100 polygons is much more eficient than drawing 100 picture boxes. I'd rather do a proper job now than sit a problem later.
 
Share this answer
 
v2
Thanks for the reply but I think you misunderstood. I'm not asking about the other persons article. His article did come up as a possible solution to my question, but unfortunately it won't work in my case. I was looking for other possibilities to try.
 
Share this answer
 
Comments
Patrice T 15-Aug-16 7:07am    
It don't cost to ask the author as he may be familiar with your problem.
Regg Fulton 15-Aug-16 7:55am    
Thanks, I'll take your advice.
Patrice T 15-Aug-16 7:09am    
You shouldn't use a Solution to post a comment.
Use the button "Have a Question or Comment?" under the solution instead.
Regg Fulton 15-Aug-16 7:54am    
My bad. First time posting. Should I delete it?
Patrice T 15-Aug-16 8:07am    
All the interest is that the one you talk to is noticed when you use that button. But since I noticed by myself, you see what you do.
You can delete if you want, it is up to you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900