none
How to rotate button vb.net RRS feed

Answers

  • Here is a start on something you can modify as required.

    Imports System.Drawing.Drawing2D
    Public Class Form3
        Private WithEvents Button1 As New PictureBox With {.Parent = Me, .Cursor = Cursors.Hand,
        .BackgroundImageLayout = ImageLayout.Stretch, .BackColor = Color.SteelBlue,
        .Location = New Point(100, 100), .Size = New Size(100, 100)}
    
        Private BackgroundBmp As Image = Image.FromFile("c:\bitmaps\apple100.png")
        Private Angle As Double = 45
        Private MouseState As Integer = 0
    
        Private Sub Form7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Text = "Rotate Button"
            BackColor = Color.Teal
            ClientSize = New Size(300, 300)
            InitializeButton(Button1, CSng(Angle))
        End Sub
    
        Private Sub picR_Paint(sender As Object, e As PaintEventArgs) Handles Button1.Paint
            'draw the rotated image
            Using mx As New Matrix
                mx.RotateAt(CSng(Angle), New PointF(CSng(Button1.ClientSize.Width / 2),
                                                    CSng(Button1.ClientSize.Height / 2)))
                e.Graphics.Transform = mx
                Dim x As Integer = CInt((Button1.ClientSize.Width / 2) -
                    (BackgroundBmp.Width / 2))
                Dim y As Integer = CInt((Button1.ClientSize.Height / 2) -
                    (BackgroundBmp.Height / 2))
    
                If MouseState = 1 Then e.Graphics.TranslateTransform(-3, 2)
    
    
                e.Graphics.DrawImage(BackgroundBmp, x, y,
                                         BackgroundBmp.Width, BackgroundBmp.Height)
    
                e.Graphics.DrawString("Name", New Font("tahoma", 14, FontStyle.Bold),
                                      Brushes.White, x, y)
    
                If MouseState = 1 Then
                    Using br As New SolidBrush(Color.FromArgb(50, Color.White))
                        e.Graphics.FillRectangle(br, Button1.ClientRectangle)
                    End Using
                End If
    
            End Using
        End Sub
    
        Private Sub InitializeButton(thePic As PictureBox, angle As Single)
            Dim picRSize As New Size(100, 100)
            Dim picRCenter As New Point(150, 150)
    
            Using rg As New Region,
                            mx As New Matrix,
                            pth As New GraphicsPath
    
                'get the size of the rotated rect
                pth.AddRectangle(New RectangleF(0, 0, picRSize.Width, picRSize.Height))
                mx.RotateAt(angle, New PointF(CSng(picRSize.Width / 2), CSng(picRSize.Height / 2)))
                pth.Transform(mx)
                Dim rect As RectangleF = pth.GetBounds()
    
                'size the pict to fit the rect
                thePic.ClientSize = New Size(CInt(rect.Width + 1), CInt(rect.Height + 1))
                thePic.Location = New Point(CInt(picRCenter.X - thePic.ClientSize.Width / 2), CInt(picRCenter.Y - thePic.ClientSize.Height / 2))
    
                'remake the path rect at the new pic center and rotate
                pth.Reset()
                Dim x1 As Integer = CInt((thePic.ClientSize.Width / 2) - (picRSize.Width / 2))
                Dim y1 As Integer = CInt((thePic.ClientSize.Height / 2) - (picRSize.Height / 2))
                pth.AddRectangle(New RectangleF(x1, y1, picRSize.Width, picRSize.Height))
                mx.Reset()
                mx.RotateAt(angle, New PointF(CSng(thePic.ClientSize.Width / 2), CSng(thePic.ClientSize.Height / 2)))
                pth.Transform(mx)
    
                'add rotated path region to picturebox
                thePic.Region = New Region(pth)
            End Using
        End Sub
    
        Private Sub picR_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
            MouseState = 1
            Button1.Refresh()
        End Sub
    
        Private Sub picR_MouseUp(sender As Object, e As MouseEventArgs) Handles Button1.MouseUp
            MouseState = 0
            Button1.Refresh()
        End Sub
    End Class

    Tuesday, January 29, 2019 11:46 AM

All replies

  • Hi,

    The winform control can't be rotated, only the position and size can be changed. To achieve rotation, you can only use GDI + to draw your own graphics, the implementation of the idea is more complicated, the amount of code is also more.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 28, 2019 8:53 AM
  • WPF is the best choice for working with this.

    Conceptual and informative documentation, none are buttons but by following what's been done you can do that.

    https://docs.microsoft.com/en-us/dotnet/framework/wpf/graphics-multimedia/how-to-rotate-an-object

    https://www.dotnetheaven.com/article/wpf-rotatetransform-in-vb.net

    The following can be helpful for Windows Forms.

    https://stackoverflow.com/questions/39744667/vb-net-rotate-graphics-object-around-center


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, January 28, 2019 12:59 PM
    Moderator
  • Here is a start on something you can modify as required.

    Imports System.Drawing.Drawing2D
    Public Class Form3
        Private WithEvents Button1 As New PictureBox With {.Parent = Me, .Cursor = Cursors.Hand,
        .BackgroundImageLayout = ImageLayout.Stretch, .BackColor = Color.SteelBlue,
        .Location = New Point(100, 100), .Size = New Size(100, 100)}
    
        Private BackgroundBmp As Image = Image.FromFile("c:\bitmaps\apple100.png")
        Private Angle As Double = 45
        Private MouseState As Integer = 0
    
        Private Sub Form7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Text = "Rotate Button"
            BackColor = Color.Teal
            ClientSize = New Size(300, 300)
            InitializeButton(Button1, CSng(Angle))
        End Sub
    
        Private Sub picR_Paint(sender As Object, e As PaintEventArgs) Handles Button1.Paint
            'draw the rotated image
            Using mx As New Matrix
                mx.RotateAt(CSng(Angle), New PointF(CSng(Button1.ClientSize.Width / 2),
                                                    CSng(Button1.ClientSize.Height / 2)))
                e.Graphics.Transform = mx
                Dim x As Integer = CInt((Button1.ClientSize.Width / 2) -
                    (BackgroundBmp.Width / 2))
                Dim y As Integer = CInt((Button1.ClientSize.Height / 2) -
                    (BackgroundBmp.Height / 2))
    
                If MouseState = 1 Then e.Graphics.TranslateTransform(-3, 2)
    
    
                e.Graphics.DrawImage(BackgroundBmp, x, y,
                                         BackgroundBmp.Width, BackgroundBmp.Height)
    
                e.Graphics.DrawString("Name", New Font("tahoma", 14, FontStyle.Bold),
                                      Brushes.White, x, y)
    
                If MouseState = 1 Then
                    Using br As New SolidBrush(Color.FromArgb(50, Color.White))
                        e.Graphics.FillRectangle(br, Button1.ClientRectangle)
                    End Using
                End If
    
            End Using
        End Sub
    
        Private Sub InitializeButton(thePic As PictureBox, angle As Single)
            Dim picRSize As New Size(100, 100)
            Dim picRCenter As New Point(150, 150)
    
            Using rg As New Region,
                            mx As New Matrix,
                            pth As New GraphicsPath
    
                'get the size of the rotated rect
                pth.AddRectangle(New RectangleF(0, 0, picRSize.Width, picRSize.Height))
                mx.RotateAt(angle, New PointF(CSng(picRSize.Width / 2), CSng(picRSize.Height / 2)))
                pth.Transform(mx)
                Dim rect As RectangleF = pth.GetBounds()
    
                'size the pict to fit the rect
                thePic.ClientSize = New Size(CInt(rect.Width + 1), CInt(rect.Height + 1))
                thePic.Location = New Point(CInt(picRCenter.X - thePic.ClientSize.Width / 2), CInt(picRCenter.Y - thePic.ClientSize.Height / 2))
    
                'remake the path rect at the new pic center and rotate
                pth.Reset()
                Dim x1 As Integer = CInt((thePic.ClientSize.Width / 2) - (picRSize.Width / 2))
                Dim y1 As Integer = CInt((thePic.ClientSize.Height / 2) - (picRSize.Height / 2))
                pth.AddRectangle(New RectangleF(x1, y1, picRSize.Width, picRSize.Height))
                mx.Reset()
                mx.RotateAt(angle, New PointF(CSng(thePic.ClientSize.Width / 2), CSng(thePic.ClientSize.Height / 2)))
                pth.Transform(mx)
    
                'add rotated path region to picturebox
                thePic.Region = New Region(pth)
            End Using
        End Sub
    
        Private Sub picR_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
            MouseState = 1
            Button1.Refresh()
        End Sub
    
        Private Sub picR_MouseUp(sender As Object, e As MouseEventArgs) Handles Button1.MouseUp
            MouseState = 0
            Button1.Refresh()
        End Sub
    End Class

    Tuesday, January 29, 2019 11:46 AM
  • if you simply need the button to appear as if it is rotated, you could use an image on the button instead of text and size the button to be taller than it is wide.
    Tuesday, January 29, 2019 1:00 PM
  • thank you.
    Thursday, January 31, 2019 2:01 AM
  • thank you.
    Thursday, January 31, 2019 2:01 AM
  • thank you.
    Thursday, January 31, 2019 2:01 AM
  • thank you.
    Thursday, January 31, 2019 2:01 AM
  • Is it a rotating picture box?
    Thursday, January 31, 2019 2:06 AM
  • Is it a rotating picture box?

    No. You cant rotate controls in VB.

    The example is creating a region on the picturebox and rotating the region.

    Saturday, February 2, 2019 1:27 PM