需要使用TextShape Class绘制形状。基本上想要在我的TextShape类中重写drawingContext.DrawRoundedRectangle()方法,该方法继承自RectangleBase。如何使用WPF中的TextShape Class绘制自定义形状?
这是我从WPFTutorial.net得到的下面的代码。这里是链接:
public void DrawRoundedRectangleMine(DrawingContext dc, Brush brush, Pen pen, Rect rect, CornerRadius cornerRadius)
{
var geometry = new StreamGeometry();
using (var context = geometry.Open())
{
bool isStroked = pen != null;
const bool isSmoothJoin = true;
context.BeginFigure(rect.TopLeft + new Vector(0, cornerRadius.TopLeft), brush != null, true);
context.ArcTo(new Point(rect.TopLeft.X + cornerRadius.TopLeft, rect.TopLeft.Y),
new Size(cornerRadius.TopLeft, cornerRadius.TopLeft),
40, false, SweepDirection.Clockwise, isStroked, isSmoothJoin);
context.LineTo(rect.TopRight - new Vector(cornerRadius.TopRight, 0), isStroked, isSmoothJoin);
context.ArcTo(new Point(rect.TopRight.X, rect.TopRight.Y + cornerRadius.TopRight),
new Size(cornerRadius.TopRight, cornerRadius.TopRight),
40, false, SweepDirection.Clockwise, isStroked, isSmoothJoin);
context.LineTo(rect.BottomRight - new Vector(0, cornerRadius.BottomRight), isStroked, isSmoothJoin);
context.ArcTo(new Point(rect.BottomRight.X - cornerRadius.BottomRight, rect.BottomRight.Y),
new Size(cornerRadius.BottomRight, cornerRadius.BottomRight),
40, false, SweepDirection.Clockwise, isStroked, isSmoothJoin);
context.LineTo(rect.BottomLeft + new Vector(cornerRadius.BottomLeft, 0), isStroked, isSmoothJoin);
context.ArcTo(new Point(rect.BottomLeft.X, rect.BottomLeft.Y - cornerRadius.BottomLeft),
new Size(cornerRadius.BottomLeft, cornerRadius.BottomLeft),
40, false, SweepDirection.Clockwise, isStroked, isSmoothJoin);
context.Close();
}
dc.DrawGeometry(brush, pen, geometry);
}
基本上我有以在第一个两个形状,它将是一个右边有弧形的矩形。而在第二个形状中,它也将是一个矩形,但是是左侧的一个弧。
请帮助我如何使用.cs类中的DrawRoundedRectangleMine()来创建它。不在xaml中。谢谢。
什么是'RectangleBase'类? – AnjumSKhan
这是一个自定义的基类,我们在子类中保留了很少的属性。但是这在绘制实际几何图形中并没有起到很大的作用。主要方法是drawingContext.DrawRoundedRectangle(FillBrush,BorderPen,new Rect(this.Left,this.Top,this.Right - this.Left,this.Bottom - this.Top),CornerRadius,CornerRadius);但是我们必须重写这个方法,以便我们可以绘制出我们想要的形状,就像我们在上面的图像中所显示的一样。谢谢! –