/// <summary> /// Move the media element with the mouse. /// </summary> private void DropZone_PreviewMouseMove(object sender, MouseEventArgs e) { if (!DropZone.IsMouseCaptured) { return; } var scaleTransform = (ScaleTransform)((TransformGroup)VideoControl.RenderTransform).Children.First(tr => tr is ScaleTransform); var controlHeight = VideoControl.ActualHeight * scaleTransform.ScaleY; var controlWidth = VideoControl.ActualWidth * scaleTransform.ScaleX; Point relativePoint = VideoControl.TransformToVisual(DropZone).Transform(new Point(0, 0)); var mathRelativeY = Math.Round(relativePoint.Y, MidpointRounding.AwayFromZero); var mathRelativeX = Math.Round(relativePoint.X, MidpointRounding.AwayFromZero); var mathMouseY = Math.Round(MousePosition.Y, MidpointRounding.AwayFromZero); var mathMouseX = Math.Round(MousePosition.X, MidpointRounding.AwayFromZero); Vector vector = new Vector { X = 0, Y = 0 }; if (controlHeight >= DropZone.ActualHeight) { var vectorY = Math.Round(e.GetPosition(DropZone).Y, MidpointRounding.AwayFromZero) - mathMouseY; if (mathRelativeY <= 0 && mathRelativeY + vectorY <= 0 && vectorY > 0 || mathRelativeY + controlHeight >= DropZone.ActualHeight && mathRelativeY + controlHeight + vectorY >= DropZone.ActualHeight && vectorY < 0) { vector.Y = vectorY; } } if (controlWidth >= DropZone.ActualWidth) { var vectorX = Math.Round(e.GetPosition(DropZone).X, MidpointRounding.AwayFromZero) - mathMouseX; if (mathRelativeX <= 0 && mathRelativeX + vectorX <= 0 && vectorX > 0 || mathRelativeX + controlWidth >= DropZone.ActualWidth && mathRelativeX + controlWidth + vectorX >= DropZone.ActualWidth && vectorX < 0) { vector.X = vectorX; } } var translateTransform = (TranslateTransform)((TransformGroup)VideoControl.RenderTransform).Children.First(tr => tr is TranslateTransform); translateTransform.X = OriginPosition.X + vector.X; translateTransform.Y = OriginPosition.Y + vector.Y; MousePosition = e.GetPosition(DropZone); OriginPosition = new Point(Math.Round(translateTransform.X, MidpointRounding.AwayFromZero), Math.Round(translateTransform.Y, MidpointRounding.AwayFromZero)); }
/// <summary> /// Zoom feature implementation. /// </summary> private void DropZone_MouseWheel(object sender, MouseWheelEventArgs args) { if (!this.HasEffectiveKeyboardFocus) { return; } var scaleTransform = (ScaleTransform)((TransformGroup)VideoControl.RenderTransform).Children.First(tr => tr is ScaleTransform); var zoom = args.Delta > 0 ? ZoomRatio : -ZoomRatio; var relativePointCache = VideoControl.TranslatePoint(new Point(0, 0), DropZone); var translateTransform = (TranslateTransform)((TransformGroup)VideoControl.RenderTransform).Children.First(tr => tr is TranslateTransform); var height = VideoControl.ActualHeight * scaleTransform.ScaleY; var width = VideoControl.ActualWidth * scaleTransform.ScaleX; if (width + zoom < this.ActualWidth && height + zoom < this.ActualHeight && zoom < 0) { translateTransform.X = 0; translateTransform.Y = 0; return; } if (scaleTransform.ScaleX + zoom > ZoomMinTreshold && scaleTransform.ScaleX + zoom < ZoomMaxTreshold && scaleTransform.ScaleY + zoom > ZoomMinTreshold && scaleTransform.ScaleY + zoom < ZoomMaxTreshold) { scaleTransform.ScaleX += zoom; scaleTransform.ScaleY += zoom; } if (zoom > 0) { return; } height = VideoControl.ActualHeight * scaleTransform.ScaleY; width = VideoControl.ActualWidth * scaleTransform.ScaleX; var relativePoint = VideoControl.TransformToVisual(DropZone).Transform(new Point(0, 0)); var mathRelativePositionY = Math.Round(relativePoint.Y - relativePointCache.Y, MidpointRounding.AwayFromZero); var mathRelativePositionX = Math.Round(relativePoint.X - relativePointCache.X, MidpointRounding.AwayFromZero); var mathTranslateY = Math.Round(translateTransform.Y, MidpointRounding.AwayFromZero); var mathTranslateX = Math.Round(translateTransform.X, MidpointRounding.AwayFromZero); if (height < DropZone.ActualHeight) { translateTransform.Y = 0; } else if (relativePoint.Y > 0) { translateTransform.Y = mathTranslateY - mathRelativePositionY; } else if (relativePoint.Y + height < DropZone.ActualHeight) { translateTransform.Y = mathTranslateY + mathRelativePositionY; } if (width < DropZone.ActualWidth) { translateTransform.X = 0; } else if (relativePoint.X > 0) { translateTransform.X = mathTranslateX - mathRelativePositionX; } else if (relativePoint.X + width < DropZone.ActualWidth) { translateTransform.X = mathTranslateX + mathRelativePositionX; } }