【问题标题】:WPF inkcanvas multi touch?WPF inkcanvas 多点触控?
【发布时间】:2015-05-12 13:27:32
【问题描述】:

我正在为触摸屏创建一个简单的绘图应用程序,并且我一直在努力使用多点触控手势在 inkcanvas 内进行缩放和旋转,但我还没有找到解决方案。

有没有一种在 inkcanvas 中提供多点触控手势的简单方法?

【问题讨论】:

  • Surface 2.0 SDK 库中有一个SurfaceInkCanvas。然而,在高于 2010 的 Visual Studio 中安装 SDK 有点棘手。
  • 也许它可以工作...我应该寻找有关如何安装它的信息(我使用 Visual Studio 2013)。是否也兼容鼠标点击?
  • 不确定,但它也应该适用于鼠标输入。对于安装,您可以使用 Orca 之类的工具修补 MSI。 Surface SDK 的一个主要缺点是您还需要在目标系统上安装 Surface Runtime。
  • 哦...看起来有点乱...我会寻找更多信息,但我看到有些人能够轻松实现多点触控,但这对我来说很混乱

标签: c# wpf multi-touch inkcanvas


【解决方案1】:
    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>

【讨论】:

  • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
  • 此代码适用于多点触控环境。如果您在画布上绘制 5 个图形,则将同时绘制 5 条线。表示可以实现多点触控。
  • 添加了设计师和.cs代码。您可以在模拟器或多点触控系统上检查和运行代码。
  • 只是给出了在 InkCanvas 上实现多点触控绘图的示例代码。
  • 如果你想在ink-canvas上处理多点触控手势,你需要手动处理......但是如果你想在Ink-canvas上进行单点触控手势,你必须设置EditMode ie.InkCanvasEditingMode。 InkAndGesture。
猜你喜欢
  • 1970-01-01
  • 2015-05-20
  • 2011-07-04
  • 2011-03-12
  • 1970-01-01
  • 2014-09-06
  • 2012-08-09
  • 1970-01-01
  • 2018-07-05
相关资源
最近更新 更多