我想要创建一个缩放控件,可以用鼠标轮操作。向上滚动->放大;向下滚动->放大。
另外,我希望缩放中心位于鼠标指针所在的位置。这样就可以随时放大鼠标光标所在的位置。
看起来很简单,但我不能去工作了。
我正在使用一个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>网格上有一个画布图像,仅供参考。
发布于 2015-01-22 18:20:44
不幸的是这有点复杂。如果您认为不需要滚动条,请检查这里的答案(我找不到特定于VB的示例,所以这些是C#):Pan & Zoom Image,如果您也需要滚动条,您将需要滚动到鼠标指针。在这种情况下,您需要这样的东西:http://www.codeproject.com/Articles/97871/WPF-simple-zoom-and-drag-support-in-a-ScrollViewer
https://stackoverflow.com/questions/28095614
复制相似问题