1

I'm using the code below to see when the mouse right button is clicked, if it hits a target (Drawing) or not.

Now If the mouse hits the target a message will be shown stating that we hit the target.

But where can I show a message that the target was NOT hit? VisualTreeHelper.HitTest() doesn't seem to return a value indicating that the target was hit or not.

private void OnMouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
    var x = MousePos.RightDown.X;
    var y = MousePos.RightDown.Y;

    var hitRect = new Rect(x - 2, y - 2, 4, 4);
    var geom = new RectangleGeometry(hitRect);

    VisualTreeHelper.HitTest(Drawing, 
                             null, 
                             MyCallback, 
                             new GeometryHitTestParameters(geom));

    // Where should I put the MessageBox.Show("You did not hit the target");
    // If I put it here it is displayed anyway

}

private HitTestResultBehavior MyCallback(HitTestResult result)
{
    MessageBox.Show("You hit the target");
    return HitTestResultBehavior.Stop;
}
Vahid
  • 5,144
  • 13
  • 70
  • 146

2 Answers2

2

Have some class level flag to indicate whether hit is successful or not. Set the flag to true from MyCallback and show message based on that flag.

bool isTargetHit;    
private void OnMouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
    isTargetHit = false;

    .......
    VisualTreeHelper.HitTest(Drawing, 
                             null, 
                             MyCallback, 
                             new GeometryHitTestParameters(geom));

    if(isTargetHit)
    {
        MessageBox.Show("You hit the target");
    }
    else
    {
        MessageBox.Show("You did not hit the target");
    } 
}

private HitTestResultBehavior MyCallback(HitTestResult result)
{
    isTargetHit = true;
    return HitTestResultBehavior.Stop;
}
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • Thanks Rohit, this works but isn't it odd that it is not returning a value? – Vahid Sep 13 '14 at 15:06
  • Other overload where you pass Visual and Point get you result but when you provide callback than it won't return result which does makes sense. – Rohit Vats Sep 13 '14 at 15:12
  • The other overload accepts a rectangle geom instead of Point too? – Vahid Sep 13 '14 at 15:18
  • 1
    @Vahid No, there is only the one with the Point argument. See the [overload list](http://msdn.microsoft.com/en-us/library/System.Windows.Media.VisualTreeHelper.HitTest.aspx). – Clemens Sep 13 '14 at 15:19
  • 1
    Thanks Rohit & Clemens for the answers. – Vahid Sep 13 '14 at 15:20
2

In addition to what Rohit has said, you might also use a local flag and an anonymous callback method like this:

private void OnMouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
    bool isTargetHit = false;

    VisualTreeHelper.HitTest(
        Drawing,
        null,
        r =>
        {
            isTargetHit = true;
            return HitTestResultBehavior.Stop;
        },
        new GeometryHitTestParameters(geom));

    if (isTargetHit)
    {
        MessageBox.Show("You hit the target");
    }
    else
    {
        MessageBox.Show("You did not hit the target");
    }
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Thanks Clemens, I was looking for something like this actually. This way I will have both the logic for hitting and not hitting at one place. – Vahid Sep 13 '14 at 15:16