| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- using System;
- using System.Windows;
- using System.Windows.Media;
- using System.Windows.Controls;
- using SHJX.Service.Common.UserColor;
- using SHJX.Service.Common.Utils;
- namespace SHJX.Service.Library.Views
- {
- /// <summary>
- /// CycleProcessBar1.xaml 的交互逻辑
- /// </summary>
- public partial class RingProcessBar : UserControl
- {
- public RingProcessBar()
- {
- InitializeComponent();
- SetValueRing2();
- }
- #region 红色进度条是否可见
- public bool RoundVisiable
- {
- get => (bool)GetValue(RoundVisiableProperty);
- set => SetValue(RoundVisiableProperty, value);
- }
- public static readonly DependencyProperty RoundVisiableProperty =
- DependencyProperty.Register(
- "RoundVisiable", typeof(bool),
- typeof(RingProcessBar), new UIPropertyMetadata(true, RoundVisiableChangedCallback)
- );
- private static void RoundVisiableChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs args)
- {
- if (obj is not RingProcessBar control) return;
- var path = control.myCycleProcessBar1;
- path.Visibility = Convert.ToBoolean(args.NewValue) ? Visibility.Visible : Visibility.Hidden;
- }
- #endregion
- #region 中间部分要显示的值
- public string CurrentText
- {
- get => (string)GetValue(CurrentTextProperty);
- set => SetValue(CurrentTextProperty, value);
- }
- public static readonly DependencyProperty CurrentTextProperty =
- DependencyProperty.Register(
- nameof(CurrentText), typeof(string),
- typeof(RingProcessBar), new UIPropertyMetadata(null, CurrentTextChangedCallback)
- );
- private static void CurrentTextChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs args)
- {
- if (obj is not RingProcessBar control) return;
- var path = control.RingName;
- path.Content = args.NewValue;
- }
- #endregion
- #region 红色圆环的值大小
- public static readonly DependencyProperty CurrentCycleValueProperty =
- DependencyProperty.Register(
- nameof(CurrentCycleValue), typeof(double),
- typeof(RingProcessBar), new UIPropertyMetadata(0.1, CurrentCycleValueChangedCallback));
- public double CurrentCycleValue
- {
- get => (double)GetValue(CurrentCycleValueProperty);
- set => SetValue(CurrentCycleValueProperty, value);
- }
- private static void CurrentCycleValueChangedCallback(DependencyObject obj,
- DependencyPropertyChangedEventArgs args)
- {
- if (obj is not RingProcessBar control) return;
- var path = control.myCycleProcessBar1;
- var angel = Convert.ToDouble(args.NewValue) * 360; //角度
- var pathGeometry = new RingProcessBar().GetValue(82, angel);
- //Data赋值
- path.Data = pathGeometry;
- //达到100%则闭合整个
- if (angel.Equals(360))
- {
- path.Data = Geometry.Parse(path.Data.ToString() + " z");
- }
- }
- #endregion
- #region 中间部分的颜色值
- public static readonly DependencyProperty CenterColorProperty =
- DependencyProperty.Register("CenterColor", typeof(string), typeof(RingProcessBar),
- new UIPropertyMetadata(string.Empty, CenterColorChangedCallback)
- );
- public string CenterColor
- {
- get => (string)GetValue(CenterColorProperty);
- set => SetValue(CenterColorProperty, value);
- }
- private static void CenterColorChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs args)
- {
- if (obj is not RingProcessBar control) return;
- var path = control.RingName;
- path.Background = args.NewValue.ToString().ConvertToBrush();
- var path1 = control.myCycleProcessBar2;
- path1.Stroke = args.NewValue.ToString().ConvertToBrush();
- }
- #endregion
- private void SetValueRing2()
- {
- var pathGeometry = GetValue(100, 360);
- //Data赋值
- myCycleProcessBar2.Data = pathGeometry;
- myCycleProcessBar2.Data = Geometry.Parse(myCycleProcessBar2.Data.ToString() + " z");
- }
- protected virtual PathGeometry GetValue(double diameter, double angel)
- {
- /*****************************************
- 方形矩阵边长为90,半长为45
- 环形半径为45,所以距离边框5个像素
- 环形描边5个像素
- ******************************************/
- //起始点
- double leftStart = diameter / 2;
- double topStart = 5;
- double radius = leftStart - topStart; //环形半径
- double endLeft = 0;
- double endTop = 0;
- var isLagreCircle = false; //是否优势弧,即大于180度的弧形
- //小于90度
- if (angel <= 90)
- {
- var ra = (90 - angel) * Math.PI / 180; //弧度
- endLeft = leftStart + Math.Cos(ra) * radius; //余弦横坐标
- endTop = topStart + radius - Math.Sin(ra) * radius; //正弦纵坐标
- }
- else if (angel <= 180)
- {
- var ra = (angel - 90) * Math.PI / 180; //弧度
- endLeft = leftStart + Math.Cos(ra) * radius; //余弦横坐标
- endTop = topStart + radius + Math.Sin(ra) * radius; //正弦纵坐标
- }
- else if (angel <= 270)
- {
- isLagreCircle = true; //优势弧
- double ra = (angel - 180) * Math.PI / 180;
- endLeft = leftStart - Math.Sin(ra) * radius;
- endTop = topStart + radius + Math.Cos(ra) * radius;
- }
- else if (angel < 360)
- {
- isLagreCircle = true; //优势弧
- double ra = (angel - 270) * Math.PI / 180;
- endLeft = leftStart - Math.Cos(ra) * radius;
- endTop = topStart + radius - Math.Sin(ra) * radius;
- }
- else
- {
- isLagreCircle = true; //优势弧
- endLeft = leftStart - 0.001; //不与起点在同一点,避免重叠绘制出非环形
- endTop = topStart;
- }
- var arcEndPt = new Point(endLeft, endTop); //结束点
- var arcSize = new Size(radius, radius);
- var direction = SweepDirection.Clockwise; //顺时针弧形
- //弧形
- var arcsegment = new ArcSegment(arcEndPt, arcSize, 0, isLagreCircle, direction, true);
- //形状集合
- var pathsegmentCollection = new PathSegmentCollection
- {
- arcsegment
- };
- //路径描述
- var pathFigure = new PathFigure
- {
- StartPoint = new Point(leftStart, topStart),
- Segments = pathsegmentCollection
- };
- //起始地址
- //路径描述集合
- var pathFigureCollection = new PathFigureCollection();
- pathFigureCollection.Add(pathFigure);
- //复杂形状
- var pathGeometry = new PathGeometry
- {
- Figures = pathFigureCollection
- };
- return pathGeometry;
- }
- }
- }
|