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