tags:

views:

1171

answers:

2

I have tried to use the MouseMove event to track the position of the mouse.

protected override void OnMouseMove(MouseEventArgs e)

This works when the mouse is over the given UIElement (in this case, my application's window,) however I would like to access this data regardless of where the mouse is, and preferably even regardless of whether or not my application has focus.

I thought that Mouse.Capture was the solution, and I have used it to capture to my window, however MouseMove is still only raised when the mouse is over the window.

Does anyone know how to get MouseMove events (or similar) independent of mouse position?

+2  A: 

You could P/Invoke the Win32 GetCursorPos function. I think that would be the most reliable way: GetCursorPos on MSDN.

The required DllImport declaration can be found here: http://www.pinvoke.net/default.aspx/user32/GetCursorPos.html

driis
+1  A: 

If you want to do this within the bounds of your application, you should use the PreviewMouseMove event. Your Window will always get this first before it routes down to child elements. If you handle MouseMove at the Window level, then child elements may mark the event as handled and you may therefore never get the event.

An alternative to using PreviewMouseMove is to subclass Window and attach a class handler to MouseMove and specify that you would always like notification, regardless of whether someone else marked the event as handled. More info available here.

HTH, Kent

Kent Boogaart
Since this solution works only within the bounds of my application, it is not what I am looking for. Thanks for the link, though: WPF event handling is making much more sense now.
Paul Williams