Assigning Zoom Modes to Mouse Buttons
#1
It would be cool if I could assign zoom modes to any of the mouse buttons. For example, have the left button be the zoom rectangle and when I click and hold the right button, it will pan. I got it to change modes when I click the right button by handling the MouseRightButtonDown event but Zoom Panel only drags the image when the left button is held. I would like to be able to configure it to drag/pan it with the right button held also. Is there a way I can do that now or perhaps you could put this in the next release.

Thanks - Mark
#2
Mark, thank you for your recommendation.

I will surely consider your idea for the next version.

With the current version it is also possible to achieve the functionality you need. It is a little bit tricky.

I have changed the PainterSample.xaml.cs file that comes with ZoomPanel.

First add the following at the end of constructor:

Code:
this.PreviewMouseRightButtonDown += new MouseButtonEventHandler(PainterSample_PreviewMouseRightButtonDown);
            this.PreviewMouseRightButtonUp += new MouseButtonEventHandler(PainterSample_PreviewMouseRightButtonUp);
            this.MouseMove += new MouseEventHandler(PainterSample_MouseMove);

Than add the following:
Code:
private Point _lastMoveMousePosition;

        void PainterSample_MouseMove(object sender, MouseEventArgs e)
        {
            // Only process mouse move when we are moving the content with right mouse button
            if (ZoomPanel1.ZoomMode != Ab2d.Controls.ZoomPanel.ZoomModeType.Move || e.RightButton != MouseButtonState.Pressed)
                return;

            Point newMousePosition;
            Size actualViewboxSize;
            double dx, dy;

            // Get actual viewbox size from ZoomPanel - this represents the scale that is used to show the content of ZoomPanel
            // Note that we cannot use the ZoomPanel.Viewbox.Size becasue we need to adjust it for the different aspect of ZoomPanel and its content (this is done in GetActualViewboxSize)
            actualViewboxSize = GetActualViewboxSize();


            newMousePosition = e.GetPosition(ZoomPanel1);

            // Calculate dx and dy and take the current zoom ration in account
            dx = (newMousePosition.X - _lastMoveMousePosition.X) * actualViewboxSize.Width;
            dy = (newMousePosition.Y - _lastMoveMousePosition.Y) * actualViewboxSize.Height;


            // Disable animation and make the transition
            ZoomPanel1.IsAnimated = false;
            ZoomPanel1.Translate(dx, dy);
            ZoomPanel1.IsAnimated = true;


            _lastMoveMousePosition = newMousePosition;

            e.Handled = true;
        }

        private Size GetActualViewboxSize()
        {
            UIElement content;
            double zoomPanelAspectRatio, contentAspectRatio;
            double aspect;
            Size actualViewboxSize;

            content = ZoomPanel1.Content as UIElement;

            actualViewboxSize = ZoomPanel1.Viewbox.Size;

            if (content != null)
            {
                // We need to adjust the Viewbox size for the different aspect ratio of ZoomPanel and its content
                zoomPanelAspectRatio = ZoomPanel1.ActualWidth / ZoomPanel1.ActualHeight;
                contentAspectRatio = content.DesiredSize.Width / content.DesiredSize.Height;

                // Apsect ration between ZoomPanel and its content
                aspect = contentAspectRatio / zoomPanelAspectRatio;

                if (aspect > 1) // content wider than ZoomPanel
                    actualViewboxSize.Height *= aspect;
                else
                    actualViewboxSize.Width /= aspect;
            }

            return actualViewboxSize;
        }

I hope this solution will suite your needs until the next version of ZoomPanel.
#3
Thumbs Up 
Thank you so much!! I will try this out!! :D
  


Forum Jump:


Users browsing this thread:
1 Guest(s)