1

I have an application wich open a modal form with the ShowDialog method. Once this form is displayed I want to capture the mouse movement even if the cursor is outside the form. How can I capture the mouse movement? I saw something with the Capture property but I cannot manage to make it work.

[edit]

I want to be notified if the mouse move outside the form.

asked Sep 15, 2009 at 10:28

3 Answers 3

2

The Capture property is the correct way, but there are some limitations.

  • Only the foreground window can capture the mouse
  • Mouse capturing can be disabled by other parts of the system
  • The Win32 API function SetCapture gets reset everytime a "mouse up" event occours. I assume that also applies for .NET.

See the remarks section of Capture property.

When the mouse is captured, you'll receive the usual events but with a wider mouse coordinate range (for example a negative X position, if the mouse is left of the capturing control)

Mouse capturing is fragile, because of it's global nature. Check if there are other ways to handle certain events. Perhaps the MouseLeave or MouseEnter events are enough in your case.

answered Sep 15, 2009 at 10:38
Sign up to request clarification or add additional context in comments.

1 Comment

The capture is done only if the cursor is hover one of the application form but this is ok for me. I set Capture after the Load event.
2

You can just use the static property Control.MousePosition.

answered Sep 15, 2009 at 10:35

Comments

0

You can read the position of the cursor, using the Cursor.Position property, see Cursor.Position

answered Sep 15, 2009 at 10:32

Comments

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.