Partager via


Comment : reconnaître des gestes d'application

L’exemple suivant montre comment effacer les entrées manuscrites lorsqu’un utilisateur effectue un ScratchOut mouvement sur un InkCanvas. Cet exemple suppose qu’un InkCanvas, appelé inkCanvas1, est déclaré dans le fichier XAML.

Exemple

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Collections.ObjectModel;

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();

        if (inkCanvas1.IsGestureRecognizerAvailable)
        {
            inkCanvas1.EditingMode = InkCanvasEditingMode.InkAndGesture;
            inkCanvas1.Gesture += new InkCanvasGestureEventHandler(inkCanvas1_Gesture);
            inkCanvas1.SetEnabledGestures(
                new ApplicationGesture[] { ApplicationGesture.ScratchOut });
        }
    }

    void inkCanvas1_Gesture(object sender, InkCanvasGestureEventArgs e)
    {
        ReadOnlyCollection<GestureRecognitionResult> gestureResults =
            e.GetGestureRecognitionResults();

        // Check the first recognition result for a gesture.
        if ((gestureResults[0].RecognitionConfidence ==
            RecognitionConfidence.Strong) &&
           (gestureResults[0].ApplicationGesture ==
            ApplicationGesture.ScratchOut))
        {
            StrokeCollection hitStrokes = inkCanvas1.Strokes.HitTest(
                                            e.Strokes.GetBounds(), 10);

            if (hitStrokes.Count > 0)
            {
                inkCanvas1.Strokes.Remove(hitStrokes);
            }
        }
    }
}
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Ink
Imports System.Collections.ObjectModel

Class Window1
    Inherits Window

    Public Sub New()
        InitializeComponent()

        If inkCanvas1.IsGestureRecognizerAvailable Then
            inkCanvas1.EditingMode = InkCanvasEditingMode.InkAndGesture
            AddHandler inkCanvas1.Gesture, AddressOf inkCanvas1_Gesture
            inkCanvas1.SetEnabledGestures(New ApplicationGesture() {ApplicationGesture.ScratchOut})
        End If

    End Sub

    Private Sub inkCanvas1_Gesture(ByVal sender As Object, ByVal e As InkCanvasGestureEventArgs)

        Dim gestureResults As ReadOnlyCollection(Of GestureRecognitionResult) = _
                              e.GetGestureRecognitionResults()

        ' Check the first recognition result for a gesture.
        If gestureResults(0).RecognitionConfidence = _
           RecognitionConfidence.Strong AndAlso _
           gestureResults(0).ApplicationGesture = _
           ApplicationGesture.ScratchOut Then

            Dim hitStrokes As StrokeCollection = _
                inkCanvas1.Strokes.HitTest(e.Strokes.GetBounds(), 10)

            If hitStrokes.Count > 0 Then
                inkCanvas1.Strokes.Remove(hitStrokes)
            End If
        End If

    End Sub
End Class

Voir aussi