Click here to Skip to main content
Click here to Skip to main content

WPF simple zoom and drag support in a ScrollViewer

By , 29 Jul 2010
 

Overview_Resized.png

Introduction

I've been looking the whole day for Open Source solutions that show a simple way of zooming and dragging arbitrary content which is hosted and managed by a ScrollViewer.

As I did not find a free one, I decided to write my own one. It supports zooming by a slider as well as by the mouse wheel.

Using the code

I will only post the whole code. I might post further comments during the next weeks.

If you have any questions regarding the usage, feel free to post. The code snippet is small enough so I hope no further explanations are necessary.

The main view is defined by the XAML below. The content that is to be zoomed and dragged is part of the "grid" control.

<Window x:Class="ZoomExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="MainWindow" Height="500" Width="500">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Slider Grid.Column="0" Orientation="Vertical" 
           HorizontalAlignment="Left" Minimum="1" x:Name="slider"/>
        <ScrollViewer Name="scrollViewer" Grid.Column="1" 
              VerticalScrollBarVisibility="Visible" 
              HorizontalScrollBarVisibility="Visible">
            
            <Grid Name="grid" Width="400" 
              Height="400" RenderTransformOrigin="0.5,0.5">
                <Grid.LayoutTransform>
                    <TransformGroup>
                        <ScaleTransform x:Name="scaleTransform"/>
                    </TransformGroup>
                </Grid.LayoutTransform>
                <Viewbox Grid.Column="0" Grid.Row="0">
                    <ContentPresenter Content="{StaticResource Kompass}"/>
                </Viewbox>
            </Grid>
            
        </ScrollViewer>
    </Grid>
</Window>

Zooming and dragging is managed by the code-behind:

public partial class MainWindow : Window
{
    Point? lastCenterPositionOnTarget;
    Point? lastMousePositionOnTarget;
    Point? lastDragPoint;

    public MainWindow()
    {
        InitializeComponent();

        scrollViewer.ScrollChanged += OnScrollViewerScrollChanged;
        scrollViewer.MouseLeftButtonUp += OnMouseLeftButtonUp;
        scrollViewer.PreviewMouseLeftButtonUp += OnMouseLeftButtonUp;
        scrollViewer.PreviewMouseWheel += OnPreviewMouseWheel;

        scrollViewer.PreviewMouseLeftButtonDown += OnMouseLeftButtonDown;
        scrollViewer.MouseMove += OnMouseMove;

        slider.ValueChanged += OnSliderValueChanged;
    }

    void OnMouseMove(object sender, MouseEventArgs e)
    {
        if (lastDragPoint.HasValue)
        {
            Point posNow = e.GetPosition(scrollViewer);

            double dX = posNow.X - lastDragPoint.Value.X;
            double dY = posNow.Y - lastDragPoint.Value.Y;

            lastDragPoint = posNow;

            scrollViewer.ScrollToHorizontalOffset(scrollViewer.HorizontalOffset - dX);
            scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - dY);
        }
    }

    void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var mousePos = e.GetPosition(scrollViewer);
        if (mousePos.X <= scrollViewer.ViewportWidth && mousePos.Y < 
            scrollViewer.ViewportHeight) //make sure we still can use the scrollbars
        {
            scrollViewer.Cursor = Cursors.SizeAll;
            lastDragPoint = mousePos;
            Mouse.Capture(scrollViewer);
        }
    }

    void OnPreviewMouseWheel(object sender, MouseWheelEventArgs e)
    {
        lastMousePositionOnTarget = Mouse.GetPosition(grid);

        if (e.Delta > 0)
        {
            slider.Value += 1;
        }
        if (e.Delta < 0)
        {
            slider.Value -= 1;
        }

        e.Handled = true;
    }

    void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        scrollViewer.Cursor = Cursors.Arrow;
        scrollViewer.ReleaseMouseCapture();
        lastDragPoint = null;
    }

    void OnSliderValueChanged(object sender, 
         RoutedPropertyChangedEventArgs<double> e)
    {
        scaleTransform.ScaleX = e.NewValue;
        scaleTransform.ScaleY = e.NewValue;

        var centerOfViewport = new Point(scrollViewer.ViewportWidth/2, 
                                         scrollViewer.ViewportHeight/2);
        lastCenterPositionOnTarget = scrollViewer.TranslatePoint(centerOfViewport, grid);
    }

    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(scrollViewer.ViewportWidth/2, 
                                                     scrollViewer.ViewportHeight/2);
                    Point centerOfTargetNow = 
                          scrollViewer.TranslatePoint(centerOfViewport, grid);

                    targetBefore = lastCenterPositionOnTarget;
                    targetNow = centerOfTargetNow;
                }
            }
            else
            {
                targetBefore = lastMousePositionOnTarget;
                targetNow = Mouse.GetPosition(grid);

                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/grid.Width;
                double multiplicatorY = e.ExtentHeight/grid.Height;

                double newOffsetX = scrollViewer.HorizontalOffset - 
                                    dXInTargetPixels*multiplicatorX;
                double newOffsetY = scrollViewer.VerticalOffset - 
                                    dYInTargetPixels*multiplicatorY;

                if (double.IsNaN(newOffsetX) || double.IsNaN(newOffsetY))
                {
                    return;
                }

                scrollViewer.ScrollToHorizontalOffset(newOffsetX);
                scrollViewer.ScrollToVerticalOffset(newOffsetY);
            }
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Kevin Stumpf
Germany Germany
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionSplendid!memberPorgram Lover24 Feb '13 - 11:34 
GeneralMy vote of 5memberarturo134631 Oct '12 - 5:37 
GeneralMy vote of 5memberMember 406769614 Jun '12 - 23:21 
GeneralMy vote of 5membernjdnjdnjdnjdnjd16 May '12 - 6:27 
GeneralMy vote of 5memberDezfoul18 Apr '12 - 19:29 
QuestionCreated some helper classes [modified]memberkgoulding18 Jan '12 - 10:03 
GeneralMy vote of 5memberUbloobok22 Oct '11 - 21:46 
GeneralMy vote of 5memberKoss8712 Jun '11 - 14:22 
GeneralThanks for the Demo!member(noor)31 Mar '11 - 3:49 
GeneralSounds familiar...memberAshley Davis30 Jul '10 - 3:03 
GeneralLike it, been there too, I did a Friction one toomvpSacha Barber29 Jul '10 - 23:11 
GeneralProblem to downloadmembersinun29 Jul '10 - 11:46 
GeneralRe: Problem to downloadmemberD-Lay29 Jul '10 - 12:25 
GeneralFixedmemberKevin Stumpf29 Jul '10 - 14:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 29 Jul 2010
Article Copyright 2010 by Kevin Stumpf
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid