是否可以在C#中设置矩形角度属性?矩形角度属性
我尝试这样做:
Rectangle r = new Rectangle();
r.Width = 5;
r.Height = 130;
r.Fill = Brushes.Black;
r.RotateTransform.AngleProperty = 30; // Here is the problem
canvas1.Children.Add(r);
是否可以在C#中设置矩形角度属性?矩形角度属性
我尝试这样做:
Rectangle r = new Rectangle();
r.Width = 5;
r.Height = 130;
r.Fill = Brushes.Black;
r.RotateTransform.AngleProperty = 30; // Here is the problem
canvas1.Children.Add(r);
一种方式是一个RotateTransformation应用于对象的RenderTransform
属性:
r.RenderTransform = new RotateTransform(30);
RotateTransform rt1 = new RotateTransform();
rt1.Angle =30;
r.RenderTransform= rt1;
OR
r.RenderTransform= new RotateTransform(30);
您试图设置一个依赖属性,它是只读的,并不意味着的支持领域使用这种方式。
使用正确的属性来代替:
r.RenderTransform.Angle = 30;
而且,我猜想,一个新的Rectangle默认情况下没有一个RotateTransform,所以你可能需要创建一个新的实例,也:
要做到这一点r.RenderTransform= new RotateTransform();
您需要为应用倾斜变换改变矩形角度,看看这个:How to: Skew an Element
昨天我解决了同样的问题。这是我在的WinForms使用:
GraphicsState graphicsState = graphics.Save();
graphics.TranslateTransform(this.Anchor.Position.X, this.Anchor.Position.Y);
graphics.RotateTransform(this.Anchor.Rotation);
graphics.FillRectangle(new SolidBrush(this.Anchor.Color), this.Anchor.GetRelativeBoundingRectangle());
graphics.Restore(graphicsState);
锚是我创建了一个类。从我自己的矩形
internal class Rectangle
{
public PointF Position { get; set; }
public Color Color { get; set; }
public SizeF Size { get; set; }
public float Rotation { get; set; }
public RectangleF GetRelativeBoundingRectangle()
{
return new RectangleF(
new PointF(-this.Size.Width/2.0f, -this.Size.Height/2.0f),
this.Size);
}
}
导出矩形的位置是矩形的中间(中心)点,而不是uppper左上角。
所以要回的第一代码部分:
GraphicsState graphicsState = graphics.Save();
保存我的图形设备的状态,所以我可以做我想做的事情,然后返回到原来的看法。然后我将位置系统转换到矩形的中心并执行旋转
graphics.TranslateTransform(this.Anchor.Position.X, this.Anchor.Position.Y);
graphics.RotateTransform(this.Anchor.Rotation);
然后,我绘制矩形。根据您想要绘制矩形的方式,这部分显然不同。你会想必请使用FillRectangle(像我一样),或者DrawRectangle的该只绘制边界:
graphics.FillRectangle(new SolidBrush(this.Anchor.Color), this.Anchor.GetRelativeBoundingRectangle());
我终于恢复了图形设备,它取消了平移和旋转,我只用于绘制旋转的原始状态矩形
graphics.Restore(graphicsState);