【问题标题】:C# canvas going overpaintedC# 画布重绘
【发布时间】:2013-04-26 17:49:31
【问题描述】:

我的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:有没有办法保存所有绘制的点?我想稍后调整我的画布大小(缩小/放大),或者如果时间要在画布中大幅移动我的画线,然后我需要再次绘制所有点。

【问题讨论】:

    标签: c# wpf user-interface line draw


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多