缩放转换应用翻译效果



我想创建一个可以用鼠标滚作的缩放控件。向上滚动 ->放大;向下滚动 ->缩小。
此外,我希望缩放中心位于鼠标指针所在的任何位置。这样人们总是可以在鼠标光标所在的位置放大。

看起来很简单,但我无法开始工作。
我正在使用Grid并对其应用ScaleTransform

当我第一次放大时,它会工作并放大该特定位置。但是,如果我将光标移动到其他位置并尝试放大一点,Grid就会偏移,并且我的初始中心会关闭。

是什么原因造成的?如何解决这个问题?

我的代码:

Class MainWindow 
Dim trans As New ScaleTransform
Dim Scale As Double = 1
Private Sub DefGrid_MouseWheel(sender As Object, e As MouseWheelEventArgs) Handles DefGrid.MouseWheel
    If e.Delta > 0 Then
        Scale = Scale + 0.1
    End If
    If e.Delta < 0 Then
        Scale = Scale - 0.1
    End If
    trans.CenterX = e.GetPosition(DefGrid).X
    trans.CenterY = e.GetPosition(DefGrid).Y
    trans.ScaleX = Scale
    trans.ScaleY = Scale
    DefGrid.RenderTransform = trans
End Sub
End Class

和 xaml:

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" PreviewMouseWheel="Window_PreviewMouseWheel">
<Grid x:Name="DefGrid" HorizontalAlignment="Left" Height="291" Margin="19,10,0,0" VerticalAlignment="Top" Width="475">
    <Canvas HorizontalAlignment="Left" Height="254" Margin="137,10,0,0" VerticalAlignment="Top" Width="195">
        <Canvas.Background>
            <ImageBrush ImageSource="TestImage.jpg"/>
        </Canvas.Background>
    </Canvas>
</Grid>
</Window>

网格上有一个画布图像,仅供参考。

不幸的是,

这有点复杂。如果您认为不需要滚动条,请检查此处的答案(我找不到特定于 VB 的示例,所以这些是 C#):平移和缩放图像如果您还需要滚动条,则需要滚动到鼠标指针。在这种情况下,您需要这样的东西:http://www.codeproject.com/Articles/97871/WPF-simple-zoom-and-drag-support-in-a-ScrollViewer

最新更新