Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 4
HitTest Method
 HitTest Method (Visual, Point)
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2010/.NET Framework 4

Other versions are also available for the following:
.NET Framework Class Library
VisualTreeHelper..::.HitTest Method (Visual, Point)

Returns the topmost Visual object of a hit test by specifying a Point.

Namespace:  System.Windows.Media
Assembly:  PresentationCore (in PresentationCore.dll)
Visual Basic
Public Shared Function HitTest ( _
    reference As Visual, _
    point As Point _
) As HitTestResult
C#
public static HitTestResult HitTest(
    Visual reference,
    Point point
)
Visual C++
public:
static HitTestResult^ HitTest(
    Visual^ reference, 
    Point point
)
F#
static member HitTest : 
        reference:Visual * 
        point:Point -> HitTestResult 

Parameters

reference
Type: System.Windows.Media..::.Visual
The Visual to hit test.
point
Type: System.Windows..::.Point
The point value to hit test against.

Return Value

Type: System.Windows.Media..::.HitTestResult
The hit test result of the Visual, returned as a HitTestResult type.

The VisualHit property of the return value represents the Visual object that was hit.

NoteNote

It is possible for a hit test that starts in a 2D visual tree to return RayHitTestResult, which is a 3D hit test result object. Hit testing that starts in a 2D visual tree seamlessly extends into any defined 3D visual tree.

The following example shows how to use the HitTest method to hit test the objects within a Canvas.

Visual Basic
        ' Respond to the left mouse button down event by initiating the hit test.
        Private Overloads Sub OnMouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
            ' Retrieve the coordinate of the mouse position.
            Dim pt As Point = e.GetPosition(CType(sender, UIElement))

            ' Perform the hit test against a given portion of the visual object tree.
            Dim result As HitTestResult = VisualTreeHelper.HitTest(myCanvas, pt)

            If result IsNot Nothing Then
                ' Perform action on hit visual object.
            End If
        End Sub
C#
// Respond to the left mouse button down event by initiating the hit test.
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    // Retrieve the coordinate of the mouse position.
    Point pt = e.GetPosition((UIElement)sender);

    // Perform the hit test against a given portion of the visual object tree.
    HitTestResult result = VisualTreeHelper.HitTest(myCanvas, pt);

    if (result != null)
    {
        // Perform action on hit visual object.
    }
}

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Relationship with Visibility or IsHitTestVisible properties      Ramon de Klein   |   Edit   |   Show History
This method doesn't care about the Visibility or IsHitTestVisible properties of UIElements (and derived objects), which is unexpected. If you need to take these properties into account, then use the other HitTest method that accepts a filter and callback and provide the filtering yourself. $0 $0 $0$0 $0 $0More information can be found on MS Connect https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=412961&;;SiteID=212.$0 $0 $0
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker