终端一体化运控平台
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

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