Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I want to rotate button in my project... Is this possible??
Posted
Updated 13-May-13 0:28am
v2
Comments
Maciej Los 13-May-13 5:54am    
WinForms, WebControls, WPF?
Archana K 13-May-13 6:09am    
Windows form.....

I'm not sure that you need to rotate control (button)... In most cases is enough to rotate text on it. If i'm wrong, please, let me know.

Please, refer this link: How to: Align Drawn Text[^]
 
Share this answer
 
v2
Comments
Archana K 13-May-13 7:16am    
I want to rotate a button....
Maciej Los 13-May-13 8:32am    
In Windows Forms it's a giant pain in the ass. In WPF, it's incredibly easy!

I think you're using the wrong application type...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-May-13 11:08am    
Right idea, a 5. The only one really working answer so far.
I added the solution for Forms, please see.
—SA
Maciej Los 15-May-13 1:57am    
+5!
The best idea so far is the Solution 4.

The right solution for System.Windows.Forms is: create a custom control which behaves like a button and implement rotation property. If you want to rotate to an arbitrary angle, you should also set the property System.Windows.Forms.Control.Region:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.region.aspx[^].

[EDIT #2]

As Maciej expressed his concerns about first solution, I'll provide some mode detail.

In case of arbitrary angle, it's a bit complicated, and I didn't want to spend so much time for more complicated code at first.

Basically, you need to have a bigger control which fits the full set of buttons at all angles; it would be a round area, and you should draw a rectangle around the round. Can you picture such figure? Inside this area, you can draw a rectangular button at any angle. Now, you render the whole button in all the detail, including borders, using System.Drawing.Graphics.Transform, depending on the current angle.

Now, you need to cut out unused part of the control. To do so, you calculate the rectangular Region representing the tilted button and assign this region to Region property of the control.

Now, how to change the angle? The Angle property should have a setter to do all the side effects of changing the value. If the value is different from the previous value, you should to two things 1) create a rectangular region at different angle and re-assign the property Region of the control; 2) call Invalidate to cause re-drawing of the control.

[END EDIT #2]

[EDIT #1]

If you only want to allow only 90°, the problem is especially simple. Derive the button class from System.Windows.Forms.ButtonBase. First, switch from horizontal to vertical position is done view swapping Width and Height values. It will correctly render non-client area of the button.

Render the client area in the overridden method System.Windows.Forms.Control.OnPaint. You need to render the text rotated. For rotation, use System.Drawing.Graphics.Transform (you will get the instance of Graphics from an event arguments parameter):
http://msdn.microsoft.com/en-us/library/system.windows.forms.buttonbase.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpaint.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.painteventargs.graphics.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.transform.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.matrix.aspx[^].

—SA
 
Share this answer
 
v3
Comments
Maciej Los 14-May-13 18:30pm    
I'm not so familiar with drawing methods as you are, Sergey, but i can't find a way how to use above methods to rotate control (button) with the specified angle... I think OP should use Matrix.RotateAt[^] function, but i'm afraid Archana[^] can't understand it (as i'm a bit confused too).
Sergey Alexandrovich Kryukov 14-May-13 18:49pm    
I explained it in detail for 90°, in detail, did you get it?

As to the arbitrary angle, it's a bit complicated. I've just added [EDIT #2], please see.
OP will also need to reproduce all the Button events, and they are much more complicated than one would be able to see at first glance. One complex thing is this: press a left mouse button and, keeping it pressed, move the mouse pointer in and out of the button. Can you see how rendering reflects the mouse position? This is one of the most difficult part. I've done it all, and more (WPF was not yet available) and I remembered that it took tons of my time before I made it all working.

So, the Solution 4 about WPF is the best so far.

I hope now my solution is detailed enough... What do you think?

—SA
Maciej Los 15-May-13 1:56am    
Now i understand what you mean. Thank you, Sergey.
+5!
Sergey Alexandrovich Kryukov 15-May-13 2:05am    
Thank you, Maciej.
—SA
_Amy 15-May-13 2:36am    
Yes, got it. +5!
Here is an answer of similar qestion:
How do I rotate a label in C#?[^]

[Edit]
Animator for WinForms[^]
[/Edit]

--Amit
 
Share this answer
 
v2
Comments
Archana K 13-May-13 6:29am    
I want VB.Net code.....
_Amy 13-May-13 6:31am    
You can use online converter. See the link:
Convert C# to VB.NET[^]
Archana K 13-May-13 6:32am    
I already done it but its not working.....
Sergey Alexandrovich Kryukov 14-May-13 11:07am    
No, for a button, it's not enough.
—SA
_Amy 14-May-13 23:52pm    
Hi SA. Please see my updated solution. I've added a link for Winforms Animation.
Thanks you for guiding. :)
this will rotate a button....
but dont know how you want..

VB
Public Class Form1
    Dim Poss As Integer = 0
    Dim OrigSize As System.Drawing.Size
    Dim orgLoc As System.Drawing.Point

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        OrigSize = Button1.Size
        orgLoc = Button1.Location
        Button1.Text = "T e x t"
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        If Poss = 0 Then
            Button1.Size = New System.Drawing.Size(OrigSize.Width - 50, OrigSize.Height + 40)
            Poss += 1
        ElseIf Poss = 1 Then
            Button1.Size = OrigSize
            Button1.Location = New System.Drawing.Point(orgLoc.X - 77, orgLoc.Y)
            Button1.Text = "t x e T"
            Poss += 1
        ElseIf Poss = 2 Then
            Button1.Location = New System.Drawing.Point(orgLoc.X, orgLoc.Y - 38)
            Button1.Size = New System.Drawing.Size(OrigSize.Width - 50, OrigSize.Height + 40)
            Poss += 1
        ElseIf Poss = 3 Then
            Button1.Location = orgLoc
            Button1.Size = OrigSize
            Button1.Text = "T e x t"
            Poss = 0
        End If

    End Sub
End Class
 
Share this answer
 
v2
Comments
Basmeh Awad 13-May-13 8:30am    
Add 2 buttons Button1 will Rotate and Button2 to rotate
Maciej Los 15-May-13 1:56am    
+5!

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