简单的WPF平移;这段代码很接近但不太正确

时间:2012-09-26 05:03:35

标签: c# wpf xaml canvas panning

我正在尝试使用RenderTransform对WPF Canvas对象执行一些简单的平移。我希望能够按住鼠标按钮并拖动。使用以下代码,单击时会出现奇怪的跳跃。是什么导致的?其次,由于缺乏更好的术语,拖动被拖动的对象是“不稳定的”。那是为什么?

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 TestWpfZoom
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private Point _last;
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            CaptureMouse();
            _last = e.GetPosition(canvas);
            base.OnMouseLeftButtonDown(e);
        }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            ReleaseMouseCapture();
            base.OnMouseLeftButtonUp(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed && IsMouseCaptured)
            {
                var pos = e.GetPosition(canvas);
                var matrix = mt.Matrix; // it's a struct
                matrix.Translate(pos.X - _last.X, pos.Y - _last.Y);
                mt.Matrix = matrix;
                _last = pos;
            }
            base.OnMouseMove(e);
        }
    }
}

和XAML:

<Window x:Class="TestWpfZoom.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">
    <Canvas Name="canvas">
        <Canvas.RenderTransform>
            <MatrixTransform x:Name="mt"/>
        </Canvas.RenderTransform>
        <Ellipse Canvas.Left="100" Canvas.Top="100" Fill="Red" Width="100" Height="100"/>
    </Canvas>
</Window>

1 个答案:

答案 0 :(得分:3)

这应该有效(插入bool变量isDragged)并替换

  

var pos = e.GetPosition(canvas);

  

var pos = e.GetPosition(this);

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonDown(e);
        CaptureMouse();
        //_last = e.GetPosition(canvas);
        _last = e.GetPosition(this);

        isDragged = true;
    }

    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonUp(e);
        ReleaseMouseCapture();
        isDragged = false;
    }

以及

protected override void OnMouseMove(MouseEventArgs e)
    {
        if (isDragged == false)
            return;

        base.OnMouseMove(e);
        if (e.LeftButton == MouseButtonState.Pressed && IsMouseCaptured)
       {

            var pos = e.GetPosition(this);
            var matrix = mt.Matrix; // it's a struct
            matrix.Translate(pos.X - _last.X, pos.Y - _last.Y);
            mt.Matrix = matrix;
            _last = pos;

       }

    }