|
- using BeDesignerSCADA.Controls;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
-
- namespace BeDesignerSCADA.Adorners
- {
- internal class SelectionAdorner : Adorner
- {
- public SelectionAdorner(UIElement adornedEIeent) : base(adornedEIeent) { }
-
- protected override void OnRender(DrawingContext drawingContext)
- {
- base.OnRender(drawingContext);
- Rect adornerRect = new Rect(AdornedElement.DesiredSize);
- SolidColorBrush renderBrush = Brushes.Transparent;
- Pen renderPen1 = new Pen(new SolidColorBrush(Colors.Tomato), 1);
- Pen renderPen = new Pen(new SolidColorBrush(Colors.Tomato), 1);
- renderPen1.DashStyle = new DashStyle(new List<double>() { 6, 4 }, 4);
-
- //1.绘制边框
- drawingContext.DrawRectangle(renderBrush, renderPen1, new Rect(adornerRect.TopLeft.X, adornerRect.TopLeft.Y, adornerRect.Width, adornerRect.Height));
- //2.绘制拖动正方形
- // Draw a circle at each corner.
- drawingContext.DrawRectangle(renderBrush, renderPen, new Rect(adornerRect.TopLeft.X - 2.5, adornerRect.TopLeft.Y - 2.5, 5, 5));
- drawingContext.DrawRectangle(renderBrush, renderPen, new Rect(adornerRect.TopRight.X - 2.5, adornerRect.TopRight.Y - 2.5, 5, 5));
- drawingContext.DrawRectangle(renderBrush, renderPen, new Rect(adornerRect.BottomLeft.X - 2.5, adornerRect.BottomLeft.Y - 2.5, 5, 5));
- drawingContext.DrawRectangle(renderBrush, renderPen, new Rect(adornerRect.BottomRight.X - 2.5, adornerRect.BottomRight.Y - 2.5, 5, 5));
- //3.绘制4个中心正方形
- drawingContext.DrawRectangle(renderBrush, renderPen,
- new Rect(adornerRect.TopLeft.X + adornerRect.Width / (double)2 - 2.5, adornerRect.TopLeft.Y - 2.5, 5, 5));
- drawingContext.DrawRectangle(renderBrush, renderPen,
- new Rect(adornerRect.TopRight.X - 2.5, adornerRect.TopRight.Y + adornerRect.Height / (double)2 - 2.5, 5, 5));
- drawingContext.DrawRectangle(renderBrush, renderPen,
- new Rect(adornerRect.BottomLeft.X - 2.5, adornerRect.BottomLeft.Y - adornerRect.Height / (double)2 - 2.5, 5, 5));
- drawingContext.DrawRectangle(renderBrush, renderPen,
- new Rect(adornerRect.BottomRight.X - adornerRect.Width / (double)2 - 2.5, adornerRect.BottomRight.Y - 2.5, 5, 5));
-
- MouseDown += SelectionAdorner_MouseDown;
- MouseMove += SelectionAdorner_MouseMove;
- MouseUp += SelectionAdorner_MouseUp;
- ResourceDictionary languageResDic = new ResourceDictionary();
- languageResDic.Source = new Uri(@"/BeDesignerSCADA;component/Themes/Styles.xaml", UriKind.RelativeOrAbsolute);
- this.Resources.MergedDictionaries.Add(languageResDic);
- ContextMenu = FindResource("AdornerRightMenu") as ContextMenu;
- Tag = CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement);
-
- this.Focus();
- this.SelectionAdorner_MouseDown(this, null);
- this.SelectionAdorner_MouseMove(this, null);
- }
-
- private void SelectionAdorner_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
- {
- ReleaseMouseCapture();
- CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement).ClearAlignLine();
- }
-
- /// <summary>
- /// 鼠标最后一次按下位置
- /// </summary>
- Point lastPoint = new Point();
- /// <summary>
- /// 鼠标在控件中的位置
- /// </summary>
- public MousePos mousePos;
- double tempX = 0d;
- double tempY = 0d;
- double movePx = 0d;
- /// <summary>
- /// 改变鼠标状态,鼠标在控件上,下,左,右,左上,右上,左下,右下,向上不同的光标
- /// </summary>
- private void SetCursor()
- {
- Rect adornerRect = new Rect(AdornedElement.DesiredSize);
- double _left = CanvasPanel.GetLeft(AdornedElement);
- double _top = CanvasPanel.GetTop(AdornedElement);
- var DQPoint = Mouse.GetPosition(CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement));
- 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))
- {
- //左上
- this.Cursor = Cursors.SizeNWSE; mousePos = MousePos.LeftTop;
- }
- 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))
- {
- //右上
- this.Cursor = Cursors.SizeNESW; mousePos = MousePos.RightTop;
- }
- 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))
- {
- //左下
- this.Cursor = Cursors.SizeNESW; mousePos = MousePos.LeftBottom ;
- }
- 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))
- {
- //右下
- this.Cursor = Cursors.SizeNWSE; mousePos = MousePos.RightBottom;
- }
- 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))
- {
- //上中
- this.Cursor = Cursors.SizeNS; mousePos = MousePos.Top;
- }
- 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))
- {
- //下中
- this.Cursor = Cursors.SizeNS; mousePos = MousePos.Bottom;
- }
- 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))
- {
- //右中
- this.Cursor = Cursors.SizeWE; mousePos = MousePos.Right;
- }
- 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))
- {
- //左中
- this.Cursor = Cursors.SizeWE; mousePos = MousePos.Left;
- }
- else
- {
- this.Cursor = Cursors.Arrow; mousePos = MousePos.None;
- }
- }
-
-
- private void SelectionAdorner_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
- {
- if (Mouse.LeftButton != MouseButtonState.Pressed
- && Mouse.MiddleButton != MouseButtonState.Pressed
- && Mouse.RightButton != MouseButtonState.Pressed)//鼠标没有按下,改变鼠标状态
- {
- SetCursor();
- }
-
- if (this.Cursor == Cursors.Arrow && Mouse.LeftButton == MouseButtonState.Pressed)//移动控件
- {
- if (lastPoint.X == 0 && lastPoint.Y == 0)
- {
- return;
- }
-
- CaptureMouse();
- var nowPoint = Mouse.GetPosition(CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement));
- double offsetX = nowPoint.X - lastPoint.X;
- double offsetY = nowPoint.Y - lastPoint.Y;
- lastPoint = nowPoint;
-
- tempX += offsetX;
- tempY += offsetY;
-
- var canvas = CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement);
- movePx = canvas.GridPxiel;
-
- if (Math.Abs(tempX) >= movePx)
- {
- offsetX = Math.Round(tempX / movePx) * movePx;
- tempX -= offsetX;
- canvas.MoveControls(offsetX, 0);
- }
-
- if (Math.Abs(tempY) >= movePx)
- {
- offsetY = Math.Round(tempY / movePx) * movePx;
- tempY -= offsetY;
- canvas.MoveControls(0, offsetY);
- }
- }
- else if (this.Cursor != Cursors.Arrow && Mouse.LeftButton == MouseButtonState.Pressed)
- {
- CaptureMouse();
- //lastPoint 最后一次鼠标按下位置 nowPonit 鼠标当前位置 mousePos 鼠标在控件中的位置
- var nowPoint = Mouse.GetPosition(CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement));
- CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement).ReCtrlSize(lastPoint, nowPoint, mousePos);
- lastPoint = nowPoint;
- }
- else if (Mouse.MiddleButton == MouseButtonState.Pressed)//中间同步拖拽控件大小
- {
- CaptureMouse();
- var nowPoint = Mouse.GetPosition(CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement));
- int offsetX = (int)(nowPoint.X - lastPoint.X);
- int offsetY = (int)(nowPoint.Y - lastPoint.Y);
- CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement).ZoomControls(offsetX, offsetY);
- lastPoint = nowPoint;
- }
- }
-
- private void SelectionAdorner_MouseDown(object sender, MouseButtonEventArgs e)
- {
- var canv = CanvasPanel.GetParentObject<CanvasPanel>(AdornedElement);
- lastPoint = Mouse.GetPosition(canv);
- Keyboard.Focus(canv);
-
- if (Keyboard.Modifiers == ModifierKeys.Control && e != null)
- {
- var layer = AdornerLayer.GetAdornerLayer(AdornedElement);
- var arr = layer.GetAdorners(AdornedElement);
- if (canv.SelectedItems.Contains(AdornedElement))
- {
- canv.SelectedItems.Remove(AdornedElement as FrameworkElement);
- canv.RefreshSelection();
- }
- }
- }
- }
-
- /// <summary>
- /// 鼠标在控件中的位置
- /// </summary>
- public enum MousePos
- {
- None,
- Top,
- Right,
- Bottom,
- Left,
- LeftTop,
- LeftBottom,
- RightTop,
- RightBottom
- }
- }
|