void MainCanvas_MouseWheel(object sender, MouseWheelEventArgs e) { const double ScaleRate = 1.1; Point mouseAtMainCanvas = e.GetPosition(MainCanvas); Point mouseAtScrollViewer = e.GetPosition(MainScrollViewer); ScaleTransform st = MainCanvas.LayoutTransform as ScaleTransform; if (e.Delta > 0 && st.ScaleX < 64) { st.ScaleX = st.ScaleY = st.ScaleX * ScaleRate; } else if (st.ScaleX > 0.2) { st.ScaleX = st.ScaleY = st.ScaleX / ScaleRate; } MainScrollViewer.ScrollToHorizontalOffset(0); MainScrollViewer.ScrollToVerticalOffset(0); UpdateLayout(); Vector offset = MainCanvas.TranslatePoint(mouseAtMainCanvas, MainScrollViewer) - mouseAtScrollViewer; MainScrollViewer.ScrollToHorizontalOffset(offset.X); MainScrollViewer.ScrollToVerticalOffset(offset.Y); UpdateLayout(); e.Handled = true; }
private void MainScrollViewer_MouseMove(object sender, MouseEventArgs e) { Cursor = Cursors.Arrow; if (e.MiddleButton == MouseButtonState.Pressed) { MainScrollViewer.ScrollToHorizontalOffset(MainScrollViewer.HorizontalOffset + MiddleMousePressPoint.X - e.GetPosition(MainScrollViewer).X); MainScrollViewer.ScrollToVerticalOffset(MainScrollViewer.VerticalOffset + MiddleMousePressPoint.Y - e.GetPosition(MainScrollViewer).Y); MiddleMousePressPoint = e.GetPosition(MainScrollViewer); Cursor = Cursors.ScrollAll; } }
void OnMouseMove(object sender, MouseEventArgs e) { if (lastDragPoint.HasValue) { Point posNow = e.GetPosition(MainScrollViewer); double dX = posNow.X - lastDragPoint.Value.X; double dY = posNow.Y - lastDragPoint.Value.Y; lastDragPoint = posNow; MainScrollViewer.ScrollToHorizontalOffset(MainScrollViewer.HorizontalOffset - dX); MainScrollViewer.ScrollToVerticalOffset(MainScrollViewer.VerticalOffset - dY); } }
void OnScrollViewerScrollChanged(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(MainScrollViewer.ViewportWidth / 2, MainScrollViewer.ViewportHeight / 2); Point centerOfTargetNow = MainScrollViewer.TranslatePoint(centerOfViewport, MainCanvas); targetBefore = lastCenterPositionOnTarget; targetNow = centerOfTargetNow; } } else { targetBefore = lastMousePositionOnTarget; targetNow = Mouse.GetPosition(MainCanvas); 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 / MainCanvas.Width; double multiplicatorY = e.ExtentHeight / MainCanvas.Height; double newOffsetX = MainScrollViewer.HorizontalOffset - dXInTargetPixels * multiplicatorX; double newOffsetY = MainScrollViewer.VerticalOffset - dYInTargetPixels * multiplicatorY; if (double.IsNaN(newOffsetX) || double.IsNaN(newOffsetY)) { return; } MainScrollViewer.ScrollToHorizontalOffset(newOffsetX); MainScrollViewer.ScrollToVerticalOffset(newOffsetY); } } }
private void ViewCenter() { MainScrollViewer.ScrollToHorizontalOffset(MainCanvas.ActualWidth / 2 - 450); }