使用OnRender绘制画布与绘图

时间:2016-05-04 14:55:08

标签: c# wpf wpf-controls

我需要使用WPF绘制形状。我已经想出了两种方法。
方法1

protected override void OnRender(DrawingContext drawingContext)
    {
        base.OnRender(drawingContext);
        Rect rect = new Rect(new System.Windows.Point(50, 50), new System.Windows.Size(400, 100));
        drawingContext.DrawRectangle(System.Windows.Media.Brushes.Aqua, (System.Windows.Media.Pen) null, rect);
    }

方法2

    public void Draw()
    {
         DrawingVisual drawingVisual = new DrawingVisual();
         DrawingContext drawingContext = drawingVisual.RenderOpen();
         Rect rect = new Rect(new System.Windows.Point(50, 50), new System.Windows.Size(400, 100));
         drawingContext.DrawRectangle(System.Windows.Media.Brushes.Aqua, (System.Windows.Media.Pen) null, rect);
         drawingContext.Close();
         canvas.Children.Add(new VisualHost { Visual = drawingVisual });
    }
      public class VisualHost : UIElement
     {
         public Visual Visual { get; set; }
         protected override int VisualChildrenCount
         {get { return Visual != null ? 1 : 0; } }
         protected override Visual GetVisualChild(int index)
         {return Visual;}
     }

这些有何不同?能够使用这两种方法绘制和显示。由于OnRender提供了覆盖,与绘制函数相比,它会绘制更多的绘图吗?我何时应该使用另一个?请指导。 TIA

0 个答案:

没有答案