private void ZoomSliderOnValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { ZoomTransform.ScaleX = e.NewValue; ZoomTransform.ScaleY = e.NewValue; var centerOfViewport = new Point(ZoomViewer.ViewportWidth/2, ZoomViewer.ViewportHeight/2); _lastCenterPositionOnTarget = ZoomViewer.TranslatePoint(centerOfViewport, ZoomGrid); }
private void ZoomViewer_MouseMove(object sender, MouseEventArgs e) { if (_lastDragPoint.HasValue) { var posNow = e.GetPosition(ZoomViewer); double dX = posNow.X - _lastDragPoint.Value.X; double dY = posNow.Y - _lastDragPoint.Value.Y; _lastDragPoint = posNow; ZoomViewer.ScrollToHorizontalOffset(ZoomViewer.HorizontalOffset - dX); ZoomViewer.ScrollToVerticalOffset(ZoomViewer.VerticalOffset - dY); } }
private void ZoomViewer_ScrollChanged(object sender, ScrollChangedEventArgs e) { if (e.ExtentHeightChange != 0 || e.ExtentWidthChange != 0) { Point? targetBefore = null; Point? targetNow = null; if (!_lastMousePositionOnTarget.HasValue) { if (_lastCenterPositionOnTarget.HasValue) { var centerOfViewport = new Point(ZoomViewer.ViewportWidth/2, ZoomViewer.ViewportHeight/2); Point centerOfTargetNow = ZoomViewer.TranslatePoint(centerOfViewport, ZoomGrid); targetBefore = _lastCenterPositionOnTarget; targetNow = centerOfTargetNow; } } else { targetBefore = _lastMousePositionOnTarget; targetNow = Mouse.GetPosition(ZoomGrid); _lastMousePositionOnTarget = null; } if (targetBefore.HasValue) { double dXInTargetPixels = targetNow.Value.X - targetBefore.Value.X; double dYInTargetPixels = targetNow.Value.Y - targetBefore.Value.Y; double multiplicatorX = e.ExtentWidth/ZoomGrid.Width; double multiplicatorY = e.ExtentHeight/ZoomGrid.Height; double newOffsetX = ZoomViewer.HorizontalOffset - dXInTargetPixels*multiplicatorX; double newOffsetY = ZoomViewer.VerticalOffset - dYInTargetPixels*multiplicatorY; if (double.IsNaN(newOffsetX) || double.IsNaN(newOffsetY)) { return; } ZoomViewer.ScrollToHorizontalOffset(newOffsetX); ZoomViewer.ScrollToVerticalOffset(newOffsetY); } } }
private void ZoomViewer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { ZoomViewer.Cursor = Cursors.Arrow; ZoomViewer.ReleaseMouseCapture(); _lastDragPoint = null; }