2013-01-17 83 views
0

我正在構建一個應用程序,我必須在畫布上繪製自己的圖形。現在我這樣做:在WPF中繪製數字

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

您可以查看[StreamGeometry](http://msdn.microsoft.com/en-us/library/system.windows.media.streamgeometry.aspx)。它是PathGeometry的輕量級替代品。 – Clemens

回答

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); 

你當然可以把整個東西的方法或將點陣列,但對於這樣的小功能看起來很傻......如果你有更多的觀點,它可能是有道理的。