侧边栏壁纸
博主头像
分享你我博主等级

行动起来,活在当下

  • 累计撰写 108 篇文章
  • 累计创建 13 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

WPF 绘制仪表

管理员
2021-10-14 / 0 评论 / 0 点赞 / 2 阅读 / 21556 字
         private int angleNext = 0;
        private int angelCurrent = 0;
        private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            RotateTransform rt = new RotateTransform();
            rt.CenterX = 200;
            rt.CenterY = 200;
            Canvas canvas = (Canvas)sender;
            if (dictionary.Count <= 0)
            {
                foreach (var item in canvas.Children)
                {
                    if ("System.Windows.Shapes.Line".Equals(item.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        var data = (Line)item;
                        dictionary.Add(data.Name, data);
                    }
                    else if ("System.Windows.Controls.TextBlock".Equals(item.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                    }
                    else if ("System.Windows.Controls.Path".Equals(item.ToString(), StringComparison.CurrentCultureIgnoreCase))
                    {
                        ((Path)item).RenderTransform = rt;
                    }
                }
            }
            //this.indicatorPin.RenderTransform = rt;
            angelCurrent = angleNext;
            Random random = new Random();
            angleNext = random.Next(100);
            double timeAnimation = Math.Abs(angelCurrent - angleNext) * 8;
            DoubleAnimation da = new DoubleAnimation(angelCurrent, angleNext,
                new Duration(TimeSpan.FromMilliseconds(timeAnimation)));
            da.AccelerationRatio = 1;
            rt.BeginAnimation(RotateTransform.AngleProperty, da);
            this.currentValueTxtBlock.Text = string.Format("当前值:{0}度", angleNext);
        }
        private int lineNum = 0;
        /// <summary>
        /// 画表盘的刻度(外圈)
        /// </summary>
        private void ExternalScale()
        {
            for (double i = 0; i <= 180; i += 9)
            {
                //添加刻度线
                Line lineScale = new Line();
                var  x1= 200 - 200 * Math.Cos(i * Math.PI / 180);
                var  y1= 200 - 200 * Math.Sin(i * Math.PI / 180);
                lineScale.X1 = x1;
                lineScale.Y1 = y1;
                lineScale.Stroke = new SolidColorBrush(Color.FromRgb(128, 128, 129));
                lineScale.StrokeThickness = 10;
                lineScale.Width = 420;
                lineScale.Height = 400;
                lineScale.Name = "line" + lineNum;
                var x2= 200 - 182 * Math.Cos(i * Math.PI / 180);
                var y2= 200 - 182 * Math.Sin(i * Math.PI / 180);
                lineScale.X2 = x2;
                lineScale.Y2 = y2;
                this.gaugeCanvas.Children.Add(lineScale);
                lineNum = lineNum + 5;
            }
            this.DrawScale();
        }
        private int externalNum = 0;
        /// <summary>
        /// 画表盘的刻度(内圈)
        /// </summary>
        private void DrawScale()
        {
            for (double i = 0; i <= 180; i += 6)
            {
                Ellipse ellipse = new Ellipse();
                if (Convert.ToInt32(i) % 18 == 0)
                {
                    ellipse.Width = 10;
                    ellipse.Height = 10;
                    ellipse.Stroke = new SolidColorBrush(Color.FromRgb(227, 227, 226));
                    ellipse.StrokeThickness = 1;
                    ellipse.Fill = new SolidColorBrush(Color.FromRgb(227, 227, 226));
                    ellipse.SetValue(Canvas.LeftProperty, 200 - 160 * Math.Cos(i * Math.PI / 180));
                    ellipse.SetValue(Canvas.TopProperty, 200 - 160 * Math.Sin(i * Math.PI / 180) - 4.5);
                    //添加刻度值
                    TextBlock txtScale = new TextBlock();
                    txtScale.Text = Convert.ToString(externalNum);
                    txtScale.FontSize = 10;
                    if (i <= 90) //对坐标值进行一定的修正
                    {
                        Canvas.SetLeft(txtScale, 200 - 145 * Math.Cos(i * Math.PI / 180));
                    }
                    else
                    {
                        Canvas.SetLeft(txtScale, 190 - 145 * Math.Cos(i * Math.PI / 180));
                    }
                    Canvas.SetTop(txtScale, 200 - 145 * Math.Sin(i * Math.PI / 180));
                    this.gaugeCanvas.Children.Add(txtScale);
                    externalNum = externalNum + 10;
                }
                else
                {
                    ellipse.Width = 7;
                    ellipse.Height = 7;
                    if (i <= 90)
                    {
                        ellipse.SetValue(Canvas.LeftProperty, 200 - 158 * Math.Cos(i * Math.PI / 180));
                        ellipse.SetValue(Canvas.TopProperty, 200 - 158 * Math.Sin(i * Math.PI / 180) - 4.5);
                    }
                    else if (i > 160)
                    {
                        ellipse.SetValue(Canvas.LeftProperty, 200 - 162 * Math.Cos(i * Math.PI / 180));
                        ellipse.SetValue(Canvas.TopProperty, 200 - 162 * Math.Sin(i * Math.PI / 180) - 4.5);
                    }
                    else
                    {
                        ellipse.SetValue(Canvas.LeftProperty, 200 - 160 * Math.Cos(i * Math.PI / 180));
                        ellipse.SetValue(Canvas.TopProperty, 200 - 160 * Math.Sin(i * Math.PI / 180) - 4.5);
                    }
                    ellipse.Stroke = new SolidColorBrush(Color.FromRgb(227, 227, 226));
                    ellipse.StrokeThickness = 1;
                    ellipse.Fill = new SolidColorBrush(Color.FromRgb(227, 227, 226));
                }
                this.gaugeCanvas.Children.Add(ellipse);
            }
            this.DrawPointer();
        }
        /// <summary>
        /// 画指针
        /// </summary>
        private void DrawPointer()
        {
            PathFigure pathFigure = new PathFigure()
            {
                StartPoint = new Point(200.00, 195.00),
                IsClosed = true
            };
            PathSegmentCollection pathSegmentCollection = new PathSegmentCollection();
            LineSegment lineSegment = new LineSegment(new Point(20, 200), true);
            pathSegmentCollection.Add(lineSegment);
            LineSegment lineSegment1 = new LineSegment(new Point(200, 205), true);
            pathSegmentCollection.Add(lineSegment1);
            pathFigure.Segments = pathSegmentCollection;
            PathFigureCollection myPathFigureCollection = new PathFigureCollection();
            myPathFigureCollection.Add(pathFigure);
            PathGeometry pathGeometry = new PathGeometry();
            pathGeometry.Figures = myPathFigureCollection;
            //this.indicatorPin.Data = pathGeometry;
            Path path = new Path();
            path.Name = "indicatorPin";
            path.Fill = new SolidColorBrush(Color.FromRgb(239, 191, 79));
            path.Data = pathGeometry;
            this.gaugeCanvas.Children.Add(path);
        }

前台代码:

        <Canvas x:Name="gaugeCanvas" Margin="20" MouseDown="Canvas_MouseDown">
           
            <TextBlock x:Name="currentValueTxtBlock" FontSize="20" Canvas.Left="140" Canvas.Top="150"/>
        </Canvas>


0

评论区