在WPF中的平移缩放都是通过RenderTransform这个类来实现这些效果的,在这个类中,除了平移和缩放还有旋转、扭曲变换、矩阵变换。这些都差不多的,都是坐标的变换。
这里我就先简单弄个平移和缩放吧:
平移呢就是以原来的对象为坐标原点(0,0),然后向X轴、Y轴进行平移变换。缩放呢有几个属性,ScaleX、ScaleY属性表示对象在X、Y轴进行缩放的倍数,CenterX 和 CenterY属性指定一个中心点。
下面有一个平移和缩放的简单的demo,用鼠标拖图片放进行平移,点击按钮来进行缩放,同时在缩放时让图片回到窗口的中央:
<Grid Name="grid"> <Grid.Resources> <TransformGroup x:Key="trasformView"> <ScaleTransform /> <TranslateTransform /> </TransformGroup> </Grid.Resources> <ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled" Focusable="False"> <ContentControl MouseLeftButtonDown="OnMouseLeftButtonDown" MouseLeftButtonUp="OnMouseLeftButtonUp" MouseMove="OnMouseMove"> <Image Name="Image1" Source="Images/Picture.jpg" Stretch="Uniform" RenderTransform="{StaticResource trasformView}" /> </ContentControl> </ScrollViewer> <StackPanel HorizontalAlignment="Right" VerticalAlignment="Bottom"> <Button Content="放大" Click="Button_Click_1" /> <Button Content="缩小" Click="Button_Click_2" /> </StackPanel> </Grid>