none
WPF关于图片定点缩放的问题 RRS feed

  • 问题

  • 最近做图片定点缩放遇到点问题,需要同时支持pc和触摸系统,看了网上很多例子,PC上可以用

    <Grid x:Name="MainPanel" >
                <Grid.Resources>
                    <TransformGroup x:Key="ImageTransformResource">
                        <ScaleTransform />
                        <TranslateTransform />
                    </TransformGroup>
                </Grid.Resources>

                <Rectangle x:Name="MasterImage"
                    MouseLeftButtonDown="MasterImage_MouseLeftButtonDown"
                    MouseLeftButtonUp="MasterImage_MouseLeftButtonUp"
                    MouseMove="MasterImage_MouseMove"
                    MouseWheel="MasterImage_MouseWheel"
                    ManipulationStarting="TouchPad_ManipulationStarting"
                    ManipulationDelta="TouchPad_ManipulationDelta"
                           IsManipulationEnabled="True">
                    <Rectangle.Fill>
                        <VisualBrush Transform="{StaticResource ImageTransformResource}" Stretch="Uniform">
                            <VisualBrush.Visual>
                                <Image Name="imgToDisplay"></Image>
                            </VisualBrush.Visual>
                        </VisualBrush>
                    </Rectangle.Fill>
                </Rectangle>
            </Grid>

       private void MasterImage_MouseWheel(object sender, MouseWheelEventArgs e)
            {
                System.Windows.Point centerPoint = e.GetPosition(MainPanel);
    
                TransformGroup group = MainPanel.FindResource("ImageTransformResource") as TransformGroup;
    
                ScaleTransform transform = group.Children[0] as ScaleTransform;
    
                transform.CenterX = this.ActualWidth / 2;//centerPoint.X;
                transform.CenterY = this.ActualHeight / 2;//centerPoint.Y;
    
                transform.ScaleX += e.Delta * 0.001;
                transform.ScaleY += e.Delta * 0.001;
    
                if (transform.ScaleX < 0.2 || transform.ScaleY < 0.2)
                {
                    transform.ScaleX += System.Math.Abs(e.Delta * 0.001);
                    transform.ScaleY += System.Math.Abs(e.Delta * 0.001);
                }
    
                if (transform.ScaleX > 2.0 || transform.ScaleY > 2.0)
                {
                    transform.ScaleX -= Math.Abs(e.Delta * 0.001);
                    transform.ScaleY -= Math.Abs(e.Delta * 0.001);
                }
            }
    
            private void MasterImage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                System.Windows.Shapes.Rectangle rectangle = sender as System.Windows.Shapes.Rectangle;
                if (rectangle == null)
    
                    return;
    
    
                rectangle.ReleaseMouseCapture();
                m_IsMouseLeftButtonDown = false;
            }
    
    
    
            private System.Windows.Point m_PreviousMousePoint;
    
            private void MasterImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                System.Windows.Shapes.Rectangle rectangle = sender as System.Windows.Shapes.Rectangle;
    
                if (rectangle == null)
    
                    return;
    
                rectangle.CaptureMouse();
    
                m_IsMouseLeftButtonDown = true;
                m_PreviousMousePoint = e.GetPosition(rectangle);
    
    
            }
    
    
    
            private void MasterImage_MouseMove(object sender, MouseEventArgs e)
            {
    
                System.Windows.Shapes.Rectangle rectangle = sender as System.Windows.Shapes.Rectangle;
    
                if (rectangle == null)
                    return;
    
    
    
                if (m_IsMouseLeftButtonDown)
                    DoImageMove(rectangle, e);
    
            }
    
    
    
            private void DoImageMove(System.Windows.Shapes.Rectangle rectangle, MouseEventArgs e)
            {
    
                //Debug.Assert(e.LeftButton == MouseButtonState.Pressed);
    
                if (e.LeftButton != MouseButtonState.Pressed)
    
                    return;
    
                TransformGroup group = MainPanel.FindResource("ImageTransformResource") as TransformGroup;
    
                TranslateTransform transform = group.Children[1] as TranslateTransform;
    
                System.Windows.Point position = e.GetPosition(rectangle);
    
                transform.X += position.X - m_PreviousMousePoint.X;
                transform.Y += position.Y - m_PreviousMousePoint.Y;
    
                m_PreviousMousePoint = position;
            }
    
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
    
    
    
                TransformGroup group = MainPanel.FindResource("ImageTransformResource") as TransformGroup;
                ScaleTransform transform = group.Children[0] as ScaleTransform;
                transform.CenterX = this.ActualWidth / 2;//centerPoint.X;
                transform.CenterY = this.ActualHeight / 2;//centerPoint.Y;
                transform.ScaleX = 0.8;
                transform.ScaleY = 0.8;
            }

    奇怪的是,在pc上鼠标操作很正常,但是在pad上触摸操作完全不行,连移动都失灵了。

    请问有什么好的办法兼容pad吗

    2016年8月19日 7:01

全部回复