作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为触摸屏创建一个简单的绘画应用程序,我一直在与多点触控手势进行一些斗争,以便在 inkcanvas 中缩放和旋转,但我还没有找到解决方案。
有没有一种在 inkcanvas 中提供多点触控手势的简单方法?
最佳答案
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 WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Dictionary<int, System.Windows.Ink.Stroke> myStrokes;
public MainWindow()
{
InitializeComponent();
myStrokes = new Dictionary<int, System.Windows.Ink.Stroke>();
this.WindowState = System.Windows.WindowState.Maximized;
SandyCanvas.TouchDown += SandyCanvas_OnTouchDown;
SandyCanvas.TouchUp += SandyCanvas_OnTouchUp;
SandyCanvas.TouchMove += SandyCanvas_OnTouchMove;
}
private void SandyCanvas_OnTouchMove(object sender, TouchEventArgs e)
{
var touchPoint = e.GetTouchPoint(this);
var point = touchPoint.Position;
if (myStrokes.ContainsKey(touchPoint.TouchDevice.Id))
{
var stroke = myStrokes[touchPoint.TouchDevice.Id];
var nearbyPoint = CheckPointNearby(stroke, point);
if (!nearbyPoint)
{
stroke.StylusPoints.Add(new StylusPoint(point.X, point.Y));
}
}
}
private static bool CheckPointNearby(System.Windows.Ink.Stroke stroke, Point point)
{
return stroke.StylusPoints.Any(p => (Math.Abs(p.X - point.X) <= 1) && (Math.Abs(p.Y - point.Y) <= 1));
}
private void SandyCanvas_OnTouchUp(object sender, TouchEventArgs e)
{
var touchPoint = e.GetTouchPoint(this);
myStrokes.Remove(touchPoint.TouchDevice.Id);
}
private void SandyCanvas_OnTouchDown(object sender, TouchEventArgs e)
{
var touchPoint = e.GetTouchPoint(this);
var point = touchPoint.Position;
System.Windows.Ink.Stroke newStroke = new System.Windows.Ink.Stroke(new StylusPointCollection(new List<Point> { point }), SandyCanvas.DefaultDrawingAttributes);
if (!myStrokes.ContainsKey(touchPoint.TouchDevice.Id))
{
myStrokes.Add(touchPoint.TouchDevice.Id, newStroke);
SandyCanvas.Strokes.Add(newStroke);
}
}
}
}
/////////////////////Designer Page////////////////////
<Window x:Class="WpfApplication1.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>
<InkCanvas x:Name="SandyCanvas"></InkCanvas>
</Grid>
</Window>
关于c# - WPF inkcanvas 多点触控?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30192330/
我是一名优秀的程序员,十分优秀!