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

ArcGauge.cs 6.5 KiB

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