c# – 简单的WPF平移;这段代码很接近但不太正确
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 简单的WPF平移;这段代码很接近但不太正确,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2537字,纯文字阅读大概需要4分钟。
内容图文
![c# – 简单的WPF平移;这段代码很接近但不太正确](/upload/InfoBanner/zyjiaocheng/785/4f19ec8967e4409fb499a42893a5e802.jpg)
我正在尝试使用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# – 简单的WPF平移;这段代码很接近但不太正确全部内容,希望文章能够帮你解决c# – 简单的WPF平移;这段代码很接近但不太正确所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。