I have a strange question and a vb.net 2010 and wpf4 project. I have a label that, when clicked, captures the mouse (MyLabel.captureMouse) and follows it around the screen until the mouse clicks again, at which point the object and the mousecapture is released.
However, I still need the functionality of mouseenter for another object. How do I get these two things to work together?
EDIT: There are two solutions to this, it seems, one of which I discovered. However, since Rick’s should work as well (though untested because of my deadline), and I hate answering my own questions on here, I accepted his answer.
In the interim of waiting for him to comment back to a problem I had, I wound up discovering my own solution. Thus, be sure to read both my answer and Rick’s.
If the other object is one of your objects, then your label and the other object can cooperate while you have the mouse captured using synthetic events. For example, in your mouse move handler, you can check
Mouse.DirectlyOverto see if it is the other object and if so, do a little bookkeeping and then callRaiseEventwith eitherMouseEnterorMouseLeaveon the other object. If you have a bunch of these objects then you just have more bookkeeping to do.Edit:
The above refers to
Mouse.DirectlyOverwhich specifically does not work when the mouse is captured. To make the above more concrete and to fix that error, here is a complete working example.Here is the markup showing a canvas, a rectangle with mouse capture handling, and an ellipse with enter/leave handling:
and here are the event handlers that demonstrate how to generate synthetic enter/leave events (but only for the ellipse) while the mouse is captured:
If you run the demo under the debugger you’lll see that the enter/leave handlers are called whether the mouse is captured or not.