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

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