StylusPlugIn Classe

Definizione

Rappresenta un plug-in che può essere aggiunto alla proprietà di StylusPlugIns un controllo.

public ref class StylusPlugIn abstract
public abstract class StylusPlugIn
type StylusPlugIn = class
Public MustInherit Class StylusPlugIn
Ereditarietà
StylusPlugIn
Derivato

Esempio

Nell'esempio seguente viene creato un oggetto personalizzato StylusPlugIn che vincola l'input penna a una determinata area del controllo .

// EventArgs for the StrokeRendered event.
public class StrokeRenderedEventArgs : EventArgs
{
    StylusPointCollection strokePoints;

    public StrokeRenderedEventArgs(StylusPointCollection points)
    {
        strokePoints = points;
    }

    public StylusPointCollection StrokePoints
    {
        get
        {
            return strokePoints;
        }
    }
}

// EventHandler for the StrokeRendered event.
public delegate void StrokeRenderedEventHandler(object sender, StrokeRenderedEventArgs e);

// A StylusPlugin that restricts the input area
class FilterPlugin : StylusPlugIn
{
    StylusPointCollection collectedPoints;
    int currentStylus = -1;
    public event StrokeRenderedEventHandler StrokeRendered;

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

        if (currentStylus == -1)
        {
            StylusPointCollection pointsFromEvent = rawStylusInput.GetStylusPoints();

            // Create an emtpy StylusPointCollection to contain the filtered
            // points.
            collectedPoints = new StylusPointCollection(pointsFromEvent.Description);
            
            // Restrict the stylus input and add the filtered 
            // points to collectedPoints. 
            StylusPointCollection points = FilterPackets(pointsFromEvent);
            rawStylusInput.SetStylusPoints(points);
            collectedPoints.Add(points);

            currentStylus = rawStylusInput.StylusDeviceId;
        }
    }

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

        if (currentStylus == rawStylusInput.StylusDeviceId)
        {
            StylusPointCollection pointsFromEvent = rawStylusInput.GetStylusPoints();

            // Restrict the stylus input and add the filtered 
            // points to collectedPoints. 
            StylusPointCollection points = FilterPackets(pointsFromEvent);
            rawStylusInput.SetStylusPoints(points);
            collectedPoints.Add(points);
        }
    }

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

        if (currentStylus == rawStylusInput.StylusDeviceId)
        {
            StylusPointCollection pointsFromEvent = rawStylusInput.GetStylusPoints();

            // Restrict the stylus input and add the filtered 
            // points to collectedPoints. 
            StylusPointCollection points = FilterPackets(pointsFromEvent);
            rawStylusInput.SetStylusPoints(points);
            collectedPoints.Add(points);

            // Subscribe to the OnStylusUpProcessed method.
            rawStylusInput.NotifyWhenProcessed(collectedPoints);

            currentStylus = -1;
        }
    }

    private StylusPointCollection FilterPackets(StylusPointCollection stylusPoints)
    {
        // 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;
        }

        // Return the modified StylusPoints.
        return stylusPoints;
    }

    // This is called on the application thread.  
    protected override void OnStylusUpProcessed(object callbackData, bool targetVerified)
    {
        // Check that the element actually receive the OnStylusUp input.
        if (targetVerified)
        {
            StylusPointCollection strokePoints = callbackData as StylusPointCollection;

            if (strokePoints == null)
            {
                return;
            }

            // Raise the StrokeRendered event so the consumer of the plugin can
            // add the filtered stroke to its StrokeCollection.
            StrokeRenderedEventArgs e = new StrokeRenderedEventArgs(strokePoints);
            OnStrokeRendered(e);
        }
    }

    protected virtual void OnStrokeRendered(StrokeRenderedEventArgs e)
    {
        if (StrokeRendered != null)
        {
            StrokeRendered(this, e);
        }
    }
}
' EventArgs for the StrokeRendered event.
Public Class StrokeRenderedEventArgs
    Inherits EventArgs

    Private currentStrokePoints As StylusPointCollection

    Public Sub New(ByVal points As StylusPointCollection)

        currentStrokePoints = points

    End Sub


    Public ReadOnly Property StrokePoints() As StylusPointCollection
        Get
            Return currentStrokePoints
        End Get
    End Property
End Class

' EventHandler for the StrokeRendered event.
Public Delegate Sub StrokeRenderedEventHandler(ByVal sender As Object, ByVal e As StrokeRenderedEventArgs) 


' A StylusPlugin that restricts the input area
Class FilterPlugin
    Inherits StylusPlugIn

    Private collectedPoints As StylusPointCollection
    Private currentStylus As Integer = -1
    Public Event StrokeRendered As StrokeRenderedEventHandler


    Protected Overrides Sub OnStylusDown(ByVal rawStylusInput As RawStylusInput)

        ' Run the base class before we modify the data
        MyBase.OnStylusDown(rawStylusInput)

        If currentStylus = -1 Then

            Dim pointsFromEvent As StylusPointCollection = rawStylusInput.GetStylusPoints()

            ' Create an emtpy StylusPointCollection to contain the filtered
            ' points.
            collectedPoints = New StylusPointCollection(pointsFromEvent.Description)

            ' Restrict the stylus input and add the filtered 
            ' points to collectedPoints. 
            Dim points As StylusPointCollection = FilterPackets(pointsFromEvent)
            rawStylusInput.SetStylusPoints(points)
            collectedPoints.Add(points)

            currentStylus = rawStylusInput.StylusDeviceId

        End If

    End Sub


    Protected Overrides Sub OnStylusMove(ByVal rawStylusInput As RawStylusInput)

        ' Run the base class before we modify the data
        MyBase.OnStylusMove(rawStylusInput)

        If currentStylus = rawStylusInput.StylusDeviceId Then

            Dim pointsFromEvent As StylusPointCollection = rawStylusInput.GetStylusPoints()

            ' Restrict the stylus input and add the filtered 
            ' points to collectedPoints. 
            Dim points As StylusPointCollection = FilterPackets(pointsFromEvent)
            rawStylusInput.SetStylusPoints(points)
            collectedPoints.Add(points)

        End If

    End Sub

    Protected Overrides Sub OnStylusUp(ByVal rawStylusInput As RawStylusInput)

        ' Run the base class before we modify the data
        MyBase.OnStylusUp(rawStylusInput)

        If currentStylus = rawStylusInput.StylusDeviceId Then

            Dim pointsFromEvent As StylusPointCollection = rawStylusInput.GetStylusPoints()

            ' Restrict the stylus input and add the filtered 
            ' points to collectedPoints. 
            Dim points As StylusPointCollection = FilterPackets(pointsFromEvent)
            rawStylusInput.SetStylusPoints(points)
            collectedPoints.Add(points)

            RecordPoints(collectedPoints, "collectPoints - StylusUp")
            ' Subscribe to the OnStylusUpProcessed method.
            rawStylusInput.NotifyWhenProcessed(collectedPoints)

            currentStylus = -1

        End If

    End Sub


    Private Function FilterPackets(ByVal stylusPoints As StylusPointCollection) As StylusPointCollection

        ' 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

        ' Return the modified StylusPoints.
        Return stylusPoints

    End Function 'FilterPackets

    ' This is called on the application thread.
    Protected Overrides Sub OnStylusUpProcessed(ByVal callbackData As Object, _
                                                ByVal targetVerified As Boolean)

        ' Check that the element actually receive the OnStylusUp input.
        If targetVerified Then
            Dim strokePoints As StylusPointCollection

            strokePoints = CType(callbackData, StylusPointCollection)

            If strokePoints Is Nothing Then
                Return
            End If

            ' Raise the StrokeRendered event so the consumer of the plugin can
            ' add the filtered stroke to its StrokeCollection.
            RecordPoints(strokePoints, "onStylusUpProcessed")
            Dim e As New StrokeRenderedEventArgs(strokePoints)
            OnStrokeRendered(e)
        End If

    End Sub


    Protected Overridable Sub OnStrokeRendered(ByVal e As StrokeRenderedEventArgs)

        RaiseEvent StrokeRendered(Me, e)

    End Sub

    Public Sub RecordPoints(ByVal points As StylusPointCollection, ByVal name As String)

        System.Diagnostics.Debug.WriteLine(name)
        For Each point As StylusPoint In points
            System.Diagnostics.Debug.WriteLine("   x: " & point.X & " y: " & point.Y)
        Next
    End Sub
End Class

Commenti

StylusPlugIn consente di modificare StylusPoint gli oggetti in thread separati. I thread separati vengono usati in modo che l'input penna del tablet esegua ancora il rendering dell'input penna del tablet anche se l'applicazione esegue un'altra operazione.

Per intercettare i punti dello stilo dall'hardware, creare una classe che eredita dalla StylusPlugIn classe . La StylusPlugIn classe include i metodi seguenti che è possibile eseguire l'override per modificare StylusPoint gli oggetti in un thread nel pool di thread penna.

L'input della penna viene instradato a un StylusPlugIn elemento sul thread della penna. Poiché non è possibile eseguire hit test accurati sul thread della penna, alcuni elementi potrebbero occasionalmente ricevere l'input dello stilo destinato ad altri elementi. Se è necessario assicurarsi che l'input sia stato indirizzato correttamente prima di eseguire un'operazione, è necessario sottoscrivere ed eseguire l'operazione nel metodo OnStylusDownProcessed, OnStylusMoveProcessedo OnStylusUpProcessed. Questi metodi vengono richiamati dal thread dell'applicazione principale dopo l'esecuzione di hit testing accurati. Per iscriversi a questi metodi, chiamare il metodo NotifyWhenProcessed nel metodo che viene eseguito nel thread associato alla penna. Ad esempio, se si chiama NotifyWhenProcessed in OnStylusMove, si OnStylusMoveProcessed verifica .

Note

Se si usa un StylusPlugIn controllo all'interno di un controllo, è necessario testare il plug-in e controllare ampiamente per assicurarsi che non generino eccezioni impreviste.

Utilizzo del testo XAML

Questa classe non viene in genere usata in XAML.

Costruttori

Nome Descrizione
StylusPlugIn()

Inizializza una nuova istanza della classe StylusPlugIn.

Proprietà

Nome Descrizione
Element

Ottiene l'oggetto UIElement a cui è associato l'oggetto StylusPlugIn .

ElementBounds

Ottiene i limiti memorizzati nella cache dell'elemento.

Enabled

Ottiene o imposta un valore che indica se l'oggetto StylusPlugIn è attivo.

IsActiveForInput

Ottiene un valore che indica se l'oggetto è in grado di accettare l'input StylusPlugIn .

Metodi

Nome Descrizione
Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene il Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale del Objectcorrente.

(Ereditato da Object)
OnAdded()

Si verifica quando l'oggetto StylusPlugIn viene aggiunto a un elemento .

OnEnabledChanged()

Si verifica quando la Enabled proprietà viene modificata.

OnIsActiveForInputChanged()

Si verifica quando la IsActiveForInput proprietà viene modificata.

OnRemoved()

Si verifica quando l'oggetto StylusPlugIn viene rimosso da un elemento .

OnStylusDown(RawStylusInput)

Si verifica su un thread nel pool di thread penna quando la penna del tablet tocca il digitalizzatore.

OnStylusDownProcessed(Object, Boolean)

Si verifica nel thread dell'interfaccia utente dell'applicazione (interfaccia utente) quando la penna del tablet tocca il digitalizzatore.

OnStylusEnter(RawStylusInput, Boolean)

Si verifica su un thread penna quando il cursore entra nei limiti di un elemento.

OnStylusLeave(RawStylusInput, Boolean)

Si verifica su un thread penna quando il cursore lascia i limiti di un elemento.

OnStylusMove(RawStylusInput)

Si verifica su un filo della penna quando la penna del tablet si sposta sul digitalizzatore.

OnStylusMoveProcessed(Object, Boolean)

Si verifica sul thread dell'interfaccia utente dell'applicazione (interfaccia utente) quando la penna del tablet si sposta sul digitalizzatore.

OnStylusUp(RawStylusInput)

Si verifica su un thread penna quando l'utente solleva la penna del tablet dal digitalizzatore.

OnStylusUpProcessed(Object, Boolean)

Si verifica sul thread dell'interfaccia utente dell'applicazione (interfaccia utente) quando l'utente solleva la penna del tablet dal digitalizzatore.

ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Si applica a