How can i rotate (90 degrees) a panel control? i know it is very simple in WPF but i 开发者_如何学运维can't use it. Do you know such a way for WinForm panel control? Thanks you all!
You would have to override the OnPaint, then manually paint all the controls on the panel using GDI. I have never done a rotate, but I have done some custom repaints for things like dropdowns. You will need to write custom OnPaints for each control type on the panel.
So more on this as I just tried it my self... I dont think you can custom paint most of the common controls. WPF is a different animal, and was designed to support this type of thing. When these controls paint, they do so under the covers and there is nothing you can do. I was able to paint and rotate the panel, but I was not able to do other controls like a Check Box.
public class RotatePanel : Panel, IRotate
{
public RotatePanel() : base()
{
Angle = 0;
}
protected override void OnPaint(PaintEventArgs e)
{
using (Graphics g = this.CreateGraphics())
{
foreach (IRotate control in this.Controls)
{
control.Angle = Angle;
}
g.RotateTransform(Angle);
g.DrawRectangle(new System.Drawing.Pen(new SolidBrush(Color.Black), 2f), 4f, 4f, 10f, 10f);
g.DrawRectangle(new System.Drawing.Pen(new SolidBrush(Color.Azure), 2f), 14f, 14f, 30f, 30f);
g.Flush();
}
base.OnPaint(e);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
}
public float Angle
{
get;
set;
}
}
public interface IRotate
{
float Angle { get; set; }
}
public class RotateCheckBox : CheckBox, IRotate
{
public float Angle { get; set; }
public RotateCheckBox():base()
{
Angle = 0;
}
protected override void OnPaint(PaintEventArgs pevent)
{
pevent.Graphics.RotateTransform(this.Angle);
pevent.Graphics.Flush();
base.OnPaint(pevent);
}
}
精彩评论