Receiving mouse events (WM_MOUSEMOVE, etc) outside of form

J

Jason

I'm trying to implement some pretty basic behavior in c#/.net 1.1
Forms but can't seem to find all the requisite pieces. When a user
clicks on the background of the form, I want the app to start
receiving MouseMove updates -- whether the pointer is over the form or
not (this being the crucial part) -- until the next MouseDown. It's
easy to receive MouseMove events while the pointer is over the form
but not when the pointer leaves. (Forms continue to receive
out-of-window MouseMove updates if a mouse-button is held down, ie a
drag, but not when just moving around.)

So, is there some simple way to do this in .NET?

The one approach that I've encounted which seems to have promise is
using the User32 SetCapture function. I can successfully capture the
mouse, but then I'm not sure how to receive the event messages
(specifically WM_MOUSEMOVE).

Okiedokie. Any ideas?? TIA
 
J

Jason

Well, actually, that only works when _dragging_ outside of the Form
(or control). So, for example, if the user presses a mouse button,
then drags outside the Form/Control while still holding the button,
said Form/Control receives MouseMove updates. However, even if the
Control.Capture is set to true, it won't receive general MouseMove
updates from outside the From... any ideas about how to do this?

Thanks!
Jason
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Jason,

Mouse capture won't help you with this. Mouse capture in windows works only
while mouse button is held down. Even windows form is designed in a way
where capture is released as soon as a mouse button is released. Otherwise
receiving mouse events from this point on is unreliable.

The only way to track the mouse if not button is pressed, IMHO, is to
install a windows hook. Fortunately WH_MOUSE_LL hooks can be programmed
with .NET.

The following is an article from MSDN Magazine obout writing windows hooks
in .NET

http://msdn.microsoft.com/msdnmag/issues/02/10/CuttingEdge/

The autor (Dino Esposito) doesn't talk about mouse hooks but there are good
helper class for writing windows hooks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top