/// <summary> /// 绘制刻度 /// </summary> /// <param name="size"></param> private void PaintCalibration(Size size) { try { var width = size.Width; var maximum = Maximum; var minimum = Minimum; var mainScale = MainScale; var viceScale = ViceScale; var decimalDigits = DecimalDigits; // 小数点位 var beginAngle = 1.75d * Math.PI; var avgMainAngle = 1.5d * Math.PI / mainScale; var avgViceAngle = avgMainAngle / viceScale; var avg = (maximum - minimum) / (double)mainScale; var fontSize = width * 27d / 518d; if (decimalDigits < 0) { decimalDigits = 0; } else if (decimalDigits > 7) { decimalDigits = 7; } _calibrationCanvas.Children.Clear(); var brush1 = new SolidColorBrush(Colors.Black); var brush2 = new SolidColorBrush(Color.FromArgb(0xff, 0x7C, 0x73, 0x63)); for (int i = 0; i <= mainScale; i++) { var mainAngle = beginAngle - avgMainAngle * i; var mainLine = new Line(); mainLine.Stroke = brush1; mainLine.StrokeThickness = 2d; mainLine.X1 = width * Math.Sin(mainAngle) * 163d / 518d + width * 0.5d; mainLine.Y1 = width * Math.Cos(mainAngle) * 163d / 518d + width * 0.5d; mainLine.X2 = width * Math.Sin(mainAngle) * 181d / 518d + width * 0.5d; mainLine.Y2 = width * Math.Cos(mainAngle) * 181d / 518d + width * 0.5d; _calibrationCanvas.Children.Add(mainLine); for (int j = 1; j < viceScale && i < mainScale; j++) { var viceAngle = mainAngle - j * avgViceAngle; var viceLine = new Line(); viceLine.Stroke = brush2; viceLine.StrokeThickness = 1d; viceLine.X1 = width * Math.Sin(viceAngle) * 163d / 518d + width * 0.5d; viceLine.Y1 = width * Math.Cos(viceAngle) * 163d / 518d + width * 0.5d; viceLine.X2 = width * Math.Sin(viceAngle) * 176d / 518d + width * 0.5d; viceLine.Y2 = width * Math.Cos(viceAngle) * 176d / 518d + width * 0.5d; _calibrationCanvas.Children.Add(viceLine); } var text = new TextBlock(); text.FontSize = fontSize; text.Text = Math.Round(minimum + i * avg, decimalDigits).ToString(); Size textSize = text.MeasureTextSize(); text.SetValue(Canvas.LeftProperty, width * Math.Sin(mainAngle) * 218d / 518d - textSize.Width / 2d + width * 0.5d); text.SetValue(Canvas.TopProperty, width * Math.Cos(mainAngle) * 218d / 518d - textSize.Height / 2d + width * 0.5d); _calibrationCanvas.Children.Add(text); } PaintPoint(size); } catch { } }
private void Paint(Size finalSize) { //maxValue = 100; var circleWidth = 4d; var pointWidth = 4d; var keduWidth = 1d; var keduLength = 5d; var standWidth = 3d; var startDegree = 290d; var standLength = 10d; var minValue = MinValue; var maxValue = MaxValue; if (minValue >= maxValue) return; //if (currenValue < minValue || currenValue > maxValue) //{ // currenValue = (minValue + maxValue) / 2; //} var widthSpan = 40d; var heightSpan = 20d; var width = finalSize.Width; var height = finalSize.Height; if (width < 100d || height < 50d) { this.Width = width = 100d; this.Height = height = 50d; } var centerX = (width - widthSpan) / 2d + heightSpan; var centerY = height - heightSpan + 10d; //Single MyWidth, MyHeight; //MyWidth = Width - widthSpan; //MyHeight = Height - heightSpan; //Single DI = 2 * MyHeight; //if (MyWidth < MyHeight) //{ // DI = MyWidth; //} var degreePerStandKeDu = (720d - 2d * startDegree) / 5d; //计算出每个标准大刻度之间的角度 var degreePerKeDu = degreePerStandKeDu / 5d; var valuePerKeDu = (maxValue - minValue) / 5; //每个大刻度之间的值 //Rectangle MyRect; //g = e.Graphics; //g.Clear(this.BackColor); //g.DrawRectangle(new Pen(Color.Black, 2), 2, 2, this.Width - 4, this.Height - 4); _background.SetValue(WidthProperty, width); _background.SetValue(HeightProperty, height); //MyRect = new Rectangle(0, 0, this.Width, this.Height); //g.SmoothingMode = SmoothingMode.AntiAlias; ////将绘图平面的坐标原点移到窗口中心 //g.TranslateTransform(MyWidth / 2 + heightSpan, MyHeight + 10); //绘制表盘 //g.FillEllipse(Brushes.Black,MyRect); //string word = currenValue.ToString().Trim() + title; //int len = ((word.Trim().Length) * (myFont.Height)) / 2; //g.DrawString(word.Trim(), myFont, Brushes.Red, new PointF(-len / 2, -MyHeight / 2 - myFont.Height / 2)); Size textSize = _text.MeasureTextSize(); _text.SetValue(Canvas.LeftProperty, centerX - textSize.Width / 2d); _text.SetValue(Canvas.TopProperty, centerY - (height - heightSpan - textSize.Height) / 2d); //g.RotateTransform(startDegree); //绘制刻度标记 _calibrationCanvas.Children.Clear(); var calibrationBrush = new SolidColorBrush(Colors.Green); _calibrationCanvas.Background = calibrationBrush; var calibrationLeft = centerX - 2d; var calibrationTop = centerY + 2d - height + heightSpan; var calibrationDegree = startDegree; var origin = new Point(0.5d, (centerY - calibrationTop) / keduLength); for (int x = 0; x < 26; x++) { //g.FillRectangle(Brushes.Green, new Rectangle(-2, (System.Convert.ToInt16(DI) / 2 - 2) * (-1), keduWidth, keduLength)); //g.RotateTransform(degreePerKeDu); var calibrationLine = new Rectangle(); calibrationLine.SetValue(Canvas.LeftProperty, calibrationLeft); calibrationLine.SetValue(Canvas.TopProperty, calibrationTop); calibrationLine.SetValue(WidthProperty, keduWidth); calibrationLine.SetValue(HeightProperty, keduLength); calibrationLine.Fill = calibrationBrush; calibrationLine.RenderTransformOrigin = origin; calibrationLine.RenderTransform = new RotateTransform() { Angle = calibrationDegree, }; _calibrationCanvas.Children.Add(calibrationLine); calibrationDegree += degreePerKeDu; } ////重置绘图平面的坐标变换 //g.ResetTransform(); //g.TranslateTransform(MyWidth / 2 + heightSpan, MyHeight + 10); //g.RotateTransform(startDegree); //mySpeed = minValue; ////绘制刻度值 var speed = minValue; var labelBrush = new SolidColorBrush(Colors.Red); var lableLineLeft = centerX - 3d; var lableLineTop = centerY + 2d - height + heightSpan; var labelTop = centerY - (height - heightSpan) + 26d; var lableLineDegree = startDegree; var lableLineOrigin = new Point(0.5d, (centerY - lableLineTop) / standLength); for (int x = 0; x < 6; x++) { //myString = mySpeed.ToString(); ////绘制红色刻度 //g.FillRectangle(Brushes.Red, new Rectangle(-3, (System.Convert.ToInt16(DI) / 2 - 2) * (-1), standWidth, standLength)); ////绘制数值 //g.DrawString(myString, myFont, Brushes.Green, new PointF(myString.Length * (-6), -MyHeight + 26)); ////旋转45度 //g.RotateTransform(degreePerStandKeDu); //mySpeed = mySpeed + valuePerKeDu; var lableLine = new Rectangle(); lableLine.SetValue(Canvas.LeftProperty, lableLineLeft); lableLine.SetValue(Canvas.TopProperty, lableLineTop); lableLine.SetValue(WidthProperty, standWidth); lableLine.SetValue(HeightProperty, standLength); lableLine.Fill = labelBrush; lableLine.RenderTransformOrigin = lableLineOrigin; lableLine.RenderTransform = new RotateTransform() { Angle = lableLineDegree, }; _calibrationCanvas.Children.Add(lableLine); //绘制数值 var grid = new Grid(); var label = new TextBlock(); label.Text = speed.ToString(); label.FontSize = this.FontSize; label.FontFamily = this.FontFamily; //label.Foreground = calibrationBrush; label.Foreground = this.Foreground; Size labelSize = label.MeasureTextSize(); grid.SetValue(Canvas.LeftProperty, centerX - labelSize.Width / 2d); grid.SetValue(Canvas.TopProperty, labelTop); grid.RenderTransformOrigin = new Point(0.5d, (centerY - labelTop) / labelSize.Height); grid.RenderTransform = new RotateTransform() { Angle = lableLineDegree, }; grid.Children.Add(label); _calibrationCanvas.Children.Add(grid); lableLineDegree += degreePerStandKeDu; speed += valuePerKeDu; } ////重置绘图平面坐标变换 //g.ResetTransform(); //g.TranslateTransform(MyWidth / 2 + heightSpan, MyHeight + 10); ////绘制指针在speed的情形 var currenValue = CurrenValue; int tureValue = currenValue; if (currenValue < minValue) { tureValue = minValue - 1; } else if (currenValue > maxValue) { tureValue = maxValue + 1; } //g.RotateTransform((float)((tureValue - minValue) * (720 - 2 * startDegree) / (maxValue - minValue) + startDegree)); ////设置线帽 //myPen = new Pen(Color.Blue, pointWidth); //myPen.EndCap = LineCap.ArrowAnchor; ////绘制指针 //g.DrawLine(myPen, new PointF(0, 0), new PointF((float)0, (float)((-1) * (MyHeight / 1.25)))); var pointerLine = new Line(); pointerLine.Stroke = new SolidColorBrush(Colors.Blue); pointerLine.StrokeThickness = pointWidth; //_pointerLine.X1 = 0; //_pointerLine.Y1 = 0; //_pointerLine.X2 = 0; pointerLine.Y2 = (height - heightSpan) / 1.25d; pointerLine.StrokeEndLineCap = PenLineCap.Triangle; pointerLine.RenderTransformOrigin = new Point(0.5d, 0d); pointerLine.RenderTransform = new RotateTransform(); pointerLine.SetValue(Canvas.LeftProperty, centerX); pointerLine.SetValue(Canvas.TopProperty, centerY - pointerLine.Y2); var arrowFigure = new PathFigure(); arrowFigure.StartPoint = new Point(centerX, centerY - pointerLine.Y2 - pointWidth); arrowFigure.Segments.Add(new LineSegment() { Point = new Point(centerX - pointWidth,centerY- pointerLine.Y2) }); arrowFigure.Segments.Add(new LineSegment() { Point = new Point(centerX + pointWidth, centerY -pointerLine.Y2) }); var arrowGeometry = new PathGeometry(); arrowGeometry.Figures.Add(arrowFigure); var arrowPath = new Path(); arrowPath.Fill = pointerLine.Stroke; arrowPath.Stroke = pointerLine.Stroke; arrowPath.Data = arrowGeometry; _pointCanvas.Children.Clear(); _pointCanvas.Height = centerY; _pointCanvas.Width = width; _pointCanvas.Children.Add(arrowPath); _pointCanvas.Children.Add(pointerLine); PaintPointer(); _axisOuterEllipse.Fill = new SolidColorBrush(Colors.Black); _axisOuterEllipse.SetValue(Canvas.LeftProperty, centerX - 7d); _axisOuterEllipse.SetValue(Canvas.TopProperty, centerY - 7d); _axisOuterEllipse.SetValue(WidthProperty, 14d); _axisOuterEllipse.SetValue(HeightProperty, 14d); _axisInnerEllipse.Fill = new SolidColorBrush(Colors.Red); _axisInnerEllipse.SetValue(Canvas.LeftProperty, centerX - 5d); _axisInnerEllipse.SetValue(Canvas.TopProperty, centerY - 5d); _axisInnerEllipse.SetValue(WidthProperty, 10d); _axisInnerEllipse.SetValue(HeightProperty, 10d); var radian = 7d * Math.PI / 18d; var radius = centerY - calibrationTop; var offsetX = radius * Math.Sin(radian); var offsetY = radius * Math.Cos(radian); var arcSegment = new ArcSegment(); arcSegment.IsLargeArc = false; arcSegment.SweepDirection = SweepDirection.Clockwise; arcSegment.Point = new Point(centerX + offsetX, centerY - offsetY); arcSegment.Size = new Size(radius, radius); var figure = new PathFigure(); figure.StartPoint = new Point(centerX - offsetX, centerY - offsetY); figure.Segments.Add(arcSegment); var pathGeometry = new PathGeometry(); pathGeometry.Figures.Add(figure); _arcPath.Stroke = new SolidColorBrush(Color.FromArgb(0xff, 0xBC,0x8F,0x8F)); _arcPath.StrokeThickness = circleWidth; _arcPath.Data = pathGeometry; //_arcPath.SetValue(Canvas.LeftProperty, centerX - 100d); //_arcPath.SetValue(Canvas.TopProperty, centerY); //g.ResetTransform(); //g.TranslateTransform(MyWidth / 2 + heightSpan, MyHeight + 10); ////绘制中心点 //g.FillEllipse(Brushes.Black, -7, -7, 14, 14); //g.FillEllipse(Brushes.Red, -5, -5, 10, 10); ////绘制外圆 //myPen = new Pen(Color.RosyBrown, circleWidth); //g.DrawArc(myPen, -MyWidth / 2, -MyHeight, MyWidth, 2 * MyHeight, 200, 140); ////g.DrawEllipse(myPen, -MyWidth / 2, -MyHeight, MyWidth, 2 * MyHeight); }
/// <summary> /// 绘制刻度 /// </summary> /// <param name="size"></param> private void PaintCalibration(Size size) { try { var width = size.Width; var maximum = Maximum; var minimum = Minimum; var mainScale = MainScale; var viceScale = ViceScale + 1; var decimalDigits = DecimalDigits; // 小数点位 var beginAngle = 236.25d * Math.PI / 180d; var avgMainAngle = 112.5 * Math.PI / (180d * mainScale); var avgViceAngle = avgMainAngle / (viceScale); var avg = (maximum - minimum) / (double)mainScale; var fontSize = width * 36d / 657d; if (decimalDigits < 0) { decimalDigits = 0; } else if (decimalDigits > 7) { decimalDigits = 7; } _calibrationCanvas.Children.Clear(); var brush = new SolidColorBrush(Colors.Black); for (int i = 0; i <= mainScale; i++) { var mainAngle = beginAngle - avgMainAngle * i; var mainLine = new Line(); mainLine.Stroke = brush; mainLine.StrokeThickness = 2d; mainLine.X1 = Math.Sin(mainAngle) * width * 346d / 657d + width * 0.5d; mainLine.Y1 = Math.Cos(mainAngle) * width * 346d / 657d + width * 469d / 657d; mainLine.X2 = Math.Sin(mainAngle) * width * 303d / 657d + width * 0.5d; mainLine.Y2 = Math.Cos(mainAngle) * width * 303d / 657d + width * 469d / 657d; _calibrationCanvas.Children.Add(mainLine); for (int j = 1; j < viceScale && i < mainScale; j++) { var viceAngle = mainAngle - j * avgViceAngle; var viceLine = new Line(); viceLine.Stroke = brush; viceLine.StrokeThickness = 1d; viceLine.X1 = Math.Sin(viceAngle) * width * 342d / 657d + width * 0.5d; viceLine.Y1 = Math.Cos(viceAngle) * width * 342d / 657d + width * 469d / 657d; viceLine.X2 = Math.Sin(viceAngle) * width * 303d / 657d + width * 0.5d; viceLine.Y2 = Math.Cos(viceAngle) * width * 303d / 657d + width * 469d / 657d; _calibrationCanvas.Children.Add(viceLine); } var text = new TextBlock(); text.FontSize = fontSize; text.Foreground = brush; text.Text = Math.Round(minimum + i * avg, decimalDigits).ToString(); Size textSize = text.MeasureTextSize(); text.SetValue(Canvas.LeftProperty, Math.Sin(mainAngle) * width * 380d / 657d - textSize.Width / 2d + width * 0.5d); text.SetValue(Canvas.TopProperty, Math.Cos(mainAngle) * width * 380d / 657d - textSize.Height / 2d + width * 469d / 657d); _calibrationCanvas.Children.Add(text); } PaintPoint(size); } catch { } }
/// <summary> /// 绘制刻度 /// </summary> /// <param name="size"></param> private void PaintCalibration(Size size) { try { var width = size.Width; var maximum = Maximum; var minimum = Minimum; var mainScale = MainScale; var decimalDigits = DecimalDigits; // 小数点位 var beginAngle = 1.75d * Math.PI; var avgMainAngle = 1.5d * Math.PI / mainScale; var avg = (maximum - minimum) / (double)mainScale; var fontSize = width * 37d / 633d; if (decimalDigits < 0) { decimalDigits = 0; } else if (decimalDigits > 7) { decimalDigits = 7; } _calibrationCanvas.Children.Clear(); var brush1 = new SolidColorBrush(Colors.Black); var brush2 = new SolidColorBrush(Colors.White); for (int i = 0; i <= mainScale; i++) { var mainAngle = beginAngle - avgMainAngle * i; var mainLine = new Line(); mainLine.Stroke = brush1; mainLine.StrokeThickness = 2d; mainLine.X1 = width * Math.Sin(mainAngle) * 222.5d / 633d + width * 0.5d; mainLine.Y1 = width * Math.Cos(mainAngle) * 222.5d / 633d + width * 0.5d; mainLine.X2 = width * Math.Sin(mainAngle) * 197.5d / 633d + width * 0.5d; mainLine.Y2 = width * Math.Cos(mainAngle) * 197.5d / 633d + width * 0.5d; _calibrationCanvas.Children.Add(mainLine); var text = new TextBlock(); text.FontSize = fontSize; text.Foreground = brush2; text.Text = Math.Round(minimum + i * avg, decimalDigits).ToString(); Size textSize = text.MeasureTextSize(); text.SetValue(Canvas.LeftProperty, width * Math.Sin(mainAngle) * 267d / 633d - textSize.Width / 2d + width * 0.5d); text.SetValue(Canvas.TopProperty, width * Math.Cos(mainAngle) * 267d / 633d - textSize.Height / 2d + width * 0.5d); _calibrationCanvas.Children.Add(text); } PaintPoint(size); } catch { } }