Gewusst wie: Auswählen von Freihandeingaben aus einem benutzerdefinierten Steuerelement

Durch Hinzufügen eines IncrementalLassoHitTester zu einem benutzerdefinierten Steuerelement können Sie das Steuerelement aktivieren, damit ein Benutzer „Freihandeingabe mit einem Lassotool“ auswählen kann, ähnlich wie die InkCanvas „Freihandeingabe mit einem Lasso“ auswählt.

In diesem Beispiel wird davon ausgegangen, dass Sie mit dem Erstellen eines freihandfähigen benutzerdefinierten Steuerelements vertraut sind. Informationen zum Erstellen eines benutzerdefinierten Steuerelements, das Freihandeingaben akzeptiert, finden Sie unter Erstellen eines Freihandeingabe-Steuerelements.

Beispiel

Wenn der Benutzer ein Lasso zeichnet, sagt der IncrementalLassoHitTester vorher, welche Striche sich innerhalb der Lassopfadgrenzen befinden werden, nachdem der Benutzer das Lasso geschlossen hat. Striche, die sich innerhalb der Grenzen des Lassopfads befinden, können als ausgewählt betrachtet werden. Die Auswahl von Strichen kann auch aufgehoben werden. Wenn der Benutzer beispielsweise die Richtung beim Zeichnen des Lassos umkehrt, wird der IncrementalLassoHitTester die Auswahl einiger Striche möglicherweise aufheben.

Der IncrementalLassoHitTester löst das SelectionChanged-Ereignis aus, mit dem das benutzerdefinierte Steuerelement reagieren kann, während der Benutzer das Lasso zeichnet. Sie können z. B. die Darstellung von Strichen ändern, wenn der Benutzer sie auswählt oder die Auswahl aufhebt.

Verwalten des Freihandmodus

Es ist hilfreich für den Benutzer, wenn das Lasso anders als die Freihandeingabe auf dem Steuerelement angezeigt wird. Um dies zu erreichen, muss das benutzerdefinierte Steuerelement nachverfolgen, ob der Benutzer mit Freihandeingabe schreibt oder sie auswählt. Die einfachste Möglichkeit besteht darin, eine Aufzählung mit zwei Werten zu deklarieren: eine, um anzugeben, dass der Benutzer mit Freihandeingabe schreibt, und eine, um anzugeben, dass der Benutzer Freihandeingaben auswählt.

// Enum that keeps track of whether StrokeCollectionDemo is in ink mode
// or select mode.
public enum InkMode
{
    Ink, Select
}
' Enum that keeps track of whether StrokeCollectionDemo is in ink mode 
' or select mode.
Public Enum InkMode
    Ink
    [Select]
End Enum 'InkMode

Fügen Sie als Nächstes zwei DrawingAttributes zur Klasse hinzu: eine, die verwendet werden soll, wenn der Benutzer mit Freihandeingabe schreibt, eine, die verwendet werden soll, wenn der Benutzer Freihandeingabe auswählt. Initialisieren Sie im Konstruktor die DrawingAttributes und fügen Sie beide AttributeChanged-Ereignisse an denselben Ereignishandler an. Legen Sie dann die DrawingAttributes-Eigenschaft des DynamicRenderer für die Freihandeingabe-DrawingAttributesfest.

DrawingAttributes inkDA;
DrawingAttributes selectDA;
Private inkDA As DrawingAttributes
Private selectDA As DrawingAttributes
// In the constructor.
// Selection drawing attributes use dark gray ink.
selectDA = new DrawingAttributes();
selectDA.Color = Colors.DarkGray;

// ink drawing attributes use default attributes
inkDA = new DrawingAttributes();
inkDA.Width = 5;
inkDA.Height = 5;

inkDA.AttributeChanged += new PropertyDataChangedEventHandler(DrawingAttributesChanged);
selectDA.AttributeChanged += new PropertyDataChangedEventHandler(DrawingAttributesChanged);
' In the constructor.
' Selection drawing attributes use dark gray ink.
selectDA = New DrawingAttributes()
selectDA.Color = Colors.DarkGray

' ink drawing attributes use default attributes
inkDA = New DrawingAttributes()
inkDA.Width = 5
inkDA.Height = 5

AddHandler inkDA.AttributeChanged, _
           AddressOf DrawingAttributesChanged

AddHandler selectDA.AttributeChanged, _
           AddressOf DrawingAttributesChanged

Fügen Sie eine Eigenschaft hinzu, die den Auswahlmodus verfügbar macht. Wenn der Benutzer den Auswahlmodus ändert, legen Sie die DrawingAttributes-Eigenschaft des DynamicRenderer für das entsprechende DrawingAttributes-Objekt fest, und fügen Sie die RootVisual-Eigenschaft dann erneut an den InkPresenter an.

// Property to indicate whether the user is inputting or
// selecting ink.
public InkMode Mode
{
    get
    {
        return mode;
    }

    set
    {
        mode = value;

        // Set the DrawingAttributes of the DynamicRenderer
        if (mode == InkMode.Ink)
        {
            renderer.DrawingAttributes = inkDA;
        }
        else
        {
            renderer.DrawingAttributes = selectDA;
        }

        // Reattach the visual of the DynamicRenderer to the InkPresenter.
        presenter.DetachVisuals(renderer.RootVisual);
        presenter.AttachVisuals(renderer.RootVisual, renderer.DrawingAttributes);
    }
}
' Property to indicate whether the user is inputting or
' selecting ink.  
Public Property Mode() As InkMode
    Get
        Return Mode
    End Get

    Set(ByVal value As InkMode)
        modeState = value

        ' Set the DrawingAttributes of the DynamicRenderer
        If modeState = InkMode.Ink Then
            renderer.DrawingAttributes = inkDA
        Else
            renderer.DrawingAttributes = selectDA
        End If

        ' Reattach the visual of the DynamicRenderer to the InkPresenter.
        presenter.DetachVisuals(renderer.RootVisual)
        presenter.AttachVisuals(renderer.RootVisual, renderer.DrawingAttributes)
    End Set
End Property

Machen Sie die DrawingAttributes als Eigenschaften verfügbar, damit Anwendungen das Aussehen der Freihandstriche und ausgewählten Striche bestimmen können.

// Property to allow the user to change the pen's DrawingAttributes.
public DrawingAttributes InkDrawingAttributes
{
    get
    {
        return inkDA;
    }
}

// Property to allow the user to change the Selector'newStroke DrawingAttributes.
public DrawingAttributes SelectDrawingAttributes
{
    get
    {
        return selectDA;
    }
}
' Property to allow the user to change the pen's DrawingAttributes.
Public ReadOnly Property InkDrawingAttributes() As DrawingAttributes
    Get
        Return inkDA
    End Get
End Property

' Property to allow the user to change the Selector'newStroke DrawingAttributes.
Public ReadOnly Property SelectDrawingAttributes() As DrawingAttributes
    Get
        Return selectDA
    End Get
End Property

Wenn eine Eigenschaft eines DrawingAttributes-Objekts geändert wird, muss das RootVisual erneut an den InkPresenter angefügt werden. Fügen Sie im Ereignishandler für das AttributeChanged-Ereignis das RootVisual erneut dem InkPresenter an.

void DrawingAttributesChanged(object sender, PropertyDataChangedEventArgs e)
{
    // Reattach the visual of the DynamicRenderer to the InkPresenter
    // whenever the DrawingAttributes change.
    presenter.DetachVisuals(renderer.RootVisual);
    presenter.AttachVisuals(renderer.RootVisual, renderer.DrawingAttributes);
}
Private Sub DrawingAttributesChanged(ByVal sender As Object, _
                             ByVal e As PropertyDataChangedEventArgs)

    ' Reattach the visual of the DynamicRenderer to the InkPresenter 
    ' whenever the DrawingAttributes change.
    presenter.DetachVisuals(renderer.RootVisual)
    presenter.AttachVisuals(renderer.RootVisual, _
                            renderer.DrawingAttributes)

End Sub

Verwenden des InkrementalLassoHitTester

Erstellen und initialisieren Sie eine StrokeCollection, die die ausgewählten Striche enthält.

// StylusPointCollection that collects the stylus points from the stylus events.
StylusPointCollection stylusPoints;
' StylusPointCollection that collects the stylus points from the stylus events.
Private stylusPoints As StylusPointCollection

Wenn der Benutzer mit dem Zeichnen eines Strichs beginnt, deaktivieren Sie die Freihandeingabe oder das Lasso und heben Sie die Auswahl aller ausgewählter Striche auf. Wenn der Benutzer dann ein Lasso zeichnet, erstellen Sie einen IncrementalLassoHitTester, indem Sie GetIncrementalLassoHitTester aufrufen, abonnieren Sie das SelectionChanged-Ereignis, und rufen Sie AddPoints auf. Dieser Code kann eine separate Methode sein und von den Methoden OnStylusDown und OnMouseDown aufgerufen werden.

private void InitializeHitTester(StylusPointCollection collectedPoints)
{
    // Deselect any selected strokes.
    foreach (Stroke selectedStroke in selectedStrokes)
    {
        selectedStroke.DrawingAttributes.Color = inkDA.Color;
    }
    selectedStrokes.Clear();

    if (mode == InkMode.Select)
    {
        // Remove the previously drawn lasso, if it exists.
        if (lassoPath != null)
        {
            presenter.Strokes.Remove(lassoPath);
            lassoPath = null;
        }

        selectionTester =
            presenter.Strokes.GetIncrementalLassoHitTester(80);
        selectionTester.SelectionChanged +=
            new LassoSelectionChangedEventHandler(selectionTester_SelectionChanged);
        selectionTester.AddPoints(collectedPoints);
    }
}
Private Sub InitializeHitTester(ByVal collectedPoints As StylusPointCollection)

    ' Deselect any selected strokes.
    Dim selectedStroke As Stroke
    For Each selectedStroke In selectedStrokes
        selectedStroke.DrawingAttributes.Color = inkDA.Color
    Next selectedStroke
    selectedStrokes.Clear()

    If modeState = InkMode.Select Then
        ' Remove the previously drawn lasso, if it exists.
        If Not (lassoPath Is Nothing) Then
            presenter.Strokes.Remove(lassoPath)
            lassoPath = Nothing
        End If

        selectionTester = presenter.Strokes.GetIncrementalLassoHitTester(80)
        AddHandler selectionTester.SelectionChanged, AddressOf selectionTester_SelectionChanged
        selectionTester.AddPoints(collectedPoints)
    End If

End Sub

Fügen Sie die Eingabestiftpunkte zum IncrementalLassoHitTester hinzu, während der Benutzer das Lasso zeichnet. Rufen Sie die folgende Methode aus den Methoden OnStylusMove, OnStylusUp, OnMouseMoveund OnMouseLeftButtonUp auf.

private void AddPointsToHitTester(StylusPointCollection collectedPoints)
{

    if (mode == InkMode.Select &&
        selectionTester != null &&
        selectionTester.IsValid)
    {
        // When the control is selecting strokes, add the
        // stylus packetList to selectionTester.
        selectionTester.AddPoints(collectedPoints);
    }
}
Private Sub AddPointsToHitTester(ByVal collectedPoints As StylusPointCollection)

    If modeState = InkMode.Select AndAlso _
       Not selectionTester Is Nothing AndAlso _
       selectionTester.IsValid Then

        ' When the control is selecting strokes, add the
        ' stylus packetList to selectionTester.
        selectionTester.AddPoints(collectedPoints)
    End If

End Sub

Bearbeiten Sie das IncrementalLassoHitTester.SelectionChanged-Ereignis, sodass es reagiert, wenn der Benutzer Striche auswählt oder ihre Auswahl aufhebt. Die LassoSelectionChangedEventArgs-Klasse verfügt über die Eigenschaften SelectedStrokes und DeselectedStrokes, die die Striche abrufen, die ausgewählt wurden oder deren Auswahl aufgehoben wurde.

void selectionTester_SelectionChanged(object sender,
    LassoSelectionChangedEventArgs args)
{
    // Change the color of all selected strokes to red.
    foreach (Stroke selectedStroke in args.SelectedStrokes)
    {
        selectedStroke.DrawingAttributes.Color = Colors.Red;
        selectedStrokes.Add(selectedStroke);
    }

    // Change the color of all unselected strokes to
    // their original color.
    foreach (Stroke unselectedStroke in args.DeselectedStrokes)
    {
        unselectedStroke.DrawingAttributes.Color = inkDA.Color;
        selectedStrokes.Remove(unselectedStroke);
    }
}
Private Sub selectionTester_SelectionChanged(ByVal sender As Object, _
                                     ByVal args As LassoSelectionChangedEventArgs)

    ' Change the color of all selected strokes to red.
    Dim selectedStroke As Stroke
    For Each selectedStroke In args.SelectedStrokes
        selectedStroke.DrawingAttributes.Color = Colors.Red
        selectedStrokes.Add(selectedStroke)
    Next selectedStroke

    ' Change the color of all unselected strokes to 
    ' their original color.
    Dim unselectedStroke As Stroke
    For Each unselectedStroke In args.DeselectedStrokes
        unselectedStroke.DrawingAttributes.Color = inkDA.Color
        selectedStrokes.Remove(unselectedStroke)
    Next unselectedStroke

End Sub

Wenn der Benutzer das Lasso gezeichnet hat, kündigen Sie das Abonnement des SelectionChanged-Ereignisses und rufen Sie EndHitTesting auf.

if (mode == InkMode.Select && lassoPath == null)
{
    // Add the lasso to the InkPresenter and add the packetList
    // to selectionTester.
    lassoPath = newStroke;
    lassoPath.DrawingAttributes = selectDA.Clone();
    presenter.Strokes.Add(lassoPath);
    selectionTester.SelectionChanged -= new LassoSelectionChangedEventHandler
                    (selectionTester_SelectionChanged);
    selectionTester.EndHitTesting();
}
If modeState = InkMode.Select AndAlso lassoPath Is Nothing Then
    ' Add the lasso to the InkPresenter and add the packetList
    ' to selectionTester.
    lassoPath = newStroke
    lassoPath.DrawingAttributes = selectDA.Clone()
    presenter.Strokes.Add(lassoPath)
    RemoveHandler selectionTester.SelectionChanged, _
                  AddressOf selectionTester_SelectionChanged
    selectionTester.EndHitTesting()
End If

Zusammenfassung:

Das folgende Beispiel ist ein benutzerdefiniertes Steuerelement, mit dem ein Benutzer Freihandeingaben mit einem Lasso auswählen kann.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Input;
using System.Windows.Input.StylusPlugIns;
using System.Windows.Ink;

// Enum that keeps track of whether StrokeCollectionDemo is in ink mode
// or select mode.
public enum InkMode
{
    Ink, Select
}

// This control allows the user to input and select ink.  When the
// user selects ink, the lasso remains visible until they erase, or clip
// the selected strokes, or clear the selection.  When the control is
// in selection mode, strokes that are selected turn red.
public class InkSelector : Label
{
    InkMode mode;

    DrawingAttributes inkDA;
    DrawingAttributes selectDA;

    InkPresenter presenter;
    IncrementalLassoHitTester selectionTester;
    StrokeCollection selectedStrokes = new StrokeCollection();

    // StylusPointCollection that collects the stylus points from the stylus events.
    StylusPointCollection stylusPoints;
    // Stroke that represents the lasso.
    Stroke lassoPath;

    DynamicRenderer renderer;

    public InkSelector()
    {
        mode = InkMode.Ink;

        // Use an InkPresenter to display the strokes on the custom control.
        presenter = new InkPresenter();
        this.Content = presenter;

        // In the constructor.
        // Selection drawing attributes use dark gray ink.
        selectDA = new DrawingAttributes();
        selectDA.Color = Colors.DarkGray;

        // ink drawing attributes use default attributes
        inkDA = new DrawingAttributes();
        inkDA.Width = 5;
        inkDA.Height = 5;

        inkDA.AttributeChanged += new PropertyDataChangedEventHandler(DrawingAttributesChanged);
        selectDA.AttributeChanged += new PropertyDataChangedEventHandler(DrawingAttributesChanged);

        // Add a DynmaicRenderer to the control so ink appears
        // to "flow" from the tablet pen.
        renderer = new DynamicRenderer();
        renderer.DrawingAttributes = inkDA;
        this.StylusPlugIns.Add(renderer);
        presenter.AttachVisuals(renderer.RootVisual,
            renderer.DrawingAttributes);
    }

    static InkSelector()
    {
        // Allow ink to be drawn only within the bounds of the control.
        Type owner = typeof(InkSelector);
        ClipToBoundsProperty.OverrideMetadata(owner,
            new FrameworkPropertyMetadata(true));
    }

    // Prepare to collect stylus packets. If Mode is set to Select,
    // get the IncrementalHitTester from the InkPresenter'newStroke
    // StrokeCollection and subscribe to its StrokeHitChanged event.
    protected override void OnStylusDown(StylusDownEventArgs e)
    {
        base.OnStylusDown(e);

        Stylus.Capture(this);

        // Create a new StylusPointCollection using the StylusPointDescription
        // from the stylus points in the StylusDownEventArgs.
        stylusPoints = new StylusPointCollection();
        StylusPointCollection eventPoints = e.GetStylusPoints(this, stylusPoints.Description);

        stylusPoints.Add(eventPoints);

        InitializeHitTester(eventPoints);
    }

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonDown(e);

        Mouse.Capture(this);

        if (e.StylusDevice != null)
        {
            return;
        }

        Point pt = e.GetPosition(this);

        StylusPointCollection collectedPoints = new StylusPointCollection(new Point[] { pt });

        stylusPoints = new StylusPointCollection();

        stylusPoints.Add(collectedPoints);

        InitializeHitTester(collectedPoints);
    }
    private void InitializeHitTester(StylusPointCollection collectedPoints)
    {
        // Deselect any selected strokes.
        foreach (Stroke selectedStroke in selectedStrokes)
        {
            selectedStroke.DrawingAttributes.Color = inkDA.Color;
        }
        selectedStrokes.Clear();

        if (mode == InkMode.Select)
        {
            // Remove the previously drawn lasso, if it exists.
            if (lassoPath != null)
            {
                presenter.Strokes.Remove(lassoPath);
                lassoPath = null;
            }

            selectionTester =
                presenter.Strokes.GetIncrementalLassoHitTester(80);
            selectionTester.SelectionChanged +=
                new LassoSelectionChangedEventHandler(selectionTester_SelectionChanged);
            selectionTester.AddPoints(collectedPoints);
        }
    }

    // Collect the stylus packets as the stylus moves.
    protected override void OnStylusMove(StylusEventArgs e)
    {
        if (stylusPoints == null)
        {
            return;
        }

        StylusPointCollection collectedPoints = e.GetStylusPoints(this, stylusPoints.Description);
        stylusPoints.Add(collectedPoints);
        AddPointsToHitTester(collectedPoints);
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {

        base.OnMouseMove(e);

        if (e.StylusDevice != null)
        {
            return;
        }

        if (e.LeftButton == MouseButtonState.Released)
        {
            return;
        }

        stylusPoints ??= new StylusPointCollection();

        Point pt = e.GetPosition(this);

        StylusPointCollection collectedPoints = new StylusPointCollection(new Point[] { pt });

        stylusPoints.Add(collectedPoints);

        AddPointsToHitTester(collectedPoints);
    }

    private void AddPointsToHitTester(StylusPointCollection collectedPoints)
    {

        if (mode == InkMode.Select &&
            selectionTester != null &&
            selectionTester.IsValid)
        {
            // When the control is selecting strokes, add the
            // stylus packetList to selectionTester.
            selectionTester.AddPoints(collectedPoints);
        }
    }

    // When the user lifts the stylus, create a Stroke from the
    // collected stylus points and add it to the InkPresenter.
    // When the control is selecting strokes, add the
    // point data to the IncrementalHitTester.
    protected override void OnStylusUp(StylusEventArgs e)
    {
        stylusPoints ??= new StylusPointCollection();
        StylusPointCollection collectedPoints =
            e.GetStylusPoints(this, stylusPoints.Description);

        stylusPoints.Add(collectedPoints);
        AddPointsToHitTester(collectedPoints);
        AddStrokeToPresenter();
        stylusPoints = null;

        Stylus.Capture(null);
    }

    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
    {

        base.OnMouseLeftButtonUp(e);

        if (e.StylusDevice != null) return;

        if (stylusPoints == null) stylusPoints = new StylusPointCollection();

        Point pt = e.GetPosition(this);

        StylusPointCollection collectedPoints = new StylusPointCollection(new Point[] { pt });

        stylusPoints.Add(collectedPoints);
        AddPointsToHitTester(collectedPoints);
        AddStrokeToPresenter();

        stylusPoints = null;

        Mouse.Capture(null);
    }

    private void AddStrokeToPresenter()
    {
        Stroke newStroke = new Stroke(stylusPoints);

        if (mode == InkMode.Ink)
        {
            // Add the stroke to the InkPresenter.
            newStroke.DrawingAttributes = inkDA.Clone();
            presenter.Strokes.Add(newStroke);
        }

        if (mode == InkMode.Select && lassoPath == null)
        {
            // Add the lasso to the InkPresenter and add the packetList
            // to selectionTester.
            lassoPath = newStroke;
            lassoPath.DrawingAttributes = selectDA.Clone();
            presenter.Strokes.Add(lassoPath);
            selectionTester.SelectionChanged -= new LassoSelectionChangedEventHandler
                            (selectionTester_SelectionChanged);
            selectionTester.EndHitTesting();
        }
    }

    void selectionTester_SelectionChanged(object sender,
        LassoSelectionChangedEventArgs args)
    {
        // Change the color of all selected strokes to red.
        foreach (Stroke selectedStroke in args.SelectedStrokes)
        {
            selectedStroke.DrawingAttributes.Color = Colors.Red;
            selectedStrokes.Add(selectedStroke);
        }

        // Change the color of all unselected strokes to
        // their original color.
        foreach (Stroke unselectedStroke in args.DeselectedStrokes)
        {
            unselectedStroke.DrawingAttributes.Color = inkDA.Color;
            selectedStrokes.Remove(unselectedStroke);
        }
    }

    // Property to indicate whether the user is inputting or
    // selecting ink.
    public InkMode Mode
    {
        get
        {
            return mode;
        }

        set
        {
            mode = value;

            // Set the DrawingAttributes of the DynamicRenderer
            if (mode == InkMode.Ink)
            {
                renderer.DrawingAttributes = inkDA;
            }
            else
            {
                renderer.DrawingAttributes = selectDA;
            }

            // Reattach the visual of the DynamicRenderer to the InkPresenter.
            presenter.DetachVisuals(renderer.RootVisual);
            presenter.AttachVisuals(renderer.RootVisual, renderer.DrawingAttributes);
        }
    }
    void DrawingAttributesChanged(object sender, PropertyDataChangedEventArgs e)
    {
        // Reattach the visual of the DynamicRenderer to the InkPresenter
        // whenever the DrawingAttributes change.
        presenter.DetachVisuals(renderer.RootVisual);
        presenter.AttachVisuals(renderer.RootVisual, renderer.DrawingAttributes);
    }

    // Property to allow the user to change the pen's DrawingAttributes.
    public DrawingAttributes InkDrawingAttributes
    {
        get
        {
            return inkDA;
        }
    }

    // Property to allow the user to change the Selector'newStroke DrawingAttributes.
    public DrawingAttributes SelectDrawingAttributes
    {
        get
        {
            return selectDA;
        }
    }
}
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Input
Imports System.Windows.Input.StylusPlugIns
Imports System.Windows.Ink


' Enum that keeps track of whether StrokeCollectionDemo is in ink mode 
' or select mode.
Public Enum InkMode
    Ink
    [Select]
End Enum 'InkMode

' This control allows the user to input and select ink.  When the
' user selects ink, the lasso remains visible until they erase, or clip
' the selected strokes, or clear the selection.  When the control is
' in selection mode, strokes that are selected turn red.
Public Class InkSelector
    Inherits Label

    Private modeState As InkMode

    Private inkDA As DrawingAttributes
    Private selectDA As DrawingAttributes

    Private presenter As InkPresenter
    Private selectionTester As IncrementalLassoHitTester
    Private selectedStrokes As New StrokeCollection()

    ' StylusPointCollection that collects the stylus points from the stylus events.
    Private stylusPoints As StylusPointCollection

    ' Stroke that represents the lasso.
    Private lassoPath As Stroke

    Private renderer As DynamicRenderer


    Public Sub New()
        modeState = InkMode.Ink

        ' Use an InkPresenter to display the strokes on the custom control.
        presenter = New InkPresenter()
        Me.Content = presenter

        ' In the constructor.
        ' Selection drawing attributes use dark gray ink.
        selectDA = New DrawingAttributes()
        selectDA.Color = Colors.DarkGray

        ' ink drawing attributes use default attributes
        inkDA = New DrawingAttributes()
        inkDA.Width = 5
        inkDA.Height = 5

        AddHandler inkDA.AttributeChanged, _
                   AddressOf DrawingAttributesChanged

        AddHandler selectDA.AttributeChanged, _
                   AddressOf DrawingAttributesChanged

        ' Add a DynmaicRenderer to the control so ink appears
        ' to "flow" from the tablet pen.
        renderer = New DynamicRenderer()
        renderer.DrawingAttributes = inkDA
        Me.StylusPlugIns.Add(renderer)
        presenter.AttachVisuals(renderer.RootVisual, _
            renderer.DrawingAttributes)

    End Sub


    Shared Sub New()
        ' Allow ink to be drawn only within the bounds of the control.
        Dim owner As Type = GetType(InkSelector)
        ClipToBoundsProperty.OverrideMetadata(owner, _
            New FrameworkPropertyMetadata(True))

    End Sub

    ' Prepare to collect stylus packets. If Mode is set to Select,  
    ' get the IncrementalHitTester from the InkPresenter'newStroke 
    ' StrokeCollection and subscribe to its StrokeHitChanged event.
    Protected Overrides Sub OnStylusDown(ByVal e As StylusDownEventArgs)
        MyBase.OnStylusDown(e)

        Stylus.Capture(Me)

        ' Create a new StylusPointCollection using the StylusPointDescription
        ' from the stylus points in the StylusDownEventArgs.
        stylusPoints = New StylusPointCollection()
        Dim eventPoints As StylusPointCollection = e.GetStylusPoints(Me, stylusPoints.Description)

        stylusPoints.Add(eventPoints)

        InitializeHitTester(eventPoints)

    End Sub


    Protected Overrides Sub OnMouseLeftButtonDown(ByVal e As MouseButtonEventArgs)
        MyBase.OnMouseLeftButtonDown(e)

        Mouse.Capture(Me)

        If Not (e.StylusDevice Is Nothing) Then
            Return
        End If

        Dim pt As Point = e.GetPosition(Me)

        Dim collectedPoints As New StylusPointCollection(New Point() {pt})

        stylusPoints = New StylusPointCollection()

        stylusPoints.Add(collectedPoints)

        InitializeHitTester(collectedPoints)

    End Sub


    Private Sub InitializeHitTester(ByVal collectedPoints As StylusPointCollection)

        ' Deselect any selected strokes.
        Dim selectedStroke As Stroke
        For Each selectedStroke In selectedStrokes
            selectedStroke.DrawingAttributes.Color = inkDA.Color
        Next selectedStroke
        selectedStrokes.Clear()

        If modeState = InkMode.Select Then
            ' Remove the previously drawn lasso, if it exists.
            If Not (lassoPath Is Nothing) Then
                presenter.Strokes.Remove(lassoPath)
                lassoPath = Nothing
            End If

            selectionTester = presenter.Strokes.GetIncrementalLassoHitTester(80)
            AddHandler selectionTester.SelectionChanged, AddressOf selectionTester_SelectionChanged
            selectionTester.AddPoints(collectedPoints)
        End If

    End Sub

    ' Collect the stylus packets as the stylus moves.
    Protected Overrides Sub OnStylusMove(ByVal e As StylusEventArgs)

        If stylusPoints Is Nothing Then
            Return
        End If

        Dim collectedPoints As StylusPointCollection = e.GetStylusPoints(Me, stylusPoints.Description)
        stylusPoints.Add(collectedPoints)
        AddPointsToHitTester(collectedPoints)

    End Sub


    Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)

        MyBase.OnMouseMove(e)

        If Not (e.StylusDevice Is Nothing) Then
            Return
        End If

        If e.LeftButton = MouseButtonState.Released Then
            Return
        End If

        If stylusPoints Is Nothing Then
            stylusPoints = New StylusPointCollection()
        End If

        Dim pt As Point = e.GetPosition(Me)

        Dim collectedPoints As New StylusPointCollection(New Point() {pt})

        stylusPoints.Add(collectedPoints)

        AddPointsToHitTester(collectedPoints)

    End Sub


    Private Sub AddPointsToHitTester(ByVal collectedPoints As StylusPointCollection)

        If modeState = InkMode.Select AndAlso _
           Not selectionTester Is Nothing AndAlso _
           selectionTester.IsValid Then

            ' When the control is selecting strokes, add the
            ' stylus packetList to selectionTester.
            selectionTester.AddPoints(collectedPoints)
        End If

    End Sub

    ' When the user lifts the stylus, create a Stroke from the
    ' collected stylus points and add it to the InkPresenter.
    ' When the control is selecting strokes, add the
    ' point data to the IncrementalHitTester.
    Protected Overrides Sub OnStylusUp(ByVal e As StylusEventArgs)

        If stylusPoints Is Nothing Then
            stylusPoints = New StylusPointCollection()
        End If

        Dim collectedPoints As StylusPointCollection = _
            e.GetStylusPoints(Me, stylusPoints.Description)

        stylusPoints.Add(collectedPoints)
        AddPointsToHitTester(collectedPoints)
        AddStrokeToPresenter()

        stylusPoints = Nothing

        Stylus.Capture(Nothing)

    End Sub


    Protected Overrides Sub OnMouseLeftButtonUp(ByVal e As MouseButtonEventArgs)

        MyBase.OnMouseLeftButtonUp(e)

        If Not (e.StylusDevice Is Nothing) Then
            Return
        End If
        If stylusPoints Is Nothing Then
            stylusPoints = New StylusPointCollection()
        End If
        Dim pt As Point = e.GetPosition(Me)

        Dim collectedPoints As New StylusPointCollection(New Point() {pt})

        stylusPoints.Add(collectedPoints)
        AddPointsToHitTester(collectedPoints)
        AddStrokeToPresenter()

        stylusPoints = Nothing

        Mouse.Capture(Nothing)

    End Sub


    Private Sub AddStrokeToPresenter()
        Dim newStroke As New Stroke(stylusPoints)

        If modeState = InkMode.Ink Then
            ' Add the stroke to the InkPresenter.
            newStroke.DrawingAttributes = inkDA.Clone()
            presenter.Strokes.Add(newStroke)
        End If

        If modeState = InkMode.Select AndAlso lassoPath Is Nothing Then
            ' Add the lasso to the InkPresenter and add the packetList
            ' to selectionTester.
            lassoPath = newStroke
            lassoPath.DrawingAttributes = selectDA.Clone()
            presenter.Strokes.Add(lassoPath)
            RemoveHandler selectionTester.SelectionChanged, _
                          AddressOf selectionTester_SelectionChanged
            selectionTester.EndHitTesting()
        End If

    End Sub

    Private Sub selectionTester_SelectionChanged(ByVal sender As Object, _
                                         ByVal args As LassoSelectionChangedEventArgs)

        ' Change the color of all selected strokes to red.
        Dim selectedStroke As Stroke
        For Each selectedStroke In args.SelectedStrokes
            selectedStroke.DrawingAttributes.Color = Colors.Red
            selectedStrokes.Add(selectedStroke)
        Next selectedStroke

        ' Change the color of all unselected strokes to 
        ' their original color.
        Dim unselectedStroke As Stroke
        For Each unselectedStroke In args.DeselectedStrokes
            unselectedStroke.DrawingAttributes.Color = inkDA.Color
            selectedStrokes.Remove(unselectedStroke)
        Next unselectedStroke

    End Sub

    ' Property to indicate whether the user is inputting or
    ' selecting ink.  
    Public Property Mode() As InkMode
        Get
            Return Mode
        End Get

        Set(ByVal value As InkMode)
            modeState = value

            ' Set the DrawingAttributes of the DynamicRenderer
            If modeState = InkMode.Ink Then
                renderer.DrawingAttributes = inkDA
            Else
                renderer.DrawingAttributes = selectDA
            End If

            ' Reattach the visual of the DynamicRenderer to the InkPresenter.
            presenter.DetachVisuals(renderer.RootVisual)
            presenter.AttachVisuals(renderer.RootVisual, renderer.DrawingAttributes)
        End Set
    End Property
    Private Sub DrawingAttributesChanged(ByVal sender As Object, _
                                 ByVal e As PropertyDataChangedEventArgs)

        ' Reattach the visual of the DynamicRenderer to the InkPresenter 
        ' whenever the DrawingAttributes change.
        presenter.DetachVisuals(renderer.RootVisual)
        presenter.AttachVisuals(renderer.RootVisual, _
                                renderer.DrawingAttributes)

    End Sub

    ' Property to allow the user to change the pen's DrawingAttributes.
    Public ReadOnly Property InkDrawingAttributes() As DrawingAttributes
        Get
            Return inkDA
        End Get
    End Property

    ' Property to allow the user to change the Selector'newStroke DrawingAttributes.
    Public ReadOnly Property SelectDrawingAttributes() As DrawingAttributes
        Get
            Return selectDA
        End Get
    End Property

End Class

Weitere Informationen