Rotate User Control

G

Guest

Hello,

i want to create my own control, but this control must be able to rotate.
I know, how to overide OnPaint method.
But..., for example I want to paint this control as rectangle with width 40,
height 60 with variable rotation.
How do I get new size of my control afer rotation?

Is there way to get size of the affected rectangle by transformated drawing?
(My controls will not be only rectangle... I want to create several types
but i need to rotate them too)

Thanks

ME
 
B

Bob Powell [MVP]

You cannot rotate the window in which a control is contained. You can rotate
the contents of a control but in cases of TextBox or indeed any control that
is a wrapper round a win32 control you will be unsuccessful.

To rotate the contents of a control you can use the Matrix to transform any
completely custom control drawing.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
G

Guest

Hello Bob,

I have read all the news about this problem...

maybe.. I should to describe more my problem.

I need to have UserControl, that is able to variable rotate...
User control, that overrides OnPaint method.. like this..

protected override void OnPaint(PaintEventArgs e)
{


base.OnPaint(e);

Graphics g = e.Graphics;
g.TranslateTransform(base.ClientSize.Width / 2.0F,
base.ClientSize.Height / 2.0F);
g.RotateTransform(5);
g.DrawRectangle(new Pen(new SolidBrush(Color.Black)), new
Rectangle(0, 0, base.Width - 1, base.Height - 1));
}

After rotation of my drawing, I need more place for this rectangle... can I
get the value for new size of the base control?
Or should I simple use two time longer size of the longer side of my drawing
for the base control?

My control has some initial Size, well.. but if I will rotate my drawing,
how do I get the new required size of my control?
 
B

Bob Powell [MVP]

There are more or less complex mathematical ways to do it but the quick and
dirty method is to create a set of points that correspond to the original
rectangle, transform those points with the desired matrix then get the
bounds of the resulting points by doing a min-max check on all the Xs and
all the Ys.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top