3

I have Windows Forms application and I need to capture the mouse movements outside my window. My simplified code in my window class is:

private void ButtonOnClick(object sender, EventArgs e)
{
 Capture = true;
 MouseMove += OnMouseMove;
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
 Console.Out.Write("!");
}

As you see, when user presses a button, my program should start tracking mouse (even if it is outside of window - this is a key featue!) But I get really strange behavior. If I move mouse inside the window, everything is great, ! are written to console. But when I move mouse outside of the window, only OnMouseMove is called only once (and the point is really outside). Then if I move mouse anywhere outside of the window it is not called anymore. If I return mouse back to window, everything is perfect. Move away - 1 message, move in the window - OK.

Can anybody help?

asked May 21, 2010 at 18:31

1 Answer 1

3

You would need a global mouse hook for that. I recommend that you first read something about hooks, eg. on MSDN. An example implementation in C# can be found at the CodeProject.

Hope that helps a bit.

answered May 21, 2010 at 18:44
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Just studied the solution from CodeProject and it works perfectly!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.