StylusPlugIn 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示可添加到控件的 StylusPlugIns 属性中的插件。
public ref class StylusPlugIn abstract
public abstract class StylusPlugIn
type StylusPlugIn = class
Public MustInherit Class StylusPlugIn
- 继承
-
StylusPlugIn
- 派生
示例
以下示例创建一个自定义,该自定义 StylusPlugIn 将墨迹限制为控件上的特定区域。
// 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
注解
这样 StylusPlugIn ,就可以在单独的线程上操作 StylusPoint 对象。 使用单独的线程,以便即使应用程序执行其他操作,墨迹仍呈现为平板电脑笔输入数据。
若要截获硬件中的触笔点,请创建继承自该类的 StylusPlugIn 类。 该 StylusPlugIn 类具有以下方法,你可以替代这些方法以操作 StylusPoint 笔线程池中的线程上的对象。
笔的输入将路由到笔线程上的元素 StylusPlugIn 。 由于无法对笔线程执行准确的命中测试,因此某些元素偶尔可能会接收用于其他元素的触笔输入。 如果需要确保在执行操作之前正确路由输入,请在或OnStylusMoveProcessedOnStylusUpProcessed方法中OnStylusDownProcessed订阅和执行操作。 执行准确的命中测试后,主应用程序线程会调用这些方法。 若要订阅这些方法,请调用 NotifyWhenProcessed 在笔线程上发生的方法中的方法。 例如,如果调用 NotifyWhenProcessed , OnStylusMove则 OnStylusMoveProcessed 会发生。
备注
如果使用 StylusPlugIn 控件内部,则应广泛测试插件和控制,以确保它们不会引发任何意外异常。
XAML 文本用法
此类通常不用于 XAML。
构造函数
StylusPlugIn() |
初始化 StylusPlugIn 类的新实例。 |
属性
Element |
获取向其附加 UIElement 的 StylusPlugIn。 |
ElementBounds |
获取元素的缓存边界。 |
Enabled |
获取或设置 StylusPlugIn 是否处于活动状态。 |
IsActiveForInput |
获取 StylusPlugIn 是否可以接受输入。 |
方法
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
OnAdded() |
在将 StylusPlugIn 添加到元素中时发生。 |
OnEnabledChanged() |
当 Enabled 属性更改时发生。 |
OnIsActiveForInputChanged() |
当 IsActiveForInput 属性更改时发生。 |
OnRemoved() |
在从元素中移除 StylusPlugIn 时发生。 |
OnStylusDown(RawStylusInput) |
当触笔接触数字化仪时,在笔线程池中的线程上发生。 |
OnStylusDownProcessed(Object, Boolean) |
当触笔接触数字化仪时,在应用程序 UI(用户界面)线程上发生。 |
OnStylusEnter(RawStylusInput, Boolean) |
当光标进入元素的边界时在笔线程上发生。 |
OnStylusLeave(RawStylusInput, Boolean) |
当光标离开元素的边界时在笔线程上发生。 |
OnStylusMove(RawStylusInput) |
当触笔移到数字化仪上时,在笔线程上发生。 |
OnStylusMoveProcessed(Object, Boolean) |
当触笔移到数字化仪上时,在应用程序 UI(用户界面)线程上发生。 |
OnStylusUp(RawStylusInput) |
当用户将触笔从数字化仪上提起时,在笔线程上发生。 |
OnStylusUpProcessed(Object, Boolean) |
当用户将触笔从数字化仪上提起时,在应用程序 UI(用户界面)线程上发生。 |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |