画布在平移时“摇晃”
本文关键字:摇晃 | 更新日期: 2023-09-27 18:18:34
简介
我是 WPF 的新手,我正在尝试自己实现画布平移。
我已经设法弄清楚了必须应用的逻辑和机制,但是在实现它们时,我得到了一种奇怪的行为。
问题
我的平移代码"工作"得很好,这意味着图像正在朝着正确的方向移动,但它在移动时会"摇晃"(如果您愿意,可以"跳舞"(。
问题
- 为什么会这样?
- 如何解决?
相关信息:
下面是一个说明问题的小型演示应用程序。
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);
}
}
}
}
您正在相对于画布的位置,并且正在移动画布,采取与窗口或包含元素相关的位置,拖动正在发生
鼠标左下
if(isPanning)
{
ptOldPanningPosition = e.GetPosition(this);
}
在鼠标移动中
if(isPanning)
{
Point ptNewPanningPosition = e.GetPosition(this);
}