共用方式為


StylusPlugIn 類別

定義

表示外掛程式,這個外掛程式可以加入至控制項的 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 方法。 例如,如果您在 中 OnStylusMove 呼叫 NotifyWhenProcessed ,就會 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)

當 Tablet 畫筆碰觸到數位板時,發生於畫筆執行緒集區中的執行緒上。

OnStylusDownProcessed(Object, Boolean)

當 Tablet 畫筆碰觸到數位板時,發生於應用程式 UI (使用者介面) 執行緒上。

OnStylusEnter(RawStylusInput, Boolean)

當手寫筆游標進入項目的範圍時,發生於畫筆執行緒上。

OnStylusLeave(RawStylusInput, Boolean)

當手寫筆游標離開項目的範圍時,發生於畫筆執行緒上。

OnStylusMove(RawStylusInput)

當 Tablet 畫筆在數位板上移動時,發生於畫筆執行緒上。

OnStylusMoveProcessed(Object, Boolean)

當 Tablet 畫筆在數位板上移動時,發生於應用程式 UI (使用者介面) 執行緒上。

OnStylusUp(RawStylusInput)

當使用者從數位板上拿起 Tablet 畫筆時,發生於畫筆執行緒上。

OnStylusUpProcessed(Object, Boolean)

當使用者從數位板上拿起 Tablet 畫筆時,發生於應用程式 UI (使用者介面) 執行緒上。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於