基础示例
参考:How to: Create a LineSegment in a PathGeometry
绘制直线(纯xaml)
<Path Stroke="Black" StrokeThickness="1">
<Path.Data>
<PathGeometry>
<PathFigure StartPoint="10,50">
<LineSegment Point="200,70" />
</PathFigure>
</PathGeometry>
</Path.Data>
</Path>
绘制直线(纯c#)
PathFigure myPathFigure = new PathFigure();
myPathFigure.StartPoint = new Point(10, 50);
LineSegment myLineSegment = new LineSegment();
myLineSegment.Point = new Point(200, 70);
PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
myPathSegmentCollection.Add(myLineSegment);
myPathFigure.Segments = myPathSegmentCollection;
PathFigureCollection myPathFigureCollection = new PathFigureCollection();
myPathFigureCollection.Add(myPathFigure);
PathGeometry myPathGeometry = new PathGeometry();
myPathGeometry.Figures = myPathFigureCollection;
Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
myPath.Data = myPathGeometry;
我的练习
差不多要画成下面的样子(我的项目里要求叠放多层,没啥,循环一下即可)。。。刻度就是画直线,没啥好说的,算好坐标就行了。
下面代码中的xi与yi参考下图。红色箭头是组合的方向。d是两条弧之间的距离。
具体坐标计算不写了,就是三角函数的事情。
bool isLargeArc = false;
if (Math.Abs(angle2-angle1) > 180) {
isLargeArc = true;
}
LineSegment line1 = new LineSegment();
PathFigure pthFigure = new PathFigure();
// 起点
pthFigure.StartPoint = new Point(x0, y0);
LineSegment lineSeg = new LineSegment();
lineSeg.Point = new Point(x1, y1); // 第一条线的终点
PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
// 第一条线画完了
myPathSegmentCollection.Add(lineSeg);
// 外层圆弧
// 坐标是弧的终点,Size(width,height),角度单位是°,isLargeArc表示是否超过180,顺/逆时针,isStroked
ArcSegment arcSegment1 = new ArcSegment(new Point(x2,y2), new Size(curRadius1, curRadius1), (angle2-angle1),
isLargeArc, SweepDirection.Clockwise, true);
myPathSegmentCollection.Add(arcSegment1);
// 第二条直线
LineSegment line2 = new LineSegment();
line2.Point = new Point(x3, y3);
myPathSegmentCollection.Add(line2);
// 内层圆弧
// 因为是反方向,所以角度是360-(angle2-angle1),并且是逆时针
ArcSegment arcSegment2 = new ArcSegment(new Point(x0, y0), new Size(curRadius2, curRadius2),(360-angle2+angle1),
isLargeArc, SweepDirection.Counterclockwise, true);
myPathSegmentCollection.Add(arcSegment2);
// 组装上去
pthFigure.Segments = myPathSegmentCollection;
PathFigureCollection pthFigureCollection = new PathFigureCollection();
pthFigureCollection.Add(pthFigure);
PathGeometry pthGeometry = new PathGeometry();
pthGeometry.Figures = pthFigureCollection;
Path path = new Path();
// 线条的颜色
path.Stroke = new SolidColorBrush(Colors.Black);
path.StrokeThickness = 2;
path.Data = pthGeometry;
// 填充色
path.Fill = new SolidColorBrush(Colors.Yellow);
// 前台我放了一个Canvas对象,叫circleBone
this.circleBone.Children.Add(path);
如何指定颜色(c#)
我自定义的Range类有个string类型的Color属性,ranges是List< Range >类型的变量。
那么Color属性可以取值"AntiqueWhite"这种名称,也可以取值 #FFD3CFC7 这种数值。
path.Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString(ranges[k].Color));