* Reset branch for WPF changes * Convert BMP to PNG; fix link-out-of-scope err * Add snippets for WPF... 6794 files!!!! * Add missing snippets * update file updated between migration * Fix paths to include * update breadcrumb and toc * fix index links * fix index links * fix index links * fix markdown
8.7 KiB
title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | helpviewer_keywords | ms.assetid | |||||
|---|---|---|---|---|---|---|---|---|---|
| How to: Select Ink from a Custom Control | 03/30/2017 |
|
|
5f3a45c6-6d40-4017-9b47-933f134ceba3 |
How to: Select Ink from a Custom Control
By adding an xref:System.Windows.Ink.IncrementalLassoHitTester to your custom control, you can enable your control so that a user can select ink with a lasso tool, similar to the way the xref:System.Windows.Controls.InkCanvas selects ink with a lasso.
This example assumes you are familiar with creating an ink-enabled custom control. To create a custom control that accepts ink input, see Creating an Ink Input Control.
Example
When the user draws a lasso, the xref:System.Windows.Ink.IncrementalLassoHitTester predicts which strokes will be within the lasso path's boundaries after the user completes the lasso. Strokes that are determined to be within the lasso path's boundaries can be thought of as being selected. Selected strokes can also become unselected. For example, if the user reverses direction while drawing the lasso, the xref:System.Windows.Ink.IncrementalLassoHitTester may unselect some strokes.
The xref:System.Windows.Ink.IncrementalLassoHitTester raises the xref:System.Windows.Ink.IncrementalLassoHitTester.SelectionChanged event, which enables your custom control to respond while the user is drawing the lasso. For example, you can change the appearance of strokes as the user selects and unselects them.
Managing the Ink Mode
It is helpful to the user if the lasso appears differently than the ink on your control. To accomplish this, your custom control must keep track of whether the user is writing or selecting ink. The easiest way to do this is to declare an enumeration with two values: one to indicate that the user is writing ink and one to indicate that the user is selecting ink.
[!code-csharpHowToSelectInk#2] [!code-vbHowToSelectInk#2]
Next, add two xref:System.Windows.Ink.DrawingAttributes to the class: one to use when the user writes ink, one to use when the user selects ink. In the constructor, initialize the xref:System.Windows.Ink.DrawingAttributes and attach both xref:System.Windows.Ink.DrawingAttributes.AttributeChanged events to the same event handler. Then set the xref:System.Windows.Input.StylusPlugIns.DynamicRenderer.DrawingAttributes%2A property of the xref:System.Windows.Input.StylusPlugIns.DynamicRenderer to the ink xref:System.Windows.Ink.DrawingAttributes.
[!code-csharpHowToSelectInk#3]
[!code-vbHowToSelectInk#3]
[!code-csharpHowToSelectInk#4]
[!code-vbHowToSelectInk#4]
Add a property that exposes the selection mode. When the user changes the selection mode, set the xref:System.Windows.Input.StylusPlugIns.DynamicRenderer.DrawingAttributes%2A property of the xref:System.Windows.Input.StylusPlugIns.DynamicRenderer to the appropriate xref:System.Windows.Ink.DrawingAttributes object and then reattach the xref:System.Windows.Input.StylusPlugIns.DynamicRenderer.RootVisual%2A Property to the xref:System.Windows.Controls.InkPresenter.
[!code-csharpHowToSelectInk#5] [!code-vbHowToSelectInk#5]
Expose the xref:System.Windows.Ink.DrawingAttributes as properties so applications can determine the appearance of the ink strokes and selection strokes.
[!code-csharpHowToSelectInk#6] [!code-vbHowToSelectInk#6]
When a property of a xref:System.Windows.Ink.DrawingAttributes object changes, the xref:System.Windows.Input.StylusPlugIns.DynamicRenderer.RootVisual%2A must be reattached to the xref:System.Windows.Controls.InkPresenter. In the event handler for the xref:System.Windows.Ink.DrawingAttributes.AttributeChanged event, reattach the xref:System.Windows.Input.StylusPlugIns.DynamicRenderer.RootVisual%2A to the xref:System.Windows.Controls.InkPresenter.
[!code-csharpHowToSelectInk#7] [!code-vbHowToSelectInk#7]
Using the IncrementalLassoHitTester
Create and initialize a xref:System.Windows.Ink.StrokeCollection that contains the selected strokes.
[!code-csharpHowToSelectInk#8] [!code-vbHowToSelectInk#8]
When the user starts to draw a stroke, either ink or the lasso, unselect any selected strokes. Then, if the user is drawing a lasso, create an xref:System.Windows.Ink.IncrementalLassoHitTester by calling xref:System.Windows.Ink.StrokeCollection.GetIncrementalLassoHitTester%2A, subscribe to the xref:System.Windows.Ink.IncrementalLassoHitTester.SelectionChanged event, and call xref:System.Windows.Ink.IncrementalHitTester.AddPoints%2A. This code can be a separate method and called from the xref:System.Windows.UIElement.OnStylusDown%2A and xref:System.Windows.UIElement.OnMouseDown%2A methods.
[!code-csharpHowToSelectInk#9] [!code-vbHowToSelectInk#9]
Add the stylus points to the xref:System.Windows.Ink.IncrementalLassoHitTester while the user draws the lasso. Call the following method from the xref:System.Windows.UIElement.OnStylusMove%2A, xref:System.Windows.UIElement.OnStylusUp%2A, xref:System.Windows.UIElement.OnMouseMove%2A, and xref:System.Windows.UIElement.OnMouseLeftButtonUp%2A methods.
[!code-csharpHowToSelectInk#10] [!code-vbHowToSelectInk#10]
Handle the xref:System.Windows.Ink.IncrementalLassoHitTester.SelectionChanged?displayProperty=nameWithType event to respond when the user selects and unselects strokes. The xref:System.Windows.Ink.LassoSelectionChangedEventArgs class has the xref:System.Windows.Ink.LassoSelectionChangedEventArgs.SelectedStrokes%2A and xref:System.Windows.Ink.LassoSelectionChangedEventArgs.DeselectedStrokes%2A properties that get the strokes that were selected and unselected, respectively.
[!code-csharpHowToSelectInk#11] [!code-vbHowToSelectInk#11]
When the user finishes drawing the lasso, unsubscribe from the xref:System.Windows.Ink.IncrementalLassoHitTester.SelectionChanged event and call xref:System.Windows.Ink.IncrementalHitTester.EndHitTesting%2A.
[!code-csharpHowToSelectInk#12] [!code-vbHowToSelectInk#12]
Putting it All Together.
The following example is a custom control that enables a user to select ink with a lasso.
[!code-csharpHowToSelectInk#1] [!code-vbHowToSelectInk#1]