/// <summary> /// Event raised on mouse down in the ZoomAndPanControl. /// </summary> protected override void OnMouseDown(MouseButtonEventArgs e) { base.OnMouseLeftButtonDown(e); GetZoomAndPanControl().SaveZoom(); _mouseHandlingMode = MouseHandlingModeEnum.Panning; _origContentMouseDownPoint = e.GetPosition(_viewportCanvas); if ((Keyboard.Modifiers & ModifierKeys.Shift) != 0) { // Shift + left- or right-down initiates zooming mode. _mouseHandlingMode = MouseHandlingModeEnum.DragZooming; _dragBorder.Visibility = Visibility.Hidden; _sizingBorder.Visibility = Visibility.Visible; Canvas.SetLeft(_sizingBorder, _origContentMouseDownPoint.X); Canvas.SetTop(_sizingBorder, _origContentMouseDownPoint.Y); _sizingBorder.Width = 0; _sizingBorder.Height = 0; } else { // Just a plain old left-down initiates panning mode. _mouseHandlingMode = MouseHandlingModeEnum.Panning; } if (_mouseHandlingMode != MouseHandlingModeEnum.None) { // Capture the mouse so that we eventually receive the mouse up event. _viewportCanvas.CaptureMouse(); e.Handled = true; } }
/// <summary> /// Event raised on mouse down in the ZoomAndPanControl. /// </summary> protected override void OnMouseDown(MouseButtonEventArgs e) { base.OnMouseDown(e); SaveZoom(); _content.Focus(); Keyboard.Focus(_content); _mouseButtonDown = e.ChangedButton; _origZoomAndPanControlMouseDownPoint = e.GetPosition(this); _origContentMouseDownPoint = e.GetPosition(_content); if ((Keyboard.Modifiers & ModifierKeys.Shift) != 0 && (e.ChangedButton == MouseButton.Left || e.ChangedButton == MouseButton.Right)) { // Shift + left- or right-down initiates zooming mode. _mouseHandlingMode = MouseHandlingModeEnum.Zooming; } else if (_mouseButtonDown == MouseButton.Left) { // Just a plain old left-down initiates panning mode. _mouseHandlingMode = MouseHandlingModeEnum.Panning; } if (_mouseHandlingMode != MouseHandlingModeEnum.None) { // Capture the mouse so that we eventually receive the mouse up event. this.CaptureMouse(); } }
/// <summary> /// Event raised on mouse up in the ZoomAndPanControl. /// </summary> protected override void OnMouseUp(MouseButtonEventArgs e) { base.OnMouseUp(e); if (_mouseHandlingMode != MouseHandlingModeEnum.None) { if (_mouseHandlingMode == MouseHandlingModeEnum.Zooming) { if (_mouseButtonDown == MouseButton.Left) { // Shift + left-click zooms in on the content. ZoomIn(_origContentMouseDownPoint); } else if (_mouseButtonDown == MouseButton.Right) { // Shift + left-click zooms out from the content. ZoomOut(_origContentMouseDownPoint); } } else if (_mouseHandlingMode == MouseHandlingModeEnum.DragZooming) { var finalContentMousePoint = e.GetPosition(_content); // When drag-zooming has finished we zoom in on the rectangle that was highlighted by the user. ApplyDragZoomRect(finalContentMousePoint); } this.ReleaseMouseCapture(); _mouseHandlingMode = MouseHandlingModeEnum.None; } }
/// <summary> /// Event raised on mouse move in the ZoomAndPanControl. /// </summary> protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); var oldContentMousePoint = MousePosition; var curContentMousePoint = e.GetPosition(_content); MousePosition = curContentMousePoint.FilterClamp(_content.ActualWidth - 1, _content.ActualHeight - 1); OnPropertyChanged(new DependencyPropertyChangedEventArgs(MousePositionProperty, oldContentMousePoint, curContentMousePoint)); if (_mouseHandlingMode == MouseHandlingModeEnum.Panning) { // // The user is left-dragging the mouse. // Pan the viewport by the appropriate amount. // var dragOffset = curContentMousePoint - _origContentMouseDownPoint; this.ContentOffsetX -= dragOffset.X; this.ContentOffsetY -= dragOffset.Y; e.Handled = true; } else if (_mouseHandlingMode == MouseHandlingModeEnum.Zooming) { var curZoomAndPanControlMousePoint = e.GetPosition(this); var dragOffset = curZoomAndPanControlMousePoint - _origZoomAndPanControlMouseDownPoint; double dragThreshold = 10; if (_mouseButtonDown == MouseButton.Left && (Math.Abs(dragOffset.X) > dragThreshold || Math.Abs(dragOffset.Y) > dragThreshold)) { // // When Shift + left-down zooming mode and the user drags beyond the drag threshold, // initiate drag zooming mode where the user can drag out a rectangle to select the area // to zoom in on. // _mouseHandlingMode = MouseHandlingModeEnum.DragZooming; InitDragZoomRect(_origContentMouseDownPoint, curContentMousePoint); } } else if (_mouseHandlingMode == MouseHandlingModeEnum.DragZooming) { // // When in drag zooming mode continously update the position of the rectangle // that the user is dragging out. // curContentMousePoint = e.GetPosition(this); SetDragZoomRect(_origZoomAndPanControlMouseDownPoint, curContentMousePoint); } }
/// <summary> /// Event raised on mouse up in the ZoomAndPanControl. /// </summary> protected override void OnMouseUp(MouseButtonEventArgs e) { base.OnMouseLeftButtonUp(e); if (_mouseHandlingMode == MouseHandlingModeEnum.DragZooming) { var zoomAndPanControl = GetZoomAndPanControl(); var curContentPoint = e.GetPosition(_viewportCanvas); var rect = ViewportHelpers.Clip(curContentPoint, _origContentMouseDownPoint, new Point(0, 0), new Point(_viewportCanvas.Width, _viewportCanvas.Height)); zoomAndPanControl.AnimatedZoomTo(rect); _dragBorder.Visibility = Visibility.Visible; _sizingBorder.Visibility = Visibility.Hidden; } _mouseHandlingMode = MouseHandlingModeEnum.None; _viewportCanvas.ReleaseMouseCapture(); e.Handled = true; }