Stroke.HitTest Method

Definition

Returns whether the Stroke intersects or is within a certain area.

Overloads

HitTest(Point)

Returns a value that indicates whether current Stroke intersects the specified point.

HitTest(IEnumerable<Point>, Int32)

Returns a value that indicates whether the current Stroke is within the specified bounds.

HitTest(IEnumerable<Point>, StylusShape)

Returns whether the specified path intersects the Stroke using the specified StylusShape.

HitTest(Point, Double)

Returns a value that indicates whether current Stroke intersects the specified area.

HitTest(Rect, Int32)

Returns a value that indicates whether the Stroke is within the bounds of the specified rectangle.

Remarks

You can use the HitTest methods to determine whether a Stroke intersects a certain point or is within specified bounds.

The following methods check whether the Stroke is intersected.

The following methods check whether the Stroke is surrounded.

HitTest(Point)

Returns a value that indicates whether current Stroke intersects the specified point.

public bool HitTest (System.Windows.Point point);

Parameters

point
Point

The Point to hit test.

Returns

true if point intersects the current stroke; otherwise, false.

Examples

The following example changes the color of a Stroke if it intersects a certain area.

Point myPoint = new Point(100, 100);

if (myStroke.HitTest(myPoint, 10))
{
    myStroke.DrawingAttributes.Color = Colors.Red;
}

Remarks

This method behaves the same way as the overloaded HitTest(Point, Double) method does when diameter is 1.

Applies to

.NET Framework 4.8.1 y otras versiones
Producto Versiones
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

HitTest(IEnumerable<Point>, Int32)

Returns a value that indicates whether the current Stroke is within the specified bounds.

public bool HitTest (System.Collections.Generic.IEnumerable<System.Windows.Point> lassoPoints, int percentageWithinLasso);

Parameters

lassoPoints
IEnumerable<Point>

An array of type Point that represents the bounds of the area to hit test.

percentageWithinLasso
Int32

The percentage of the length of the Stroke, that must be in lassoPoints for the Stroke to be considered hit.

Returns

true if the current stroke is within the specified bounds; otherwise false.

Examples

The following example renders a stroke purple if at least 80% of the stroke is within the bounds of myPoints.

Point[] myPoints = new Point[] {
    new Point(100, 100),
    new Point(200, 100),
    new Point(200, 200),
    new Point(100, 200)};

if (aStroke.HitTest(myPoints, 80))
{
    aStroke.DrawingAttributes.Color = Colors.Purple;
}

Remarks

The HitTest method connects the first and last points in lassoPoints to create the lasso.

Applies to

.NET Framework 4.8.1 y otras versiones
Producto Versiones
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

HitTest(IEnumerable<Point>, StylusShape)

Returns whether the specified path intersects the Stroke using the specified StylusShape.

public bool HitTest (System.Collections.Generic.IEnumerable<System.Windows.Point> path, System.Windows.Ink.StylusShape stylusShape);

Parameters

path
IEnumerable<Point>

The path that stylusShape follows for hit testing.

stylusShape
StylusShape

The shape of path with which to hit test.

Returns

true if stylusShape intersects the current stroke; otherwise, false.

Examples

The following code renders a stroke purple if the stroke intersects the path of myPoints.

Point[] myPoints = new Point[] {
    new Point(100, 100),
    new	Point(200, 100),
    new	Point(200, 200),
    new	Point(100, 200)};

EllipseStylusShape myStylus = new EllipseStylusShape(5.0, 5.0, 0.0);

if (aStroke.HitTest(myPoints, myStylus))
{
    aStroke.DrawingAttributes.Color = Colors.Purple;
}

Remarks

The HitTest method uses stylusShape to hit test the stroke along eraserPath.

Applies to

.NET Framework 4.8.1 y otras versiones
Producto Versiones
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

HitTest(Point, Double)

Returns a value that indicates whether current Stroke intersects the specified area.

public bool HitTest (System.Windows.Point point, double diameter);

Parameters

point
Point

The Point that defines the center of the area to hit test.

diameter
Double

The diameter of the area to hit test.

Returns

true if the specified area intersects the current stroke; otherwise, false.

Examples

The following example changes the color of a Stroke if it intersects a certain area.

Point myPoint = new Point(100, 100);

if (myStroke.HitTest(myPoint, 10))
{
    myStroke.DrawingAttributes.Color = Colors.Red;
}

Applies to

.NET Framework 4.8.1 y otras versiones
Producto Versiones
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

HitTest(Rect, Int32)

Returns a value that indicates whether the Stroke is within the bounds of the specified rectangle.

public bool HitTest (System.Windows.Rect bounds, int percentageWithinBounds);

Parameters

bounds
Rect

A Rect that represents the bounds of the area to hit test.

percentageWithinBounds
Int32

The percentage of the length of the Stroke, that must be in percentageWithinBounds for the Stroke to be considered hit.

Returns

true if the current stroke is within the bounds of bounds; otherwise, false.

Examples

The following example renders a stroke purple if at least 80% of the stroke is within the Rect.

Rect rect1 = new Rect(100, 100, 100, 100);

if (aStroke.HitTest(rect1, 80))
{
    aStroke.DrawingAttributes.Color = Colors.Purple;
}

Applies to

.NET Framework 4.8.1 y otras versiones
Producto Versiones
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9