1

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?

4

2 回答 2

2

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

于 2009-01-15T15:52:15.047 回答
2

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.

于 2009-01-15T16:06:55.173 回答