Comment : reconnaître des gestes d'application
Mise à jour : novembre 2007
L'exemple suivant montre comment effacer l'encre lorsqu'un utilisateur fait un geste ScratchOut sur un InkCanvas. Cet exemple suppose qu'un InkCanvas, appelé inkCanvas1, est déclaré dans le fichier XAML.
Exemple
Imports System
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 'New
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 'inkCanvas1_Gesture
End Class 'Window1
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);
}
}
}
}