RingProcessBar.xaml.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Media;
  4. using System.Windows.Controls;
  5. using SHJX.Service.Common.UserColor;
  6. using SHJX.Service.Common.Utils;
  7. namespace SHJX.Service.Library.Views
  8. {
  9. /// <summary>
  10. /// CycleProcessBar1.xaml 的交互逻辑
  11. /// </summary>
  12. public partial class RingProcessBar : UserControl
  13. {
  14. public RingProcessBar()
  15. {
  16. InitializeComponent();
  17. SetValueRing2();
  18. }
  19. #region 红色进度条是否可见
  20. public bool RoundVisiable
  21. {
  22. get => (bool)GetValue(RoundVisiableProperty);
  23. set => SetValue(RoundVisiableProperty, value);
  24. }
  25. public static readonly DependencyProperty RoundVisiableProperty =
  26. DependencyProperty.Register(
  27. "RoundVisiable", typeof(bool),
  28. typeof(RingProcessBar), new UIPropertyMetadata(true, RoundVisiableChangedCallback)
  29. );
  30. private static void RoundVisiableChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs args)
  31. {
  32. if (obj is not RingProcessBar control) return;
  33. var path = control.myCycleProcessBar1;
  34. path.Visibility = Convert.ToBoolean(args.NewValue) ? Visibility.Visible : Visibility.Hidden;
  35. }
  36. #endregion
  37. #region 中间部分要显示的值
  38. public string CurrentText
  39. {
  40. get => (string)GetValue(CurrentTextProperty);
  41. set => SetValue(CurrentTextProperty, value);
  42. }
  43. public static readonly DependencyProperty CurrentTextProperty =
  44. DependencyProperty.Register(
  45. nameof(CurrentText), typeof(string),
  46. typeof(RingProcessBar), new UIPropertyMetadata(null, CurrentTextChangedCallback)
  47. );
  48. private static void CurrentTextChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs args)
  49. {
  50. if (obj is not RingProcessBar control) return;
  51. var path = control.RingName;
  52. path.Content = args.NewValue;
  53. }
  54. #endregion
  55. #region 红色圆环的值大小
  56. public static readonly DependencyProperty CurrentCycleValueProperty =
  57. DependencyProperty.Register(
  58. nameof(CurrentCycleValue), typeof(double),
  59. typeof(RingProcessBar), new UIPropertyMetadata(0.1, CurrentCycleValueChangedCallback));
  60. public double CurrentCycleValue
  61. {
  62. get => (double)GetValue(CurrentCycleValueProperty);
  63. set => SetValue(CurrentCycleValueProperty, value);
  64. }
  65. private static void CurrentCycleValueChangedCallback(DependencyObject obj,
  66. DependencyPropertyChangedEventArgs args)
  67. {
  68. if (obj is not RingProcessBar control) return;
  69. var path = control.myCycleProcessBar1;
  70. var angel = Convert.ToDouble(args.NewValue) * 360; //角度
  71. var pathGeometry = new RingProcessBar().GetValue(82, angel);
  72. //Data赋值
  73. path.Data = pathGeometry;
  74. //达到100%则闭合整个
  75. if (angel.Equals(360))
  76. {
  77. path.Data = Geometry.Parse(path.Data.ToString() + " z");
  78. }
  79. }
  80. #endregion
  81. #region 中间部分的颜色值
  82. public static readonly DependencyProperty CenterColorProperty =
  83. DependencyProperty.Register("CenterColor", typeof(string), typeof(RingProcessBar),
  84. new UIPropertyMetadata(string.Empty, CenterColorChangedCallback)
  85. );
  86. public string CenterColor
  87. {
  88. get => (string)GetValue(CenterColorProperty);
  89. set => SetValue(CenterColorProperty, value);
  90. }
  91. private static void CenterColorChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs args)
  92. {
  93. if (obj is not RingProcessBar control) return;
  94. var path = control.RingName;
  95. path.Background = args.NewValue.ToString().ConvertToBrush();
  96. var path1 = control.myCycleProcessBar2;
  97. path1.Stroke = args.NewValue.ToString().ConvertToBrush();
  98. }
  99. #endregion
  100. private void SetValueRing2()
  101. {
  102. var pathGeometry = GetValue(100, 360);
  103. //Data赋值
  104. myCycleProcessBar2.Data = pathGeometry;
  105. myCycleProcessBar2.Data = Geometry.Parse(myCycleProcessBar2.Data.ToString() + " z");
  106. }
  107. protected virtual PathGeometry GetValue(double diameter, double angel)
  108. {
  109. /*****************************************
  110. 方形矩阵边长为90,半长为45
  111. 环形半径为45,所以距离边框5个像素
  112. 环形描边5个像素
  113. ******************************************/
  114. //起始点
  115. double leftStart = diameter / 2;
  116. double topStart = 5;
  117. double radius = leftStart - topStart; //环形半径
  118. double endLeft = 0;
  119. double endTop = 0;
  120. var isLagreCircle = false; //是否优势弧,即大于180度的弧形
  121. //小于90度
  122. if (angel <= 90)
  123. {
  124. var ra = (90 - angel) * Math.PI / 180; //弧度
  125. endLeft = leftStart + Math.Cos(ra) * radius; //余弦横坐标
  126. endTop = topStart + radius - Math.Sin(ra) * radius; //正弦纵坐标
  127. }
  128. else if (angel <= 180)
  129. {
  130. var ra = (angel - 90) * Math.PI / 180; //弧度
  131. endLeft = leftStart + Math.Cos(ra) * radius; //余弦横坐标
  132. endTop = topStart + radius + Math.Sin(ra) * radius; //正弦纵坐标
  133. }
  134. else if (angel <= 270)
  135. {
  136. isLagreCircle = true; //优势弧
  137. double ra = (angel - 180) * Math.PI / 180;
  138. endLeft = leftStart - Math.Sin(ra) * radius;
  139. endTop = topStart + radius + Math.Cos(ra) * radius;
  140. }
  141. else if (angel < 360)
  142. {
  143. isLagreCircle = true; //优势弧
  144. double ra = (angel - 270) * Math.PI / 180;
  145. endLeft = leftStart - Math.Cos(ra) * radius;
  146. endTop = topStart + radius - Math.Sin(ra) * radius;
  147. }
  148. else
  149. {
  150. isLagreCircle = true; //优势弧
  151. endLeft = leftStart - 0.001; //不与起点在同一点,避免重叠绘制出非环形
  152. endTop = topStart;
  153. }
  154. var arcEndPt = new Point(endLeft, endTop); //结束点
  155. var arcSize = new Size(radius, radius);
  156. var direction = SweepDirection.Clockwise; //顺时针弧形
  157. //弧形
  158. var arcsegment = new ArcSegment(arcEndPt, arcSize, 0, isLagreCircle, direction, true);
  159. //形状集合
  160. var pathsegmentCollection = new PathSegmentCollection
  161. {
  162. arcsegment
  163. };
  164. //路径描述
  165. var pathFigure = new PathFigure
  166. {
  167. StartPoint = new Point(leftStart, topStart),
  168. Segments = pathsegmentCollection
  169. };
  170. //起始地址
  171. //路径描述集合
  172. var pathFigureCollection = new PathFigureCollection();
  173. pathFigureCollection.Add(pathFigure);
  174. //复杂形状
  175. var pathGeometry = new PathGeometry
  176. {
  177. Figures = pathFigureCollection
  178. };
  179. return pathGeometry;
  180. }
  181. }
  182. }