123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424 |
- using System;
- using System.Drawing;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Input;
- using System.Windows.Media;
-
- namespace XHWK.WKTool.Utils
- {
- internal class ImageOperationUtil
- {
- #region 图片拉伸移动
-
- private System.Windows.Point initialPoint;
-
- private Thumb RectLeftUp;
- private Thumb RectRightUp;
- private Thumb RectLeftDown;
- private Thumb RectRightDown;
- private System.Windows.Shapes.Rectangle RectImgBorder;
- private System.Windows.Controls.Image imgCanvas;
- private Grid GridM;
- private bool mouseDown = false;
-
- public void setMouseDown(bool mouseDown) {
- this.mouseDown = mouseDown;
- }
-
- public ImageOperationUtil(
- Thumb RectLeftUp,
- Thumb RectRightUp,
- Thumb RectLeftDown,
- Thumb RectRightDown,
- System.Windows.Shapes.Rectangle RectImgBorder,
- System.Windows.Controls.Image imgCanvas,
- Grid GridM
- )
- {
- this.RectLeftUp = RectLeftUp;
- this.RectRightUp = RectRightUp;
- this.RectLeftDown = RectLeftDown;
- this.RectRightDown = RectRightDown;
- this.RectImgBorder = RectImgBorder;
- this.imgCanvas = imgCanvas;
- this.GridM = GridM;
- }
-
- /// <summary>
- /// 隐藏图片四个点和线
- /// </summary>
- public void HideAngleBorder()
- {
- RectLeftUp.Visibility = Visibility.Hidden;
- RectRightUp.Visibility = Visibility.Hidden;
- RectLeftDown.Visibility = Visibility.Hidden;
- RectRightDown.Visibility = Visibility.Hidden;
- RectImgBorder.Visibility = Visibility.Hidden;
- }
-
- private void ShowAngleBorder()
- {
- PointLocation();
- RectLeftUp.Visibility = Visibility.Visible;
- RectRightUp.Visibility = Visibility.Visible;
- RectLeftDown.Visibility = Visibility.Visible;
- RectRightDown.Visibility = Visibility.Visible;
- RectImgBorder.Visibility = Visibility.Visible;
- }
-
- /// <summary>
- /// 确定四个点和边框的位置大小
- /// </summary>
- private void PointLocation()
- {
- RectImgBorder.Width = imgCanvas.ActualWidth + 10.0;
- RectImgBorder.Height = imgCanvas.ActualHeight + 10.0;
- RectImgBorder.Margin = new Thickness(imgCanvas.Margin.Left - 5.0, imgCanvas.Margin.Top - 5.0, 0, 0);
-
- Canvas.SetLeft(RectLeftUp, imgCanvas.Margin.Left - 10.0);
- Canvas.SetTop(RectLeftUp, imgCanvas.Margin.Top - 10.0);
-
- Canvas.SetLeft(RectRightUp, imgCanvas.Margin.Left + imgCanvas.ActualWidth - 10.0);
- Canvas.SetTop(RectRightUp, imgCanvas.Margin.Top - 10.0);
-
- Canvas.SetLeft(RectLeftDown, imgCanvas.Margin.Left - 10.0);
- Canvas.SetTop(RectLeftDown, imgCanvas.Margin.Top + imgCanvas.ActualHeight - 10.0);
-
- Canvas.SetLeft(RectRightDown, imgCanvas.Margin.Left + imgCanvas.ActualWidth - 10.0);
- Canvas.SetTop(RectRightDown, imgCanvas.Margin.Top + imgCanvas.ActualHeight - 10.0);
- }
-
- public void PicEMap_MouseDown(object sender, MouseButtonEventArgs e)
- {
- System.Windows.Point point = e.GetPosition(imgCanvas);
- initialPoint = point;
-
- HideAngleBorder();
- }
-
- public void imgCanvas_MouseMove(object sender, MouseEventArgs e)
- {
- if (e.LeftButton == MouseButtonState.Pressed && mouseDown)
- {
- System.Windows.Point point = e.GetPosition(imgCanvas);
- imgCanvas.Margin = new Thickness(imgCanvas.Margin.Left + (point.X - initialPoint.X), imgCanvas.Margin.Top + (point.Y - initialPoint.Y), 0, 0);
- App.PageDrawList[App.PageContextData.currpage - 1].ImageLocation = new TranslateTransform
- {
- X = imgCanvas.Margin.Left,
- Y = imgCanvas.Margin.Top
- };
- App.PageDrawList[App.PageContextData.currpage - 1].ImageSizes = new ScaleTransform
- {
- CenterX = imgCanvas.ActualWidth,
- CenterY = imgCanvas.ActualHeight
- };
- App.PageDrawList[App.PageContextData.currpage - 1].IsImageLocation = false;
- }
- }
-
- public void imgCanvas_MouseUp()
- {
- if (mouseDown)
- {
- ShowAngleBorder();
- }
- }
-
- private PointF imgRightDown;
-
- /// <summary>
- /// 设置控件最上层
- /// </summary>
- /// <param name="element">
- /// </param>
- public void BringToFront(Thumb element)//图片置于最顶层显示
- {
- if (element == null)
- {
- return;
- }
-
- Canvas parent = element.Parent as Canvas;
- if (parent == null)
- {
- return;
- }
-
- int maxZ = parent.Children.OfType<UIElement>()//linq语句,取Zindex的最大值
- .Where(x => x != element)
- .Select(x => Canvas.GetZIndex(x))
- .Max();
- Canvas.SetZIndex(element, maxZ + 1);
- }
-
- public void RectRightUp_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
- {
- try
- {
- Thumb thu = (Thumb)sender;
- BringToFront(thu);
-
- imgRightDown = new System.Drawing.PointF((float)(imgCanvas.Margin.Left + imgCanvas.ActualWidth), (float)(imgCanvas.Margin.Top + imgCanvas.ActualHeight));
- HideAngleBorder();
- switch (thu.Name)
- {
- case "RectLeftUp":
- RectLeftUp.Visibility = Visibility.Visible;
- break;
-
- case "RectRightUp":
- RectRightUp.Visibility = Visibility.Visible;
- break;
-
- case "RectLeftDown":
- RectLeftDown.Visibility = Visibility.Visible;
- break;
-
- case "RectRightDown":
- RectRightDown.Visibility = Visibility.Visible;
- break;
-
- default:
- break;
- }
- }
- catch (Exception ex)
- {
- MessageWindow.Show(ex.Message);
- }
- }
-
- public void RectRightUp_DragDelta(object sender, DragDeltaEventArgs e)
- {
- try
- {
- if (mouseDown)
- {
- Thumb thu = (Thumb)sender;
-
- #region 判断是否超出 暂无
-
- double plul = Canvas.GetLeft(RectLeftUp);
- double plut = Canvas.GetTop(RectLeftUp);
- double prdl = Canvas.GetLeft(RectRightDown);
- double prdt = Canvas.GetTop(RectRightDown);
-
- #endregion 判断是否超出 暂无
-
- double imgW = 0;
- double imgH = 0;
- double imgX = 0;
- double imgY = 0;
- switch (thu.Name)
- {
- case "RectLeftUp":
-
- #region 左上
-
- imgW = imgRightDown.X - (Mouse.GetPosition(GridM).X);
- imgH = imgRightDown.Y - (Mouse.GetPosition(GridM).Y);
- imgX = 0;
- imgY = 0;
- if (imgW < 50)
- {
- imgCanvas.Width = 50;
- imgX = imgCanvas.Margin.Left;
- Canvas.SetLeft(thu, Canvas.GetLeft(RectRightDown) - 50);
- }
- else
- {
- imgCanvas.Width = imgW;
- imgX = Mouse.GetPosition(GridM).X;
- Canvas.SetLeft(thu, Canvas.GetLeft(thu) + e.HorizontalChange);
- }
- if (imgH < 50)
- {
- imgCanvas.Height = 50;
- imgY = imgCanvas.Margin.Top;
- Canvas.SetTop(thu, Canvas.GetTop(RectRightDown) - 50);
- }
- else
- {
- imgCanvas.Height = imgH;
- imgY = Mouse.GetPosition(GridM).Y;
- Canvas.SetTop(thu, Canvas.GetTop(thu) + e.VerticalChange);
- }
- imgCanvas.Margin = new Thickness(imgX, imgY, 0, 0);
-
- #endregion 左上
-
- break;
-
- case "RectRightUp":
-
- #region 右上
-
- imgW = Mouse.GetPosition(GridM).X - imgCanvas.Margin.Left;
- imgH = imgRightDown.Y - (Mouse.GetPosition(GridM).Y);
- imgX = 0;
- imgY = 0;
- if (imgW < 50)
- {
- imgCanvas.Width = 50;
- imgX = imgCanvas.Margin.Left;
- Canvas.SetLeft(thu, Canvas.GetLeft(RectLeftUp) + 50);
- }
- else
- {
- imgCanvas.Width = imgW;
- imgX = imgCanvas.Margin.Left;
- Canvas.SetLeft(thu, Canvas.GetLeft(thu) + e.HorizontalChange);
- }
- if (imgH < 50)
- {
- imgCanvas.Height = 50;
- imgY = imgCanvas.Margin.Top;
- Canvas.SetTop(thu, Canvas.GetTop(RectRightDown) - 50);
- }
- else
- {
- imgCanvas.Height = imgH;
- imgY = Mouse.GetPosition(GridM).Y;
- Canvas.SetTop(thu, Canvas.GetTop(thu) + e.VerticalChange);
- }
- imgCanvas.Margin = new Thickness(imgX, imgY, 0, 0);
-
- #endregion 右上
-
- break;
-
- case "RectLeftDown":
-
- #region 左下
-
- imgW = imgRightDown.X - (Mouse.GetPosition(GridM).X);
- imgH = Mouse.GetPosition(GridM).Y - imgCanvas.Margin.Top;
- imgX = 0;
- imgY = 0;
- if (imgW < 50)
- {
- imgCanvas.Width = 50;
- imgX = imgCanvas.Margin.Left;
- Canvas.SetLeft(thu, Canvas.GetLeft(RectRightDown) - 50);
- }
- else
- {
- imgCanvas.Width = imgW;
- imgX = Mouse.GetPosition(GridM).X;
- Canvas.SetLeft(thu, Canvas.GetLeft(thu) + e.HorizontalChange);
- }
- if (imgH < 50)
- {
- imgCanvas.Height = 50;
- imgY = imgCanvas.Margin.Top;
- Canvas.SetTop(thu, Canvas.GetTop(RectLeftUp) + 50);
- }
- else
- {
- imgCanvas.Height = imgH;
- imgY = imgCanvas.Margin.Top;
- Canvas.SetTop(thu, Canvas.GetTop(thu) + e.VerticalChange);
- }
- imgCanvas.Margin = new Thickness(imgX, imgY, 0, 0);
- //imgCanvas.Width = imgRightDown.X - (Mouse.GetPosition(GridM).X);
- //imgCanvas.Height = Mouse.GetPosition(GridM).Y - imgCanvas.Margin.Top;
- //imgCanvas.Margin = new Thickness(Mouse.GetPosition(GridM).X, imgCanvas.Margin.Top, 0, 0);
-
- #endregion 左下
-
- break;
-
- case "RectRightDown":
-
- #region 右下
-
- imgW = Mouse.GetPosition(GridM).X - imgCanvas.Margin.Left;
- imgH = Mouse.GetPosition(GridM).Y - imgCanvas.Margin.Top;
- if (imgW < 50)
- {
- imgCanvas.Width = 50;
- Canvas.SetLeft(thu, Canvas.GetLeft(RectLeftUp) + 50);
- }
- else
- {
- imgCanvas.Width = imgW;
- Canvas.SetLeft(thu, Canvas.GetLeft(thu) + e.HorizontalChange);
- }
- if (imgH < 50)
- {
- imgCanvas.Height = 50;
- Canvas.SetTop(thu, Canvas.GetTop(RectLeftUp) + 50);
- }
- else
- {
- imgCanvas.Height = imgH;
- Canvas.SetTop(thu, Canvas.GetTop(thu) + e.VerticalChange);
- }
- //imgCanvas.Margin = new Thickness(imgX, imgY, 0, 0);
- //imgCanvas.Width += e.HorizontalChange;
- //imgCanvas.Height += e.VerticalChange;
-
- #endregion 右下
-
- break;
-
- default:
- break;
- }
- App.PageDrawList[App.PageContextData.currpage - 1].ImageLocation = new TranslateTransform
- {
- X = imgCanvas.Margin.Left,
- Y = imgCanvas.Margin.Top
- };
- App.PageDrawList[App.PageContextData.currpage - 1].ImageSizes = new ScaleTransform
- {
- CenterX = imgCanvas.ActualWidth,
- CenterY = imgCanvas.ActualHeight
- };
- App.PageDrawList[App.PageContextData.currpage - 1].IsImageLocation = false;
- }
- }
- catch (Exception)
- {
- MessageWindow.Show("图片过小!");
- }
- }
-
- public void RectRightUp_DragCompleted()
- {
- ShowAngleBorder();
- }
-
- /// <summary>
- /// 点击标题栏 隐藏截图的四个点和线
- /// </summary>
- /// <param name="sender">
- /// </param>
- /// <param name="e">
- /// </param>
- public void Grid_MouseDown()
- {
- HideAngleBorder();
- }
-
- /// <summary>
- /// 鼠标左键点击事件
- /// </summary>
- /// <param name="sender">
- /// </param>
- /// <param name="e">
- /// </param>
- public void Window_MouseLeftButtonDown_1()
- {
- if (RectImgBorder.Visibility != Visibility.Hidden)
- {
- HideAngleBorder();
- }
- }
-
- #endregion 图片拉伸移动
- }
- }
|