ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-06-30 00:55:34  阅读:250  来源: 互联网

标签:c canvas wpf xaml 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 onm ouseLeftButtonDown(MouseButtonEventArgs e)
        {
            CaptureMouse();
            _last = e.GetPosition(canvas);
            base.OnMouseLeftButtonDown(e);
        }

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

        protected override void onm ouseMove(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>

解决方法:

这应该工作(插入bool变量isDragged)并替换

var pos = e.GetPosition(canvas);

var pos = e.GetPosition(this);

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

        isDragged = true;
    }

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

并且

protected override void onm ouseMove(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;

       }

    }

标签:c,canvas,wpf,xaml,panning
来源: https://codeday.me/bug/20190630/1331508.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有