views:

16

answers:

1

WPF,
MSDN:

UIElement.PreviewMouseMove Event

Occurs when the mouse pointer moves while the mouse pointer is over this element.

As I can see, this is not true... When I PreviewMouseDown, then PreviewMouseUp, an PreviewMouseMove event interferes. Is this a bug in Framework or MSDN documentation leak?

I just need to differentiate when a element really moves, and this "static" "Move" breaks my logic...

Function calls log:

Function: MyCanvas.OnPreviewMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs), Thread: 0x6F4 Main Thread
Function: MyCanvas.OnPreviewMouseMove(System.Windows.Input.MouseEventArgs), Thread: 0x6F4 Main Thread
Function: MyCanvas.OnPreviewMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs), Thread: 0x6F4 Main Thread
Function: MyCanvas.OnPreviewMouseMove(System.Windows.Input.MouseEventArgs), Thread: 0x6F4 Main Thread

The previous log was obtained when obtaining also the DoubleClick event, so, I don't suppose I moved the mouse even by a micrometer...

A: 

Using simple handlers I was able to get PreviewMouseUp and PreviewMouseDown (and LeftButtonUp/Down) events to fire reliably without MouseMove interfering. I suspect that the behavior you're seeing is because, especially with modern high resolution mice, it's impossible to not move the mouse (even very slightly) while clicking it. Actually, it would be very foolish to assume your users won't do that. There's nothing preventing the user moving the mouse while keeping the mouse button pressed.

If you want to register the event sequence only if the mouse is stationary, record the position on mouse down and only do your actions on mouse up if the position changed significantly.

Alex Paven
did you try on a canvas or something else? because when you DoubleClick you don't move the mouse between 2 clicks.
serhio
No, I surely don't move the mouse. this is almost impossible that, if I move, I do it always 1 single event in all test situations. If the mouse where so sensible, I could detect at least once the 2 MouseMove events between mouse Down and Up.
serhio
I beg to differ - even when you double-click you can very well move the mouse :) There's a threshold for the movement (I think) as well as the time for the DoubleClick event to register, but since the Double-click time is configurable, one can very easily increase it and move the mouse quite a ways before clicking the second time.
Alex Paven
As I said... I was able to detect the up and down events without move in-between on a border and on a canvas. Test environment was VS2010 with WPF 4, and a crappy mouse :)
Alex Paven
Just noticed that the trace you pasted actually mentions **On**PreviewMouseMove; it's entirely possible the framework actually calls it, but detects that the mouse hasn't moved and doesn't fire the actual event.
Alex Paven
@my previous comment: Actually, I took a quick peek with Reflector and there's nothing in the framework to suggest that.
Alex Paven
did you test on a Canvas?
serhio
Yes. (need to make the comment longer but don't have any other ideas to add)
Alex Paven
**On**PreviewMouseMove is the function I renamed using += OnMouse..., I used a Breackpoint Hittrace...
serhio
a dreakului apelurile estea... :)
serhio
Serios?! :) Nu stiu atunci ce sa zic, la mine se comporta ok.
Alex Paven