WPF使用Path在C#后台使用代碼畫線,動態添加至canvas


PathFigure myPathFigure = new PathFigure();//畫第一條直線
myPathFigure.StartPoint = new Point(0, 0);
LineSegment myLineSegment = new LineSegment();
myLineSegment.Point = new Point(50, 50);
PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
myPathSegmentCollection.Add(myLineSegment);
myPathFigure.Segments = myPathSegmentCollection;

 

PathFigure myPathFigure2 = new PathFigure();//畫第二條直線
myPathFigure2.StartPoint = new Point(0, 0);
LineSegment myLineSegment2 = new LineSegment();
myLineSegment2.Point = new Point(50, 100);
PathSegmentCollection myPathSegmentCollection2 = new PathSegmentCollection();
myPathSegmentCollection2.Add(myLineSegment2);
myPathFigure2.Segments = myPathSegmentCollection2;

 

PathFigureCollection myPathFigureCollection = new PathFigureCollection();
myPathFigureCollection.Add(myPathFigure);//添加畫的線段
myPathFigureCollection.Add(myPathFigure2);

PathGeometry myPathGeometry = new PathGeometry();
myPathGeometry.Figures = myPathFigureCollection;

Path myPath = new Path();
myPath.Stroke = Brushes.Green;//設置Path基礎屬性
myPath.StrokeThickness = 3;
myPath.Data = myPathGeometry;
canvas.Children.Add(myPath);

效果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM