gpt4 book ai didi

c# - WPF inkcanvas 多点触控?

转载 作者:行者123 更新时间:2023-12-02 04:21:26 27 4
gpt4 key购买 nike

我正在为触摸屏创建一个简单的绘画应用程序,我一直在与多点触控手势进行一些斗争,以便在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com