Skip to main content
.NET Framework Class Library
HitTestResultCallback Delegate

Represents a callback that is used to customize hit testing. WPF invokes the HitTestResultCallback to report hit test intersections to the user.

Namespace:   System.Windows.Media
Assembly:  PresentationCore (in PresentationCore.dll)
Syntax
Public Delegate Function HitTestResultCallback ( _
	result As [%$TOPIC/ms619160_en-us_VS_110_1_0_0_0_0%] _
) As [%$TOPIC/ms619160_en-us_VS_110_1_0_0_0_1%]
public delegate [%$TOPIC/ms619160_en-us_VS_110_1_0_1_0_0%] HitTestResultCallback(
	[%$TOPIC/ms619160_en-us_VS_110_1_0_1_0_1%] result
)
public delegate [%$TOPIC/ms619160_en-us_VS_110_1_0_2_0_0%] HitTestResultCallback(
	[%$TOPIC/ms619160_en-us_VS_110_1_0_2_0_1%]^ result
)
type HitTestResultCallback = 
    delegate of  
        result:[%$TOPIC/ms619160_en-us_VS_110_1_0_3_0_0%] -> [%$TOPIC/ms619160_en-us_VS_110_1_0_3_0_1%]

Parameters

result
Type: System.Windows.MediaHitTestResult

The HitTestResult value that represents a visual object that is returned from a hit test.

Return Value

Type: System.Windows.MediaHitTestResultBehavior
A HitTestFilterBehavior that represents the action resulting from the hit test.
Remarks

The hit test callback method defines the actions that you perform when a hit test is identified on a particular visual object in the visual tree. After you perform the actions, your callback is expected to return a HitTestResultBehavior value that determines whether to continue iterating through the visual tree for any other visual objects.

Examples

The following example shows how to invoke HitTest by using a HitTestResultCallback value. The corresponding hit test callback method is also defined.

		' Capture the mouse event and hit test the coordinate point value against
		' the child visual objects.
		Private Sub MyVisualHost_MouseLeftButtonUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
			' Retreive the coordinates of the mouse button event.
			Dim pt As Point = e.GetPosition(CType(sender, UIElement))

			' Initiate the hit test by setting up a hit test result callback method.
			VisualTreeHelper.HitTest(Me, Nothing, New HitTestResultCallback(AddressOf myCallback), New PointHitTestParameters(pt))
		End Sub

		' If a child visual object is hit, toggle its opacity to visually indicate a hit.
		Public Function myCallback(ByVal result As HitTestResult) As HitTestResultBehavior
			If result.VisualHit.GetType() Is GetType(DrawingVisual) Then
				If (CType(result.VisualHit, DrawingVisual)).Opacity = 1.0 Then
					CType(result.VisualHit, DrawingVisual).Opacity = 0.4
				Else
					CType(result.VisualHit, DrawingVisual).Opacity = 1.0
				End If
			End If

			' Stop the hit test enumeration of objects in the visual tree.
			Return HitTestResultBehavior.Stop
		End Function
// Capture the mouse event and hit test the coordinate point value against 
// the child visual objects. 
void MyVisualHost_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    // Retreive the coordinates of the mouse button event.
    System.Windows.Point pt = e.GetPosition((UIElement)sender);

    // Initiate the hit test by setting up a hit test result callback method.
    VisualTreeHelper.HitTest(this, null, new HitTestResultCallback(myCallback), new PointHitTestParameters(pt));
}

// If a child visual object is hit, toggle its opacity to visually indicate a hit. 
public HitTestResultBehavior myCallback(HitTestResult result)
{
    if (result.VisualHit.GetType() == typeof(DrawingVisual))
    {
        if (((DrawingVisual)result.VisualHit).Opacity == 1.0)
        {
            ((DrawingVisual)result.VisualHit).Opacity = 0.4;
        }
        else
        {
            ((DrawingVisual)result.VisualHit).Opacity = 1.0;
        }
    }

    // Stop the hit test enumeration of objects in the visual tree. 
    return HitTestResultBehavior.Stop;
}
Version Information

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.