Intercettazione dell'input dello stilo

L'architettura System.Windows.Input.StylusPlugIns fornisce un meccanismo per l'implementazione di un controllo di basso livello sull'input Stylus e sulla creazione di oggetti input penna Stroke digitali. La StylusPlugIn classe fornisce un meccanismo per implementare un comportamento personalizzato e applicarlo al flusso di dati provenienti dal dispositivo stilo per ottenere prestazioni ottimali.

In questo argomento sono contenute le seguenti sottosezioni:

Architettura

StylusPlugIn è l'evoluzione delle API StylusInput, descritte in Accesso e modifica dell'input penna.

Ogni UIElement oggetto ha una StylusPlugIns proprietà che è un oggetto StylusPlugInCollection. È possibile aggiungere un oggetto StylusPlugIn alla proprietà di StylusPlugIns un elemento per modificare i StylusPoint dati man mano che vengono generati. StylusPointi dati sono costituiti da tutte le proprietà supportate dal digitalizzatore di sistema, inclusi i dati dei punti e Y , nonché PressureFactor i X dati.

Gli StylusPlugIn oggetti vengono inseriti direttamente nel flusso di dati provenienti dal Stylus dispositivo quando si aggiunge alla StylusPlugInStylusPlugIns proprietà . L'ordine in cui i plug-in vengono aggiunti alla StylusPlugIns raccolta determina l'ordine in cui riceveranno StylusPoint i dati. Ad esempio, se si aggiunge un plug-in di filtro che limita l'input a una determinata area e quindi si aggiunge un plug-in che riconosce i movimenti mentre vengono scritti, il plug-in che riconosce i movimenti riceverà dati filtrati StylusPoint .

Implementazione di plug-in stilo

Per implementare un plug-in, derivare una classe da StylusPlugIn. Questa classe viene applicata o il flusso di dati in quanto proviene da Stylus. In questa classe è possibile modificare i valori dei StylusPoint dati.

Attenzione

Se un StylusPlugIn genera o causa un'eccezione, l'applicazione verrà chiusa. È consigliabile testare accuratamente i controlli che utilizzano e StylusPlugIn usano un controllo solo se si è certi che non genererà un'eccezione StylusPlugIn .

L'esempio seguente illustra un plug-in che limita l'input dello stilo modificando i X valori e Y nei StylusPoint dati man mano che provengono dal Stylus dispositivo.

using System;
using System.Windows.Media;
using System.Windows;
using System.Windows.Input.StylusPlugIns;
using System.Windows.Input;
using System.Windows.Ink;
Imports System.Windows.Media
Imports System.Windows
Imports System.Windows.Input.StylusPlugIns
Imports System.Windows.Input
Imports System.Windows.Ink
// A StylusPlugin that restricts the input area.
class FilterPlugin : StylusPlugIn
{
    protected override void OnStylusDown(RawStylusInput rawStylusInput)
    {
        // Call the base class before modifying the data.
        base.OnStylusDown(rawStylusInput);

        // Restrict the stylus input.
        Filter(rawStylusInput);
    }

    protected override void OnStylusMove(RawStylusInput rawStylusInput)
    {
        // Call the base class before modifying the data.
        base.OnStylusMove(rawStylusInput);

        // Restrict the stylus input.
        Filter(rawStylusInput);
    }

    protected override void OnStylusUp(RawStylusInput rawStylusInput)
    {
        // Call the base class before modifying the data.
        base.OnStylusUp(rawStylusInput);

        // Restrict the stylus input
        Filter(rawStylusInput);
    }

    private void Filter(RawStylusInput rawStylusInput)
    {
        // Get the StylusPoints that have come in.
        StylusPointCollection stylusPoints = rawStylusInput.GetStylusPoints();

        // Modify the (X,Y) data to move the points
        // inside the acceptable input area, if necessary.
        for (int i = 0; i < stylusPoints.Count; i++)
        {
            StylusPoint sp = stylusPoints[i];
            if (sp.X < 50) sp.X = 50;
            if (sp.X > 250) sp.X = 250;
            if (sp.Y < 50) sp.Y = 50;
            if (sp.Y > 250) sp.Y = 250;
            stylusPoints[i] = sp;
        }

        // Copy the modified StylusPoints back to the RawStylusInput.
        rawStylusInput.SetStylusPoints(stylusPoints);
    }
}
' A StylusPlugin that restricts the input area.
Class FilterPlugin
    Inherits StylusPlugIn

    Protected Overrides Sub OnStylusDown(ByVal rawStylusInput As RawStylusInput)
        ' Call the base class before modifying the data.
        MyBase.OnStylusDown(rawStylusInput)

        ' Restrict the stylus input.
        Filter(rawStylusInput)

    End Sub


    Protected Overrides Sub OnStylusMove(ByVal rawStylusInput As RawStylusInput)
        ' Call the base class before modifying the data.
        MyBase.OnStylusMove(rawStylusInput)

        ' Restrict the stylus input.
        Filter(rawStylusInput)

    End Sub


    Protected Overrides Sub OnStylusUp(ByVal rawStylusInput As RawStylusInput)
        ' Call the base class before modifying the data.
        MyBase.OnStylusUp(rawStylusInput)

        ' Restrict the stylus input
        Filter(rawStylusInput)

    End Sub


    Private Sub Filter(ByVal rawStylusInput As RawStylusInput)
        ' Get the StylusPoints that have come in.
        Dim stylusPoints As StylusPointCollection = rawStylusInput.GetStylusPoints()

        ' Modify the (X,Y) data to move the points 
        ' inside the acceptable input area, if necessary.
        Dim i As Integer
        For i = 0 To stylusPoints.Count - 1
            Dim sp As StylusPoint = stylusPoints(i)
            If sp.X < 50 Then
                sp.X = 50
            End If
            If sp.X > 250 Then
                sp.X = 250
            End If
            If sp.Y < 50 Then
                sp.Y = 50
            End If
            If sp.Y > 250 Then
                sp.Y = 250
            End If
            stylusPoints(i) = sp
        Next i

        ' Copy the modified StylusPoints back to the RawStylusInput.
        rawStylusInput.SetStylusPoints(stylusPoints)

    End Sub
End Class

Aggiunta del plug-in a un inkCanvas

Il modo più semplice per usare il plug-in personalizzato consiste nell'implementare una classe che deriva da InkCanvas e aggiungerla alla StylusPlugIns proprietà .

Nell'esempio seguente viene illustrato un oggetto personalizzato InkCanvas che filtra l'input penna.

public class FilterInkCanvas : InkCanvas
{
    FilterPlugin filter = new FilterPlugin();

    public FilterInkCanvas()
        : base()
    {
        this.StylusPlugIns.Add(filter);
    }
}

Se si aggiunge un oggetto FilterInkCanvas all'applicazione ed eseguirlo, si noterà che l'input penna non è limitato a un'area fino al completamento di un tratto da parte dell'utente. Ciò è dovuto al fatto che ha InkCanvas una DynamicRenderer proprietà , che è un StylusPlugIn oggetto e è già un membro dell'insieme StylusPlugIns . L'oggetto StylusPlugIns personalizzato StylusPlugIn aggiunto alla raccolta riceve i StylusPoint dati dopo DynamicRenderer aver ricevuto i dati. Di conseguenza, i StylusPoint dati non verranno filtrati fino a quando l'utente solleva la penna per terminare un tratto. Per filtrare l'input penna quando l'utente lo disegna, è necessario inserire prima FilterPlugin di DynamicRenderer.

Il codice C# seguente illustra un oggetto personalizzato InkCanvas che filtra l'input penna durante la creazione.

public class DynamicallyFilteredInkCanvas : InkCanvas
{
    FilterPlugin filter = new FilterPlugin();

    public DynamicallyFilteredInkCanvas()
        : base()
    {
        int dynamicRenderIndex =
            this.StylusPlugIns.IndexOf(this.DynamicRenderer);

        this.StylusPlugIns.Insert(dynamicRenderIndex, filter);
    }
}

Conclusione

Derivando le proprie StylusPlugIn classi e inserendole in StylusPlugInCollection raccolte, è possibile migliorare notevolmente il comportamento dell'input penna digitale. È possibile accedere ai StylusPoint dati man mano che vengono generati, offrendo la possibilità di personalizzare l'input Stylus . Poiché si ha un accesso di basso livello ai dati, è possibile implementare la raccolta e il rendering dell'input penna con prestazioni ottimali per l'applicazione StylusPoint .

Vedi anche