user1291401
user1291401

Reputation: 264

How to rotate custom control in Windows Forms

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.

Upvotes: 0

Views: 4953

Answers (4)

Adam Houldsworth
Adam Houldsworth

Reputation: 64477

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.

Upvotes: 1

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28403

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

Upvotes: 1

d.lavysh
d.lavysh

Reputation: 1504

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.

Upvotes: 2

nestedloop
nestedloop

Reputation: 2646

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.

Upvotes: 1

Related Questions