在WPF中绘制数字

时间:2013-01-17 06:39:00

标签: c# wpf canvas

我正在构建一个应用程序,我必须在Canvas上绘制自己的数字。现在我这样做:

PathFigure figure = new PathFigure() { StartPoint = new Point(0, TurboHeight / turboSizeFactor * turboSchaufelFactor), IsClosed = true};
LineSegment seg = new LineSegment() { Point = new Point(turboWidth, TurboHeight / turboSizeFactor * turboSchaufelFactor) };
figure.Segments.Add(seg);
seg = new LineSegment(){Point=new Point(turboWidth*0.85, 0)};
figure.Segments.Add(seg);
seg = new LineSegment() { Point = new Point(turboWidth * 0.15, 0) };
figure.Segments.Add(seg);
PathGeometry geo = new PathGeometry();
geo.Figures.Add(figure);
Path path = new Path() { Data = geo, Stroke = Brushes.Black, StrokeThickness = 1, SnapsToDevicePixels = true, Fill = Brushes.LightGray};
CnvMain.Children.Add(path);

这是做到这一点的方式还是有一种更简单的方法。我的意思是它为画布上的几行创建了很多对象。

感谢。

1 个答案:

答案 0 :(得分:1)

对我而言,就像你没有浪费太多。我唯一想做的就是不将所有中间结果存储在您重复使用的变量中......例如:

PathFigure figure = new PathFigure() 
{ 
    StartPoint = new Point(0, TurboHeight / turboSizeFactor * turboSchaufelFactor),
    IsClosed = true
};


figure.Segments.Add(new LineSegment() { Point = new Point(turboWidth, TurboHeight / turboSizeFactor * turboSchaufelFactor) });
figure.Segments.Add(new LineSegment() { Point = new Point(turboWidth * 0.85, 0) });
figure.Segments.Add(new LineSegment() { Point = new Point(turboWidth * 0.15, 0) });

PathGeometry geo = new PathGeometry();
geo.Figures.Add(figure);

Path path = new Path() { Data = geo, Stroke = Brushes.Black, StrokeThickness = 1, SnapsToDevicePixels = true, Fill = Brushes.LightGray };

CnvMain.Children.Add(path);

你当然可以将整个东西放在一个方法中,或者把点放在一个数组中,但对于这样一个看似愚蠢的小函数......如果你有更多的分数,它可能是有道理的。