终端一体化运控平台
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

183 rader
5.1 KiB

  1. using BPASmartClient.Compiler;
  2. using BPASmartClient.SCADAControl;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Markup;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. namespace BeDesignerSCADA.Controls
  20. {
  21. /// <summary>
  22. /// 运行控件
  23. /// RunCanvas.xaml 的交互逻辑
  24. /// </summary>
  25. public partial class RunCanvas : UserControl
  26. {
  27. public RunCanvas()
  28. {
  29. InitializeComponent();
  30. Unloaded += (s, e) => Destory();
  31. }
  32. /// <summary>
  33. /// Dispose子集
  34. /// </summary>
  35. public void Destory()
  36. {
  37. foreach (var item in RootCanvas.Children)
  38. {
  39. if (item is IDisposable disposable)
  40. {
  41. disposable.Dispose();
  42. }
  43. }
  44. }
  45. /// <summary>
  46. /// 加载数据
  47. /// </summary>
  48. public void LoadingData(string path)
  49. {
  50. //加载控件
  51. List<FrameworkElement> Children = new List<FrameworkElement>();
  52. FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  53. using (StreamReader sr = new StreamReader(fs, System.Text.Encoding.Unicode))
  54. {
  55. while (sr.Peek() > -1)
  56. {
  57. string str = sr.ReadLine();
  58. var ele = XamlReader.Parse(str) as FrameworkElement;
  59. Children.Add(ele);
  60. }
  61. }
  62. if (Children.Count > 0)
  63. {
  64. Run(Children);
  65. }
  66. }
  67. /// <summary>
  68. /// 初始化
  69. /// </summary>
  70. /// <param name="canvas"></param>
  71. public void Init(List<FrameworkElement> canvas)
  72. {
  73. RootCanvas.Children.Clear();
  74. foreach (FrameworkElement element in canvas)
  75. {
  76. RootCanvas.Children.Add(element);
  77. }
  78. }
  79. public void RunSelect()
  80. {
  81. Config.GetInstance().Reset();
  82. foreach (FrameworkElement element in RootCanvas.Children)
  83. {
  84. RegisterJsName(element);//注册rejs
  85. }
  86. foreach (FrameworkElement element in RootCanvas.Children)
  87. {
  88. if (element is IExecutable executable)
  89. executable.IsExecuteState = true;
  90. }
  91. }
  92. public void Run(List<FrameworkElement> canvas)
  93. {
  94. RootCanvas.Children.Clear();
  95. foreach (FrameworkElement element in canvas)
  96. {
  97. if (element is IExecutable executable)
  98. executable.IsExecuteState = true;
  99. RootCanvas.Children.Add(element);
  100. RegisterJsName(element);
  101. }
  102. }
  103. // 注册名称到Js
  104. static void RegisterJsName(FrameworkElement element)
  105. {
  106. Config.GetInstance().SetVariable(element.Name, element);
  107. if (element is Panel panel)
  108. {
  109. foreach (var item in panel.Children)
  110. {
  111. RegisterJsName(item as FrameworkElement);
  112. }
  113. }
  114. }
  115. #region 拖动与缩放
  116. private void RootCanvas_MouseMove(object sender, MouseEventArgs e)
  117. {
  118. if (DragEnable.IsChecked == false)
  119. {
  120. return;
  121. }
  122. if (e.LeftButton == MouseButtonState.Pressed && isPressed)
  123. {
  124. Point point = e.GetPosition(this);
  125. var movex = (point.X - last.X);
  126. var movey = (point.Y - last.Y);
  127. Translate.X += movex;
  128. Translate.Y += movey;
  129. last = point;
  130. }
  131. }
  132. bool isPressed = false;
  133. Point last;//记录上次鼠标坐标位置
  134. private void RootCanvas_MouseLeftButtoDown(object sender, MouseButtonEventArgs e)
  135. {
  136. last = e.GetPosition(this);
  137. isPressed = true;
  138. }
  139. private void RootCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  140. {
  141. isPressed = false;
  142. }
  143. // 缩放
  144. private void RootCanvas_MouseWheel(object sender, MouseWheelEventArgs e)
  145. {
  146. if (ZoomEnable.IsChecked == false)
  147. {
  148. return;
  149. }
  150. var zoomS = (e.Delta / 960d);
  151. var zoom = zoomS + Scale.ScaleX;
  152. if (zoom > 3 || zoom < 0.8)
  153. {
  154. return;
  155. }
  156. Scale.ScaleX = Scale.ScaleY = zoom;
  157. Point mouse = e.GetPosition(RootCanvas);
  158. Point newMouse = new Point(mouse.X * zoomS, mouse.Y * zoomS);
  159. Translate.X -= newMouse.X;
  160. Translate.Y -= newMouse.Y;
  161. }
  162. #endregion
  163. }
  164. }