终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

198 regels
6.4 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Animation;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace BPASmartClient.SCADAControl
  18. {
  19. /// <summary>
  20. /// 新测试仪表盘
  21. /// </summary>
  22. public class ArcGauge : Control, IExecutable
  23. {
  24. public ArcGauge()
  25. {
  26. Width = 300;
  27. Height = 300;
  28. SetCurrentValue(ValueProperty, 0d);
  29. SetCurrentValue(MinValueProperty, 0d);
  30. SetCurrentValue(MaxValueProperty, 100d);
  31. }
  32. private void InitTick()
  33. {
  34. // 画大刻度
  35. for (int i = 0; i < 11; i++)
  36. {
  37. Line line = new Line();
  38. line.X1 = 0;
  39. line.Y1 = 0;
  40. line.X2 = 0;
  41. line.Y2 = 12;
  42. line.Stroke = Brushes.White;
  43. line.StrokeThickness = 2;
  44. line.HorizontalAlignment = HorizontalAlignment.Center;
  45. line.RenderTransformOrigin = new Point(0.5, 0.5);
  46. line.RenderTransform = new RotateTransform() { Angle = -140 + i * 28 };
  47. bdGrid.Children.Add(line);
  48. DrawText();
  49. }
  50. // 画小刻度
  51. for (int i = 0; i < 10; i++)
  52. {
  53. var start = -140 + 28 * i + 2.8;
  54. for (int j = 0; j < 9; j++)
  55. {
  56. Line line = new Line();
  57. line.X1 = 0;
  58. line.Y1 = 0;
  59. line.X2 = 0;
  60. line.Y2 = 6;
  61. line.Stroke = Brushes.White;
  62. line.StrokeThickness = 1;
  63. line.HorizontalAlignment = HorizontalAlignment.Center;
  64. line.RenderTransformOrigin = new Point(0.5, 0.5);
  65. line.RenderTransform = new RotateTransform() { Angle = start + j * 2.8 };
  66. bdGrid.Children.Add(line);
  67. }
  68. }
  69. }
  70. List<TextBlock> textLabels = new List<TextBlock>();
  71. private void DrawText()
  72. {
  73. foreach (var item in textLabels)
  74. {
  75. bdGrid.Children.Remove(item);
  76. }
  77. textLabels.Clear();
  78. var per = MaxValue / 10;
  79. for (int i = 0; i < 11; i++)
  80. {
  81. TextBlock textBlock = new TextBlock();
  82. textBlock.Text = $"{MinValue + (per * i)}";
  83. textBlock.HorizontalAlignment = HorizontalAlignment.Center;
  84. textBlock.RenderTransformOrigin = new Point(0.5, 0.5);
  85. textBlock.RenderTransform = new RotateTransform() { Angle = -140 + i * 28 };
  86. textBlock.Margin = new Thickness(12);
  87. textBlock.Foreground = Brushes.White;
  88. bdGrid.Children.Add(textBlock);
  89. textLabels.Add(textBlock);
  90. }
  91. }
  92. static ArcGauge()
  93. {
  94. DefaultStyleKeyProperty.OverrideMetadata(typeof(ArcGauge), new FrameworkPropertyMetadata(typeof(ArcGauge)));
  95. }
  96. RotateTransform rotateTransform;
  97. Grid bdGrid;
  98. public override void OnApplyTemplate()
  99. {
  100. base.OnApplyTemplate();
  101. rotateTransform = GetTemplateChild("PointRotate") as RotateTransform;
  102. bdGrid = GetTemplateChild("bdGrid") as Grid;
  103. Refresh();
  104. InitTick();
  105. }
  106. private bool isExecuteState;
  107. public bool IsExecuteState
  108. {
  109. get { return isExecuteState; }
  110. set
  111. {
  112. isExecuteState = value;
  113. if (IsExecuteState)
  114. {
  115. Register();
  116. }
  117. }
  118. }
  119. [Category("值设定")]
  120. public double Value
  121. {
  122. get { return (double)GetValue(ValueProperty); }
  123. set { SetValue(ValueProperty, value); }
  124. }
  125. public static readonly DependencyProperty ValueProperty =
  126. DependencyProperty.Register("Value", typeof(double), typeof(ArcGauge), new PropertyMetadata(0d, OnValueChanged));
  127. private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as ArcGauge)?.Refresh();
  128. [Category("值设定")]
  129. public double MinValue
  130. {
  131. get { return (double)GetValue(MinValueProperty); }
  132. set { SetValue(MinValueProperty, value); }
  133. }
  134. public static readonly DependencyProperty MinValueProperty =
  135. DependencyProperty.Register("MinValue", typeof(double), typeof(ArcGauge), new PropertyMetadata(0d, OnValueChanged));
  136. [Category("值设定")]
  137. public double MaxValue
  138. {
  139. get { return (double)GetValue(MaxValueProperty); }
  140. set { SetValue(MaxValueProperty, value); }
  141. }
  142. public string ControlType => "控件";
  143. public static readonly DependencyProperty MaxValueProperty =
  144. DependencyProperty.Register("MaxValue", typeof(double), typeof(ArcGauge), new PropertyMetadata(0d, OnValueChanged));
  145. private void Refresh()
  146. {
  147. if (rotateTransform == null)
  148. return;
  149. DrawText();
  150. DoubleAnimation da = new DoubleAnimation();
  151. da.Duration = new Duration(TimeSpan.FromMilliseconds(350));
  152. da.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
  153. if (Value > MaxValue)
  154. {
  155. rotateTransform.Angle = 140;
  156. da.To = 140;
  157. }
  158. else if (Value < MinValue)
  159. {
  160. rotateTransform.Angle = -140;
  161. da.To = -140;
  162. }
  163. else
  164. {
  165. var range = MaxValue - MinValue;
  166. var process = Value / range;
  167. var tAngle = process * 280 - 140;
  168. rotateTransform.Angle = tAngle;
  169. da.To = tAngle;
  170. }
  171. rotateTransform.BeginAnimation(RotateTransform.AngleProperty, da);
  172. }
  173. public void Register()
  174. {
  175. Refresh();
  176. }
  177. }
  178. }