终端一体化运控平台
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.
 
 
 

199 lines
6.4 KiB

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