Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Zoom into mousepoint in Canvas

$
0
0

Hey guys,

I have a problem with the zooming in a canvas. The canvas is filled with elements and I want to zoom into an element when I put my mouse over the element and scroll with the mouse. With this code I can scroll and it zooms but it zooms into canvas point 0.0 (upperleft) and that's not what I want.

This is (a piece) of my XAML:

<ScrollViewer Grid.Row="1" x:Name="scrollViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"><Canvas x:Name="oCanvas" MouseWheel="oCanvas_MouseWheel" ><Canvas.RenderTransform><ScaleTransform x:Name="oCanvasScaleTransform"/></Canvas.RenderTransform></Canvas></ScrollViewer>


And this is the code for zooming:

    Private Const ScaleRate As Double = 1.1

    Private Sub oCanvas_MouseWheel(sender As Object, e As MouseWheelEventArgs)
        Try
            If e.Delta > 0 Then
                oCanvasScaleTransform.ScaleX *= ScaleRate
                oCanvasScaleTransform.ScaleY *= ScaleRate
            Else
                If oCanvasScaleTransform.ScaleX > 1 Then
                    oCanvasScaleTransform.ScaleX /= ScaleRate
                    oCanvasScaleTransform.ScaleY /= ScaleRate
                End If
            End If
            Dim p As Point = Mouse.GetPosition(oCanvas)
            scrollViewer.ScrollToHorizontalOffset(p.X)
            scrollViewer.ScrollToVerticalOffset(p.Y)
        Catch ex As Exception
End Try
    End Sub

Where do I go wrong ? :)

Cheers


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>