StylusPlugIn Classe
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Representa um plug-in que pode ser adicionado à propriedade de StylusPlugIns um controlo.
public ref class StylusPlugIn abstract
public abstract class StylusPlugIn
type StylusPlugIn = class
Public MustInherit Class StylusPlugIn
- Herança
-
StylusPlugIn
- Derivado
Exemplos
O exemplo seguinte cria um costume StylusPlugIn que limita a tinta a uma certa área no controlo.
// 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
Observações
Permite-te StylusPlugIn manipular StylusPoint objetos em threads separados. São usados threads separados para que a tinta continue a ser renderizada à medida que a caneta do tablet insere dados, mesmo que a aplicação esteja a fazer outra coisa.
Para intercetar pontos de stylus do hardware, crie uma classe que herde da StylusPlugIn classe. A StylusPlugIn classe tem os seguintes métodos que podes sobrepor para manipular StylusPoint objetos numa thread no pool de canetas thread.
A entrada da caneta é encaminhada para um elemento StylusPlugIn na linha da caneta. Como não é possível realizar testes de acerto precisos na linha de canetas, alguns elementos podem ocasionalmente receber entrada da stylus destinada a outros elementos. Se você precisar se certificar de que a entrada foi roteada corretamente antes de executar uma operação, inscreva-se e execute a operação no método OnStylusDownProcessed, OnStylusMoveProcessedou OnStylusUpProcessed. Estes métodos são invocados pelo tópico principal de aplicação após a realização de testes de acerto precisos. Para subscrever a esses métodos, invoque o método NotifyWhenProcessed no método que ocorre na sequência do pen. Por exemplo, se chamar NotifyWhenProcessed em OnStylusMove, ocorre.OnStylusMoveProcessed
Note
Se usares um StylusPlugIn controlo interior, deves testar o plug-in e o controlo extensivamente para garantir que não fazem exceções indesejadas.
Utilização de Texto XAML
Esta classe não é normalmente usada em XAML.
Construtores
| Name | Description |
|---|---|
| StylusPlugIn() |
Inicializa uma nova instância da StylusPlugIn classe. |
Propriedades
| Name | Description |
|---|---|
| Element |
Obtém o UIElement que StylusPlugIn está ligado. |
| ElementBounds |
Obtém os limites em cache do elemento. |
| Enabled |
Obtém ou define se o StylusPlugIn está ativo. |
| IsActiveForInput |
Obtém se é StylusPlugIn capaz de aceitar entrada. |
Métodos
| Name | Description |
|---|---|
| Equals(Object) |
Determina se o objeto especificado é igual ao objeto atual. (Herdado de Object) |
| GetHashCode() |
Serve como função de hash predefinida. (Herdado de Object) |
| GetType() |
Obtém o Type da instância atual. (Herdado de Object) |
| MemberwiseClone() |
Cria uma cópia superficial do atual Object. (Herdado de Object) |
| OnAdded() |
Ocorre quando o StylusPlugIn é adicionado a um elemento. |
| OnEnabledChanged() |
Ocorre quando a Enabled propriedade muda. |
| OnIsActiveForInputChanged() |
Ocorre quando a IsActiveForInput propriedade muda. |
| OnRemoved() |
Ocorre quando o StylusPlugIn é removido de um elemento. |
| OnStylusDown(RawStylusInput) |
Ocorre numa thread no pool de threads da caneta quando a caneta do tablet toca no digitalizador. |
| OnStylusDownProcessed(Object, Boolean) |
Ocorre na thread da interface da aplicação quando a caneta do tablet toca no digitalizador. |
| OnStylusEnter(RawStylusInput, Boolean) |
Ocorre numa linha de caneta quando o cursor entra nos limites de um elemento. |
| OnStylusLeave(RawStylusInput, Boolean) |
Ocorre numa linha de caneta quando o cursor sai dos limites de um elemento. |
| OnStylusMove(RawStylusInput) |
Ocorre numa linha de caneta quando a caneta do tablet se move no digitalizador. |
| OnStylusMoveProcessed(Object, Boolean) |
Ocorre na thread da interface da aplicação quando a caneta do tablet se move no digitalizador. |
| OnStylusUp(RawStylusInput) |
Ocorre numa linha de caneta quando o utilizador levanta a caneta do tablet do digitalizador. |
| OnStylusUpProcessed(Object, Boolean) |
Ocorre na thread da interface da aplicação quando o utilizador levanta a caneta do tablet do digitalizador. |
| ToString() |
Devolve uma cadeia que representa o objeto atual. (Herdado de Object) |