画刻度
private void DrawScale()
{
for (int i = 0; i <= 180; i += 5)
{
//添加刻度线
Line lineScale = new Line();
//大刻度
if (i % 25 == 0)
{
lineScale.X1 = 200 - 160 * Math.Cos(i * Math.PI / 180);
lineScale.Y1 = 200 - 160 * Math.Sin(i * Math.PI / 180);
}
else//小刻度
{
lineScale.X1 = 200 - 170 * Math.Cos(i * Math.PI / 180);
lineScale.Y1 = 200 - 170 * Math.Sin(i * Math.PI / 180);
}
//直线刻度的终点,注意角度转为弧度制
lineScale.X2 = 200 - 180 * Math.Cos(i * Math.PI / 180);
lineScale.Y2 = 200 - 180 * Math.Sin(i * Math.PI / 180);
//将直线画在Canvas画布上
this.gaugeCanvas.Children.Add(lineScale);
}
}
指针
<Path x:Name="indicatorPin" Fill="Orange">
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<PathFigure StartPoint="200,195" IsClosed="True">
<PathFigure.Segments>
<LineSegment Point="20,200"/>
<LineSegment Point="200,205"/>
</PathFigure.Segments>
</PathFigure>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>
更新刻度值
RotateTransform rt = new RotateTransform();
rt.CenterX = 200;
rt.CenterY = 200;
rt.Angle = DateTime.Now.Second+new Random().Next(1,100);
this.indicatorpin.RenderTransform = rt;