HitTest Method
Collapse the table of content
Expand the table of content

StrokeCollection.HitTest Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Indicates whether a specified StylusPointCollection intersects with a StrokeCollection object.

Namespace:  System.Windows.Ink
Assembly:  System.Windows (in System.Windows.dll)

public StrokeCollection HitTest(
	StylusPointCollection stylusPointCollection


Type: System.Windows.Input.StylusPointCollection
The StylusPointCollection used to check for intersection with the Stroke object.

Return Value

Type: System.Windows.Ink.StrokeCollection
A StrokeCollection that contains the strokes that intersect with the points in the specified StylusPointCollection.

Consider a StrokeCollection named MyStrokes that contain strokes S0 - S2, and a StylusPointCollection named MySPC that contains points P0 - P3. The following illustration shows the position of these strokes and points.

Hit-Test for StrokeCollection

If you make the following HitTest call, the returned StrokeCollection contains S1 and S2.


The following code demonstrates the HitTest method. If you draw a stroke that intersects an existing stroke collection, the color of the existing stroke changes to red.

    <Rectangle Height="500" Width="500" Stroke="Black" Margin="50,50,0,0" />
    <InkPresenter x:Name="MyIP" Height="500" Width="500" Background="Transparent" Margin="50,50,0,0"
              LostMouseCapture="MyIP_LostMouseCapture" />

Stroke MyStroke;

//A new stroke object named MyStroke is created. MyStroke is added to the StrokeCollection of the InkPresenter named MyIP
private void MyIP_MouseLeftButtonDown(object sender, MouseEventArgs e)

    StylusPointCollection MyStylusPointCollection = new StylusPointCollection();

    MyStroke = new Stroke(MyStylusPointCollection);
    MyStroke.DrawingAttributes.Color = Colors.Yellow;
    MyStroke.DrawingAttributes.Width = 8;


//StylusPoint objects are collected from the MouseEventArgs and added to MyStroke.
private void MyIP_MouseMove(object sender, MouseEventArgs e)
    if (MyStroke != null)
        StrokeCollection HitStrokes = new StrokeCollection();
        HitStrokes = MyIP.Strokes.HitTest(e.StylusDevice.GetStylusPoints(MyIP));
        //Change the color of the strokes that the new strokes intersects with.
        for (int i = 0; i < HitStrokes.Count - 1; i++)
            HitStrokes[i].DrawingAttributes.Color = Colors.Red;


//MyStroke is completed
private void MyIP_LostMouseCapture(object sender, MouseEventArgs e)
    MyStroke = null;

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

© 2017 Microsoft