终端一体化运控平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

229 строки
11 KiB

  1. using BeDesignerSCADA.Controls;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. namespace BeDesignerSCADA.Adorners
  13. {
  14. internal class SelectionAdorner : Adorner
  15. {
  16. public SelectionAdorner(UIElement adornedEIeent) : base(adornedEIeent) { }
  17. protected override void OnRender(DrawingContext drawingContext)
  18. {
  19. base.OnRender(drawingContext);
  20. Rect adornerRect = new Rect(AdornedElement.DesiredSize);
  21. SolidColorBrush renderBrush = Brushes.Transparent;
  22. Pen renderPen1 = new Pen(new SolidColorBrush(Colors.Tomato), 1);
  23. Pen renderPen = new Pen(new SolidColorBrush(Colors.Tomato), 1);
  24. renderPen1.DashStyle = new DashStyle(new List<double>() { 6, 4 }, 4);
  25. //1.绘制边框
  26. drawingContext.DrawRectangle(renderBrush, renderPen1, new Rect(adornerRect.TopLeft.X, adornerRect.TopLeft.Y, adornerRect.Width, adornerRect.Height));
  27. //2.绘制拖动正方形
  28. // Draw a circle at each corner.
  29. drawingContext.DrawRectangle(renderBrush, renderPen, new Rect(adornerRect.TopLeft.X - 2.5, adornerRect.TopLeft.Y - 2.5, 5, 5));
  30. drawingContext.DrawRectangle(renderBrush, renderPen, new Rect(adornerRect.TopRight.X - 2.5, adornerRect.TopRight.Y - 2.5, 5, 5));
  31. drawingContext.DrawRectangle(renderBrush, renderPen, new Rect(adornerRect.BottomLeft.X - 2.5, adornerRect.BottomLeft.Y - 2.5, 5, 5));
  32. drawingContext.DrawRectangle(renderBrush, renderPen, new Rect(adornerRect.BottomRight.X - 2.5, adornerRect.BottomRight.Y - 2.5, 5, 5));
  33. //3.绘制4个中心正方形
  34. drawingContext.DrawRectangle(renderBrush, renderPen,
  35. new Rect(adornerRect.TopLeft.X + adornerRect.Width / (double)2 - 2.5, adornerRect.TopLeft.Y - 2.5, 5, 5));
  36. drawingContext.DrawRectangle(renderBrush, renderPen,
  37. new Rect(adornerRect.TopRight.X - 2.5, adornerRect.TopRight.Y + adornerRect.Height / (double)2 - 2.5, 5, 5));
  38. drawingContext.DrawRectangle(renderBrush, renderPen,
  39. new Rect(adornerRect.BottomLeft.X - 2.5, adornerRect.BottomLeft.Y - adornerRect.Height / (double)2 - 2.5, 5, 5));
  40. drawingContext.DrawRectangle(renderBrush, renderPen,
  41. new Rect(adornerRect.BottomRight.X - adornerRect.Width / (double)2 - 2.5, adornerRect.BottomRight.Y - 2.5, 5, 5));
  42. MouseDown += SelectionAdorner_MouseDown;
  43. MouseMove += SelectionAdorner_MouseMove;
  44. MouseUp += SelectionAdorner_MouseUp;
  45. ResourceDictionary languageResDic = new ResourceDictionary();
  46. languageResDic.Source = new Uri(@"/BeDesignerSCADA;component/Themes/Styles.xaml", UriKind.RelativeOrAbsolute);
  47. this.Resources.MergedDictionaries.Add(languageResDic);
  48. ContextMenu = FindResource("AdornerRightMenu") as ContextMenu;
  49. Tag = CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement);
  50. this.Focus();
  51. this.SelectionAdorner_MouseDown(this, null);
  52. this.SelectionAdorner_MouseMove(this, null);
  53. }
  54. private void SelectionAdorner_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
  55. {
  56. ReleaseMouseCapture();
  57. CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement).ClearAlignLine();
  58. }
  59. /// <summary>
  60. /// 鼠标最后一次按下位置
  61. /// </summary>
  62. Point lastPoint = new Point();
  63. /// <summary>
  64. /// 鼠标在控件中的位置
  65. /// </summary>
  66. public MousePos mousePos;
  67. double tempX = 0d;
  68. double tempY = 0d;
  69. double movePx = 0d;
  70. /// <summary>
  71. /// 改变鼠标状态,鼠标在控件上,下,左,右,左上,右上,左下,右下,向上不同的光标
  72. /// </summary>
  73. private void SetCursor()
  74. {
  75. Rect adornerRect = new Rect(AdornedElement.DesiredSize);
  76. double _left = CanvasPanel.GetLeft(AdornedElement);
  77. double _top = CanvasPanel.GetTop(AdornedElement);
  78. var DQPoint = Mouse.GetPosition(CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement));
  79. if (DQPoint.X >= (adornerRect.TopLeft.X - 2.5 + _left) && DQPoint.X <= (adornerRect.TopLeft.X + 2.5 + _left) && DQPoint.Y >= (adornerRect.TopLeft.Y - 2.5 + _top) && DQPoint.Y <= (adornerRect.TopLeft.Y + 2.5 + _top))
  80. {
  81. //左上
  82. this.Cursor = Cursors.SizeNWSE; mousePos = MousePos.LeftTop;
  83. }
  84. else if (DQPoint.X >= (adornerRect.TopRight.X - 2.5 + _left) && DQPoint.X <= (adornerRect.TopRight.X + 2.5 + _left) && DQPoint.Y >= (adornerRect.TopRight.Y - 2.5 + _top) && DQPoint.Y <= (adornerRect.TopRight.Y + 2.5 + _top))
  85. {
  86. //右上
  87. this.Cursor = Cursors.SizeNESW; mousePos = MousePos.RightTop;
  88. }
  89. else if (DQPoint.X >= (adornerRect.BottomLeft.X - 2.5 + _left) && DQPoint.X <= (adornerRect.BottomLeft.X + 2.5 + _left) && DQPoint.Y >= (adornerRect.BottomLeft.Y - 2.5 + _top) && DQPoint.Y <= (adornerRect.BottomLeft.Y + 2.5 + _top))
  90. {
  91. //左下
  92. this.Cursor = Cursors.SizeNESW; mousePos = MousePos.LeftBottom ;
  93. }
  94. else if (DQPoint.X >= (adornerRect.BottomRight.X - 2.5 + _left) && DQPoint.X <= (adornerRect.BottomRight.X + 2.5 + _left) && DQPoint.Y >= (adornerRect.BottomRight.Y - 2.5 + _top) && DQPoint.Y <= (adornerRect.BottomRight.Y + 2.5 + _top))
  95. {
  96. //右下
  97. this.Cursor = Cursors.SizeNWSE; mousePos = MousePos.RightBottom;
  98. }
  99. else if (DQPoint.X >= (adornerRect.TopLeft.X + adornerRect.Width / (double)2 - 2.5 + _left) && DQPoint.X <= (adornerRect.TopLeft.X + adornerRect.Width / (double)2 + 2.5 + _left) && DQPoint.Y >= (adornerRect.TopLeft.Y - 2.5 + _top) && DQPoint.Y <= (adornerRect.TopLeft.Y + 2.5 + _top))
  100. {
  101. //上中
  102. this.Cursor = Cursors.SizeNS; mousePos = MousePos.Top;
  103. }
  104. else if (DQPoint.X >= (adornerRect.BottomRight.X - adornerRect.Width / (double)2 - 2.5 + _left) && DQPoint.X <= (adornerRect.BottomRight.X - adornerRect.Width / (double)2 + 2.5 + _left) && DQPoint.Y >= (adornerRect.BottomRight.Y - 2.5 + _top) && DQPoint.Y <= (adornerRect.BottomRight.Y + 2.5 + _top))
  105. {
  106. //下中
  107. this.Cursor = Cursors.SizeNS; mousePos = MousePos.Bottom;
  108. }
  109. else if (DQPoint.X >= (adornerRect.TopRight.X - 2.5 + _left) && DQPoint.X <= (adornerRect.TopRight.X + 2.5 + _left) && DQPoint.Y >= (adornerRect.TopRight.Y + adornerRect.Height / (double)2 - 2.5 + _top) && DQPoint.Y <= (adornerRect.TopRight.Y + adornerRect.Height / (double)2 + 2.5 + _top))
  110. {
  111. //右中
  112. this.Cursor = Cursors.SizeWE; mousePos = MousePos.Right;
  113. }
  114. else if (DQPoint.X >= (adornerRect.BottomLeft.X - 2.5 + _left) && DQPoint.X <= (adornerRect.BottomLeft.X + 2.5 + _left) && DQPoint.Y >= (adornerRect.BottomLeft.Y - adornerRect.Height / (double)2 - 2.5 + _top) && DQPoint.Y <= (adornerRect.BottomLeft.Y - adornerRect.Height / (double)2 + 2.5 + _top))
  115. {
  116. //左中
  117. this.Cursor = Cursors.SizeWE; mousePos = MousePos.Left;
  118. }
  119. else
  120. {
  121. this.Cursor = Cursors.Arrow; mousePos = MousePos.None;
  122. }
  123. }
  124. private void SelectionAdorner_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
  125. {
  126. if (Mouse.LeftButton != MouseButtonState.Pressed
  127. && Mouse.MiddleButton != MouseButtonState.Pressed
  128. && Mouse.RightButton != MouseButtonState.Pressed)//鼠标没有按下,改变鼠标状态
  129. {
  130. SetCursor();
  131. }
  132. if (this.Cursor == Cursors.Arrow && Mouse.LeftButton == MouseButtonState.Pressed)//移动控件
  133. {
  134. if (lastPoint.X == 0 && lastPoint.Y == 0)
  135. {
  136. return;
  137. }
  138. CaptureMouse();
  139. var nowPoint = Mouse.GetPosition(CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement));
  140. double offsetX = nowPoint.X - lastPoint.X;
  141. double offsetY = nowPoint.Y - lastPoint.Y;
  142. lastPoint = nowPoint;
  143. tempX += offsetX;
  144. tempY += offsetY;
  145. var canvas = CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement);
  146. movePx = canvas.GridPxiel;
  147. if (Math.Abs(tempX) >= movePx)
  148. {
  149. offsetX = Math.Round(tempX / movePx) * movePx;
  150. tempX -= offsetX;
  151. canvas.MoveControls(offsetX, 0);
  152. }
  153. if (Math.Abs(tempY) >= movePx)
  154. {
  155. offsetY = Math.Round(tempY / movePx) * movePx;
  156. tempY -= offsetY;
  157. canvas.MoveControls(0, offsetY);
  158. }
  159. }
  160. else if (this.Cursor != Cursors.Arrow && Mouse.LeftButton == MouseButtonState.Pressed)
  161. {
  162. CaptureMouse();
  163. //lastPoint 最后一次鼠标按下位置 nowPonit 鼠标当前位置 mousePos 鼠标在控件中的位置
  164. var nowPoint = Mouse.GetPosition(CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement));
  165. CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement).ReCtrlSize(lastPoint, nowPoint, mousePos);
  166. lastPoint = nowPoint;
  167. }
  168. else if (Mouse.MiddleButton == MouseButtonState.Pressed)//中间同步拖拽控件大小
  169. {
  170. CaptureMouse();
  171. var nowPoint = Mouse.GetPosition(CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement));
  172. int offsetX = (int)(nowPoint.X - lastPoint.X);
  173. int offsetY = (int)(nowPoint.Y - lastPoint.Y);
  174. CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement).ZoomControls(offsetX, offsetY);
  175. lastPoint = nowPoint;
  176. }
  177. }
  178. private void SelectionAdorner_MouseDown(object sender, MouseButtonEventArgs e)
  179. {
  180. var canv = CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement);
  181. lastPoint = Mouse.GetPosition(canv);
  182. Keyboard.Focus(canv);
  183. if (Keyboard.Modifiers == ModifierKeys.Control && e != null)
  184. {
  185. var layer = AdornerLayer.GetAdornerLayer(AdornedElement);
  186. var arr = layer.GetAdorners(AdornedElement);
  187. if (canv.SelectedItems.Contains(AdornedElement))
  188. {
  189. canv.SelectedItems.Remove(AdornedElement as FrameworkElement);
  190. canv.RefreshSelection();
  191. }
  192. }
  193. }
  194. }
  195. /// <summary>
  196. /// 鼠标在控件中的位置
  197. /// </summary>
  198. public enum MousePos
  199. {
  200. None,
  201. Top,
  202. Right,
  203. Bottom,
  204. Left,
  205. LeftTop,
  206. LeftBottom,
  207. RightTop,
  208. RightBottom
  209. }
  210. }