2016-09-24 5 views
0

TextShapeクラスを使用して図形を描画する必要があります。基本的には、RectangleBaseを継承するTextShapeクラス内でdrawingContext.DrawRoundedRectangle()メソッドをオーバーライドしたいと考えています。WPFでTextShapeクラスを使用してカスタマイズした図形を描画する方法は?

これは私が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

基本的に私は最初の1で2つの形状を作成する必要があります。ここのリンクです右に弧を描いた四角形になります。また、第2の形状では、矩形になりますが、左に弧があります。

DrawRoundedRectangleMine()を.csクラスに使用して作成する方法を教えてください。 xamlにはありません。ありがとう。

+0

'RectangleBase'クラスとは何ですか? – AnjumSKhan

+0

これは子クラスで使用するプロパティをほとんど保持していないカスタム基本クラスです。しかし、それは実際のジオメトリを描くのにあまり重要な役割を果たしません。主なメソッドは、drawingContext.DrawRoundedRectangle(FillBrush、BorderPen、新しい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

お返事ありがとうございます。 –

関連する問題