apocalypse
apocalypse

Reputation: 5884

Receive mouse move even cursor is outside control

I did something like scrollbar from Control. Everything is fine, but when cursor leaves control area, OnMouseMove is not received anymore.

When you use standard windows scrollbar, you can use it even if mouse cursor is outside the control's surface.

To avoid this, the only idea I have, is get cursor position from screen, then calculate scrollbar position on screen, and use timer or something to update my Control. But it sounds very hard and ugly.

Any idea how to fix it?

EDIT: I meant Control not UserControl.

EDIT2: It receives mouse! I had bug in code, I called MouseDown in MouseMove method, but in MouseDown I had X/Y constraint that will force to return if X<0 etc.

Upvotes: 2

Views: 4019

Answers (1)

Sebastian
Sebastian

Reputation: 8005

What you are looking for is called mouse capture and is described here:

http://msdn.microsoft.com/en-us/library/ms171545(v=vs.80).aspx

In short:

  • on mouse down in your control you "capture" the mouse input
  • then as long as no-one else captures the mouse, you will receive all mouse movement events, even those outside of your control
  • you can release capture if you don't require the data anymore, optionally in "OnMouseUp"

Upvotes: 2

Related Questions