tags:

views:

368

answers:

4

I have object1 as a child of a Canvas. object1 has a MouseLeftButtonDown event handler which captures the object.

object1.CaptureMouse();

While still holding down the left mouse button, I move the mouse over object2. In code, how do I find out if the mouse is actually over object2 (or determine whichever object it is currently over)?

As best as I can tell, I can't use a mouse event handler for object2 as the call to object1's CaptureMouse() method on object1 disables other mouse events until I call object1's ReleaseMouseCapture() method.

A: 

You're right in that the 'CaptureMouse()' call is going to prevent other mouse events from happening.

If you then hook into the MouseMove event of object1, you can use the event arguments passed into that event handler to determine where the mouse currently is, and thereby determine where it is relative to the other object's coordinates that you're concerned with.

This article doesn't exactly cover what you want, but has some stuff that might be helpful to you figuring out what you're trying to do:

http://jesseliberty.com/2009/01/13/drag-and-drop-with-managed-code/

Jesse Taber
A: 

That article was somewhat helpful, so thanks for that. However, it is this:

"you can use the event arguments passed into that event handler to determine where the mouse currently is, and thereby determine where it is relative to the other object's coordinates that you're concerned with."

...that I'm trying to determine.

I can get the current position of the mouse. What I don't know how to do is to get the coordinates of the new object (object2) and then determine if the current mouse position is 'inside' object2.

In the case of a Shape see there might be a solution by looking at the clip geometry of the Shape but in my case object2 is a Canvas so that doesn't quite work.

+1  A: 

You will want to use the System.Windows.VisualTreeHelper:

VisualTreeHelper.FindElementsInHostCoordinates will "Retrieves a set of objects that are located within a specified point of an object's coordinate space."

there are two overloads of the function, one that takes a Point and one that takes a Rect.

Michael S. Scherotter
That did it. Thanks!
A: 

That did it. Thanks!