3

I'm creating a simple paint application for a touchscreen, and I have been fighting a little bit with multi touch gestures for zooming and rotating inside an inkcanvas, but I haven't find a solution for this.

Is there a simple way of providing multitouch gestures in inkcanvas?

  • 2
    There is a [SurfaceInkCanvas](https://msdn.microsoft.com/en-us/library/microsoft.surface.presentation.controls.surfaceinkcanvas.aspx) in the Surface 2.0 SDK Library. It is however a little tricky to get the SDK installed in a Visual Studio higher than 2010. – Clemens May 12 '15 at 13:38
  • Maybe it could work...I should look for information about how to install it (I use visual studio 2013). Is it compatible with mouse clicks too? –  May 12 '15 at 18:01
  • Not sure, but it should also work with mouse input. For the installation you might be able to patch the MSI with a tool like Orca. One major drawback of the Surface SDK is that you also need to have the Surface Runtime installed on the target system. – Clemens May 12 '15 at 18:08
  • Oh...seems like a little bit of a mess...I will look for more information, but I have seen some people being able to implement multi touch easily but it's a chaos for me –  May 12 '15 at 21:38

1 Answers1

5
    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>
Sandeep Jadhav
  • 815
  • 1
  • 10
  • 27
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – abdul Sep 04 '18 at 08:54
  • 1
    this code is for multi touch environment. if you are drawing on canvas with 5 Figures then 5 lines will draw at the same time. means multi touch line can be achieve. – Sandeep Jadhav Sep 04 '18 at 09:02
  • there is designer and .cs code added. you can check and run code on Simulator or multi touch system. – Sandeep Jadhav Sep 04 '18 at 09:04
  • just sample code given for achieving multi touch drawing on InkCanvas. – Sandeep Jadhav Sep 04 '18 at 09:04
  • if you want to handle multi touch gestures on ink-canvas you need to handle manually... but if you want single touch gestures on Ink-canvas there is EditMode you have to set ie.InkCanvasEditingMode.InkAndGesture. – Sandeep Jadhav Sep 04 '18 at 09:13