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

242 lines
7.4 KiB

  1. using BPASmartClient.Compiler;
  2. using BPASmartClient.SCADAControl;
  3. using System;
  4. using System.Collections.Generic;
  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 KnobButton : Slider, IExecutable
  24. {
  25. static KnobButton()
  26. {
  27. DefaultStyleKeyProperty.OverrideMetadata(typeof(KnobButton), new FrameworkPropertyMetadata(typeof(KnobButton)));
  28. }
  29. public KnobButton()
  30. {
  31. SetCurrentValue(WidthProperty, 150d);
  32. SetCurrentValue(HeightProperty, 150d);
  33. SetCurrentValue(MaximumProperty, 100d);
  34. MouseDown += Path_MouseDown;
  35. MouseMove += Path_MouseMove;
  36. MouseWheel += Path_MouseWheel;
  37. MouseLeftButtonUp += KnobButton_MouseLeftButtonUp;
  38. Update();
  39. }
  40. #region 设计需要
  41. public string ControlType => "控件";
  42. private bool isExecuteState;
  43. public bool IsExecuteState
  44. {
  45. get { return isExecuteState; }
  46. set
  47. {
  48. isExecuteState = value;
  49. if (IsExecuteState)
  50. Register();
  51. }
  52. }
  53. /// <summary>
  54. /// 注册需要处理的事件
  55. /// </summary>
  56. public void Register()
  57. {
  58. ValueChanged += KnobButton_ValueChanged; ;
  59. }
  60. private void KnobButton_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  61. {
  62. Config.GetInstance().RunJsScipt(ValueChangedExecute);
  63. }
  64. #endregion
  65. #region 绘制
  66. private void InitTick()
  67. {
  68. // 画大刻度
  69. for (int i = 0; i < 11; i++)
  70. {
  71. Line line = new Line();
  72. line.X1 = 0;
  73. line.Y1 = 0;
  74. line.X2 = 0;
  75. line.Y2 = 12;
  76. line.Stroke = Brushes.Gray;
  77. line.StrokeThickness = 2;
  78. line.HorizontalAlignment = HorizontalAlignment.Center;
  79. line.RenderTransformOrigin = new Point(0.5, 0.5);
  80. line.RenderTransform = new RotateTransform() { Angle = -140 + i * 28 };
  81. bdGrid.Children.Add(line);
  82. }
  83. // 画小刻度
  84. for (int i = 0; i < 10; i++)
  85. {
  86. var start = -140 + 28 * i + 2.8;
  87. for (int j = 0; j < 9; j++)
  88. {
  89. Line line = new Line();
  90. line.X1 = 0;
  91. line.Y1 = 0;
  92. line.X2 = 0;
  93. line.Y2 = 6;
  94. line.Stroke = Brushes.Gray;
  95. line.StrokeThickness = 1;
  96. line.HorizontalAlignment = HorizontalAlignment.Center;
  97. line.RenderTransformOrigin = new Point(0.5, 0.5);
  98. line.RenderTransform = new RotateTransform() { Angle = start + j * 2.8 };
  99. bdGrid.Children.Add(line);
  100. }
  101. }
  102. }
  103. #endregion
  104. protected override void OnValueChanged(double oldValue, double newValue)
  105. {
  106. base.OnValueChanged(oldValue, newValue);
  107. Update();
  108. }
  109. protected override void OnMaximumChanged(double oldMaximum, double newMaximum)
  110. {
  111. base.OnMaximumChanged(oldMaximum, newMaximum);
  112. Update();
  113. }
  114. protected override void OnMinimumChanged(double oldMinimum, double newMinimum)
  115. {
  116. base.OnMinimumChanged(oldMinimum, newMinimum);
  117. Update();
  118. }
  119. public string ValueChangedExecute
  120. {
  121. get { return (string)GetValue(ValueChangedExecuteProperty); }
  122. set { SetValue(ValueChangedExecuteProperty, value); }
  123. }
  124. public static readonly DependencyProperty ValueChangedExecuteProperty =
  125. DependencyProperty.Register("ValueChangedExecute", typeof(string), typeof(KnobButton), new PropertyMetadata(string.Empty));
  126. public int Step
  127. {
  128. get { return (int)GetValue(StepProperty); }
  129. set { SetValue(StepProperty, value); }
  130. }
  131. public static readonly DependencyProperty StepProperty =
  132. DependencyProperty.Register("Step", typeof(int), typeof(KnobButton), new PropertyMetadata(1));
  133. RotateTransform rotatevalue;
  134. Grid bdGrid;
  135. public override void OnApplyTemplate()
  136. {
  137. base.OnApplyTemplate();
  138. rotatevalue = GetTemplateChild("rotatevalue") as RotateTransform;
  139. bdGrid = GetTemplateChild("bdGrid") as Grid;
  140. Update();
  141. InitTick();
  142. }
  143. private void Update()
  144. {
  145. if (rotatevalue == null) return;
  146. double perangle = 280 / (Maximum - Minimum);
  147. double angle = (perangle * (Value - Minimum)) + 40;
  148. DoubleAnimation da = new DoubleAnimation();
  149. da.Duration = new Duration(TimeSpan.FromMilliseconds(350));
  150. da.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
  151. da.To = angle;
  152. rotatevalue.Angle = angle;
  153. rotatevalue.BeginAnimation(RotateTransform.AngleProperty, da);
  154. }
  155. Point lastpoint;
  156. private void Path_MouseMove(object sender, MouseEventArgs e)
  157. {
  158. if (e.LeftButton == MouseButtonState.Released) return;
  159. CaptureMouse();
  160. Point point = e.GetPosition(this);
  161. double xmove = point.X - lastpoint.X;
  162. double ymove = point.Y - lastpoint.Y;
  163. double changeValue = (xmove + ymove) / 10 * Step;
  164. if ((changeValue + Value) > Maximum)
  165. {
  166. if (Value < Maximum)
  167. {
  168. Value = Maximum;
  169. }
  170. return;
  171. }
  172. if ((changeValue + Value) < Minimum)
  173. {
  174. if (Value > Minimum)
  175. {
  176. Value = Minimum;
  177. }
  178. return;
  179. }
  180. Value = changeValue + Value;
  181. lastpoint = point;
  182. }
  183. private void Path_MouseDown(object sender, MouseButtonEventArgs e)
  184. {
  185. if (e.LeftButton == MouseButtonState.Released) return;
  186. lastpoint = e.GetPosition(this);
  187. }
  188. private void KnobButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  189. {
  190. ReleaseMouseCapture();
  191. }
  192. private void Path_MouseWheel(object sender, MouseWheelEventArgs e)
  193. {
  194. double changeValue = (e.Delta / 120) * Step;
  195. if ((changeValue + Value) > Maximum)
  196. {
  197. if (Value < Maximum)
  198. {
  199. Value = Maximum;
  200. }
  201. return;
  202. }
  203. if ((changeValue + Value) < Minimum)
  204. {
  205. if (Value > Minimum)
  206. {
  207. Value = Minimum;
  208. }
  209. return;
  210. }
  211. Value = Value + changeValue;
  212. Update();
  213. }
  214. }
  215. }