首页 文章

C#画布过度绘画

提问于
浏览
0

我的 MainWindow 中有一个 Canvas ,我在那里划了一条线 . 当它绘制我的 Canvas 的宽度/高度时,绘图将继续在我的 MainWindow 中 . 我的代码中有错误或正常吗?

<Canvas x:Name="coordinateSystem" HorizontalAlignment="Right" Height="580" Margin="0,10,283,0" VerticalAlignment="Top" Width="1024" Cursor="Cross" UseLayoutRounding="False"/>

这是我每次为我的行获得新坐标时调用的函数:

// xOld, yOld and t are static
// t represents the time
private void drawPoly(double value) 
{    
    t++;
    Point pOne = new Point(xOld, yOld);
    Point pTwo = new Point(t, value);

    GeometryGroup lineGroup = new GeometryGroup();
    LineGeometry connectorGeometry = new LineGeometry();
    connectorGeometry.StartPoint = pOne;
    connectorGeometry.EndPoint = pTwo;
    lineGroup.Children.Add(connectorGeometry);
    System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
    path.Data = lineGroup;
    path.StrokeThickness = 1;
    path.Stroke = path.Fill = Brushes.Red;

    coordinateSystem.Children.Add(path);    

    xOld = t;
    yOld = value;
}

谢谢

PS: 有没有办法保存所有绘制点?我想稍后调整我的画布大小(缩小/放大)或者如果时间大到我在画布上移动画线然后我需要再次绘制所有点 .

1 回答

  • 2

    画布不会剪辑子元素 . 如果要阻止子元素被绘制到Canvas之外,则需要将ClipToBounds设置为true或设置Canvas的Clip .

相关问题