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

Stroke.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 Stroke object.

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

public bool HitTest(
	StylusPointCollection stylusPointCollection


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

Return Value

Type: System.Boolean
true if the specified StylusPointCollection intersects with the Stroke object; otherwise, false.

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

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)

        //Iterate through the stroke collection
        for (int i = 0; i < MyIP.Strokes.Count - 1; i++)
            Stroke HitStroke = MyIP.Strokes[i];
            //If the new stroke intersects with an existing stroke, the color of the 
            //existing stroke is changed to red.
            if (HitStroke.HitTest(e.StylusDevice.GetStylusPoints(MyIP)))
                HitStroke.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