/// <summary> /// 图片左键按下 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void IMG1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { var img = sender as ContentControl; if (img == null) { return; } var group = IMG.FindResource("Imageview") as TransformGroup; var transform = group.Children[1] as TranslateTransform; var transform2 = group.Children[0] as ScaleTransform; GeneralTransform generalTransform1 = IMG1.TransformToAncestor(borderWin); Point currentPoint = generalTransform1.Transform(new Point(0, 0)); double left = currentPoint.X; double top = currentPoint.Y; double right = borderWin.ActualWidth - left - IMG1.ActualWidth * transform2.ScaleX; double bottom = borderWin.ActualHeight - top - IMG1.ActualHeight * transform2.ScaleY; if (left >= 0 && right >= 0 && top >= 0 && bottom >= 0) { mouseDown = false; imgIsDown = true; this.DragMove(); } else { e.Handled = true; img.CaptureMouse(); mouseXY = e.GetPosition(img); imgIsDown = true; mouseDown = true; } }
/// <summary> /// 执行缩放操作 /// </summary> /// <param name="group"></param> /// <param name="point"></param> /// <param name="delta"></param> private void DowheelZoom(TransformGroup group, Point point, double delta) { var pointToContent = group.Inverse.Transform(point); //var pointToContent = ScaleCenter; var transform = group.Children[0] as ScaleTransform; if (transform.ScaleX + delta < 0.1) { return; } transform.ScaleX += delta; transform.ScaleY += delta; var transform1 = group.Children[1] as TranslateTransform; GeneralTransform generalTransform1 = IMG1.TransformToAncestor(borderWin); Point currentPoint = generalTransform1.Transform(new Point(0, 0)); double left = currentPoint.X; double top = currentPoint.Y; double right = borderWin.ActualWidth - left - IMG1.ActualWidth * transform.ScaleX; double bottom = borderWin.ActualHeight - top - IMG1.ActualHeight * transform.ScaleY; if (transform.ScaleX > 1 && delta < 0 && IMG1.ActualHeight >= this.MinHeight) { bool XIsContinue = true; bool YIsContinue = true; //-------X if (right >= 0 && left < 0) { transform1.X = borderWin.ActualWidth - IMG1.ActualWidth * transform.ScaleX; XIsContinue = false; } if (left >= 0 && right < 0) { transform1.X = 0; XIsContinue = false; } if (XIsContinue) { transform1.X = -1 * ((pointToContent.X * transform.ScaleX) - point.X); } //-------Y if (bottom >= 0 && top < 0) { transform1.Y = borderWin.ActualHeight - IMG1.ActualHeight * transform.ScaleY; YIsContinue = false; } if (top >= 0 && bottom < 0) { transform1.Y = 0; YIsContinue = false; } if (top < 0 && bottom < 0) { transform1.Y = -1 * ((pointToContent.Y * transform.ScaleY) - point.Y); } if (YIsContinue) { transform1.Y = -1 * ((pointToContent.Y * transform.ScaleY) - point.Y); } } else { transform1.X = -1 * ((pointToContent.X * transform.ScaleX) - point.X); transform1.Y = -1 * ((pointToContent.Y * transform.ScaleY) - point.Y); } this.gridProgress.Visibility = Visibility.Visible; int percent = (int)(transform.ScaleX * 100); if (percent == 100) { transform1.X = 0; transform1.Y = 0; transform.ScaleX = 1; transform.ScaleY = 1; } txtProgress.Text = string.Format("{0}%", percent); (this.Resources["ShowProgress"] as Storyboard).Begin(); }
/// <summary> /// 执行移动操作 /// </summary> /// <param name="img"></param> /// <param name="e"></param> private void Domousemove(MainWindow img, MouseEventArgs e) { if (e.LeftButton != MouseButtonState.Pressed) { return; } var group = IMG.FindResource("Imageview") as TransformGroup; var transform = group.Children[1] as TranslateTransform; var transformScale = group.Children[0] as ScaleTransform; var position = e.GetPosition(img); GeneralTransform generalTransform1 = IMG1.TransformToAncestor(borderWin); Point currentPoint = generalTransform1.Transform(new Point(0, 0)); double left = currentPoint.X; double top = currentPoint.Y; double right = borderWin.ActualWidth - left - IMG1.ActualWidth * transformScale.ScaleX; double bottom = borderWin.ActualHeight - top - IMG1.ActualHeight * transformScale.ScaleY; bool IsFresh = false; if ((mouseXY.X - position.X < 0 && left < 0) || (mouseXY.X - position.X > 0 && right < 0)) { if (this.MinWidth > IMG1.ActualWidth) { if (mouseXY.X - position.X < 0 && transform.X - mouseXY.X + position.X > 0) { transform.X = 0; } else if (mouseXY.X - position.X > 0 && right + mouseXY.X - position.X > 0) { transform.X = borderWin.ActualWidth - IMG1.ActualWidth * transformScale.ScaleX; } else { transform.X -= mouseXY.X - position.X; } IsFresh = true; } else { if (mouseXY.X - position.X < 0 && transform.X - mouseXY.X + position.X > 0) { transform.X = 0; } else if (mouseXY.X - position.X > 0 && right + mouseXY.X - position.X > 0) { transform.X = borderWin.ActualWidth - IMG1.ActualWidth * transformScale.ScaleX; } else { transform.X -= mouseXY.X - position.X; } IsFresh = true; } } if ((mouseXY.Y - position.Y < 0 && top < 0) || (mouseXY.Y - position.Y > 0 && bottom < 0)) { if (this.MinWidth > IMG1.ActualWidth) { if (mouseXY.Y - position.Y < 0 && transform.Y - mouseXY.Y + position.Y > 0) { transform.Y = 0; } else if (mouseXY.Y - position.Y > 0 && bottom + mouseXY.Y - position.Y > 0) { transform.Y = borderWin.ActualHeight - IMG1.ActualHeight * transformScale.ScaleY; } else { transform.Y -= mouseXY.Y - position.Y; } IsFresh = true; } else { if (mouseXY.Y - position.Y < 0 && transform.Y - mouseXY.Y + position.Y > 0) { transform.Y = 0; } else if (mouseXY.Y - position.Y > 0 && transform.Y - mouseXY.Y + position.Y < borderWin.ActualHeight - IMG1.ActualHeight * transformScale.ScaleY) { transform.Y = borderWin.ActualHeight - IMG1.ActualHeight * transformScale.ScaleY; } else { transform.Y -= mouseXY.Y - position.Y; } IsFresh = true; } } mouseXY = position; }