HitTestResultCallback 代理人

定義

表示用來自訂點擊測試的回呼。 WPF 叫用 HitTestResultCallback 將點擊的測試交集報告給使用者。

C#
public delegate System.Windows.Media.HitTestResultBehavior HitTestResultCallback(HitTestResult result);

參數

result
HitTestResult

HitTestResult 值,表示從點擊測試傳回的視覺物件。

傳回值

HitTestResultBehavior

HitTestFilterBehavior,表示從點擊測試產生的動作。

範例

下列範例示範如何使用 值叫 HitTestHitTestResultCallback 。 也會定義對應的點擊測試回呼方法。

C#
// 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)
{
    // Retrieve 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;
}

備註

點擊測試回呼方法會定義您在視覺化樹狀結構中特定視覺物件上識別點擊測試時所執行的動作。 執行動作之後,您的回呼應該會傳回值,以判斷是否要繼續逐一 HitTestResultBehavior 查看任何其他視覺物件的視覺化樹狀結構。

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於

產品 版本
.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
Windows Desktop 3.0, 3.1, 5, 6, 7