Canvas "jiggles" 平移时

Canvas "jiggles" when panning

简介

我是 WPF 的新手,我正在尝试自己实现 Canvas 平移。

我已经设法找出必须应用的逻辑和机制,但在实施它们时我遇到了一个奇怪的行为。

问题

我的平移代码 "works" 好吧,这意味着图像在正确的方向上移动,但它在移动时 "jiggles"(或 "dances",如果你愿意的话)。

问题

相关信息:

下面是一个演示问题的小型演示应用程序。

XAML:

<Window x:Class="CanvasPanning.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">
    <Grid Background="Beige">
        <Canvas Name="canvas" 
                Width="200" 
                Height="200" 
                Background="Aqua"
                MouseLeftButtonDown="canvas_MouseLeftButtonDown" 
                MouseLeftButtonUp="canvas_MouseLeftButtonUp" 
                LostMouseCapture="canvas_LostMouseCapture" 
                MouseMove="canvas_MouseMove">
            <Ellipse Stroke="Black" Fill="Beige" Canvas.Left="50" Canvas.Top="50" Width="100" Height="50" />
            <Rectangle Stroke="Black" Fill="Red" Canvas.Left="50" Canvas.Top="120" Width="100" Height="50" />
        </Canvas>
    </Grid>
</Window>

后面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CanvasPanning
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Matrix m;
        private bool isPanning;
        private Point ptOldPanningPosition;

        public MainWindow()
        {
            InitializeComponent();

            isPanning = false;
            m = canvas.RenderTransform.Value;
        }

        private void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);

            if (isPanning) return;

            isPanning = canvas.CaptureMouse();

            if(isPanning)
            {
                ptOldPanningPosition = e.GetPosition(canvas);
            }

        }

        private void canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonUp(e);

            if (isPanning)
            {
                isPanning = false;
                canvas.ReleaseMouseCapture();
            }
        }

        private void canvas_LostMouseCapture(object sender, MouseEventArgs e)
        {
            isPanning = false;
            base.OnLostMouseCapture(e);
        }

        private void canvas_MouseMove(object sender, MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if(isPanning)
            {
                Point ptNewPanningPosition = e.GetPosition(canvas);

                Vector offset = ptNewPanningPosition - ptOldPanningPosition;

                m.OffsetX += offset.X;
                m.OffsetY += offset.Y;

                ptOldPanningPosition = ptNewPanningPosition;

                canvas.RenderTransform = new MatrixTransform(m);

            }
        }
    }
}

您正在采取相对于 canvas 的位置并且您正在移动 canvas,采取与 window 或正在发生拖动的包含元素相关的位置

在鼠标左下

if(isPanning)
{
    ptOldPanningPosition = e.GetPosition(this);
}

并且在 mousemove

if(isPanning)
{
    Point ptNewPanningPosition = e.GetPosition(this);
}