2016-09-24 62 views
0

需要使用TextShape Class绘制形状。基本上想要在我的TextShape类中重写drawingContext.DrawRoundedRectangle()方法,该方法继承自RectangleBase。如何使用WPF中的TextShape Class绘制自定义形状?

这是我从WPFTutorial.net得到的下面的代码。这里是链接:

http://www.wpftutorial.net/DrawRoundedRectangle.html

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); 
    } 

想要达到的形状这样的: 1)partial half capsule 2)Reverse Partial Half Capsule

基本上我有以在第一个两个形状,它将是一个右边有弧形的矩形。而在第二个形状中,它也将是一个矩形,但是是左侧的一个弧。

请帮助我如何使用.cs类中的DrawRoundedRectangleMine()来创建它。不在xaml中。谢谢。

+0

什么是'RectangleBase'类? – AnjumSKhan

+0

这是一个自定义的基类,我们在子类中保留了很少的属性。但是这在绘制实际几何图形中并没有起到很大的作用。主要方法是drawingContext.DrawRoundedRectangle(FillBrush,BorderPen,new Rect(this.Left,this.Top,this.Right - this.Left,this.Bottom - this.Top),CornerRadius,CornerRadius);但是我们必须重写这个方法,以便我们可以绘制出我们想要的形状,就像我们在上面的图像中所显示的一样。谢谢! –

回答

0

这是你问的:

protected override void OnRender (DrawingContext drawingContext) 
    { 
     double radius = RenderSize.Height/2; 

     var sg = new StreamGeometry(); 
     using (var ctx = sg.Open()) 
     { 
      ctx.BeginFigure (new Point(0, 0), true, true); 
      ctx.LineTo (new Point(RenderSize.Width - radius, 0), true, false); 
      ctx.ArcTo (new Point (RenderSize.Width - radius, RenderSize.Height), new Size (radius, radius), 0, true, SweepDirection.Clockwise, true, false); 
      ctx.LineTo (new Point(0, RenderSize.Height), true, false); 
     } 

     var pen = new Pen (Stroke, StrokeThickness); 

     drawingContext.DrawGeometry (Fill, pen, sg); 
    } 

这几行画正是这里所描述的环境中第一个形状:

How do I get my WPF Custom shape class to render in desiger?

+0

感谢您的回复。 –