0

I need to rotate my custom control in Windows Form using C# without using third-party libraries.

Don't want to either rotate the text or image of control instead actually need to entirely rotate control.

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
user1291401
  • 264
  • 1
  • 6
  • 18
  • And what have you tried so far to achieve it? – Ondrej Janacek Dec 12 '13 at 09:17
  • In the drawing namespaces under `System` there are helper methods that will take a rectangle and perform a matrix transformation on it, this will allow you to paint a rotated / scaled / moved rectangle without having to need to calculate the transformed rectangle yourself. Just hunting for it now. – Adam Houldsworth Dec 12 '13 at 09:20
  • using wpf control in winforms, i tried to rotate it.. but results were not as expected. – user1291401 Dec 12 '13 at 09:21
  • @Adam Houldsworth -- please post it here once you get solution on dis – user1291401 Dec 12 '13 at 09:26
  • @user1291401 Have done, but my solution isn't specific to your question, it's a general approach on how to do it. Basically it all hinges on how much control over the painting you can get. – Adam Houldsworth Dec 12 '13 at 09:27

4 Answers4

2

From here:Is it possible to rotate a button control in WinForms? 5 up vote accepted

You can't rotate controls. That's simply not supported by the native API controls that WinForms uses.

And one might wonder why it even should be supported. What could you possibly be trying to do that you'd need to rotate a button control? It would be much easier to draw it in a different place with a different shape in the first place, rather than trying to rotate an existing control. (Do note that you can also resize and reposition a control at run-time, if that would fit your needs. Investigate the Size and Location properties.)

The only workaround is to draw the control's image to a bitmap, hide the control, and draw the bitmap onto the form in the location you want it to appear. Of course, that won't result in a control that the user can interact with. They won't be able to click an image of a button, because it's not a real button. If that's acceptable to you, you should probably be using an image in the first place, rather than a button.

Community
  • 1
  • 1
d.lavysh
  • 1,404
  • 14
  • 23
1

I am pretty sure you can not perform this in windows forms at least not easily. However you can perform this in WPF and then bring WPF to your windows Form if you are looking for cool designs or even special effects to your controls.

This is FAR more easily done in WPF. In Windows Form, it's a huage pain to pull off.

Hope this help

Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
  • I tried even in WPF.. but result is not as expected .. sometimes controls text/iamge gets trimmed at some corners. – user1291401 Dec 12 '13 at 09:20
  • refer http://stackoverflow.com/questions/10791564/how-to-rotate-translate-and-scale-a-control-object-around-an-specific-point-in – Vignesh Kumar A Dec 12 '13 at 09:29
1

It is not possible to rotate controls. This is not supported by WinForms' controls API. As you are dealing with a custom control, try simply redrawing it to suit your purposes.

nestedloop
  • 2,596
  • 23
  • 34
  • 1
    It is supported through drawing, which is how WPF does it... saying it's not supported is not entirely accurate. It's just more manual because there is no rendering engine in the background giving you as much support. – Adam Houldsworth Dec 12 '13 at 09:21
  • @AdamHouldsworth As I said, the OP can "rotate" the custom control by redrawing it. – nestedloop Dec 12 '13 at 09:24
  • 1
    @nestedloop All UI frameworks "rotate" by redrawing it... You are correct in that the control itself offers no help, but it is entirely possible to rotate controls if you can control the painting of them. – Adam Houldsworth Dec 12 '13 at 09:26
1

It is possible if you can get control of the painting, but you have to do a lot of the work yourself. Basically it all occurs on painting. A good example demonstrating how to do it is the Dock Panel Suite for WinForms.

In the VS2005AutoHideStrip (found here), there is a GetTransformedRectangle method that uses a System.Drawing.Drawing2D.Matrix class to rotate a rectangle.

It also sets the Transform property on the Graphics class, which will apply transformations automatically when you ask for something to be painted.

I advise reviewing this code for examples, it does it to draw docked tab strips down the sides of the page as opposed to top / bottom.


Some controls like TextBox are funny in that they borrow heavily from low-level Win32 libraries in their painting / behaviour, I've no idea if it is possible to get enough control to rotate this.
Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187