Compartilhar via


Como reconhecer gestos de aplicativo

O exemplo a seguir demonstra como apagar tinta quando um usuário faz um gesto de ScratchOut em um InkCanvas. Este exemplo pressupõe que um InkCanvas, chamado inkCanvas1, seja declarado no arquivo XAML.

Exemplo

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

Consulte também