Mouse Hook in C#

  • Thread starter Thread starter Logan Mckinley
  • Start date Start date
L

Logan Mckinley

I need to know where the cursor is (x,y) and when it moves even when it is
not over my form. I know i can get the current location with:
System.Windows.Forms.Cursor.Position.X
System.Windows.Forms.Cursor.Position.X
But that does not raise an event and using a timer would be to processor
intensive. In MFC you would write a mousehook to solve this problem but when
i run the C# MouseHook code for from MSDN
(http://support.microsoft.com/?kbid=318804) it only seems to return anything
when the mouse is over the form. I am guessing i am doing something wrong
with the MouseHook.
Thanks in advance,
~Logan
 
Hi,

To achieve this you will have to install a system wide hook. As per the MSDN
documentation this is not possible using a managed assembly. You will have
to do this using C/C++ and then one option might be to expose an interface
that marshals the information back to the managed application.

Hope this helps

Chris Taylor
 
Not being a MS Insider I can only guess, but with time I am sure more and
more of the missing functiontionality will be addressed. However I think
that .NET is targeting application development and for a long time to come
we will be using C/C++ to write the more system level type code. While .NET
will provide a more secure environment for application level tasks.

Those are just my thoughts

Chris Taylor
 
So does the C# MouseHook per the MSDN article i posted catch mouse movements
across all of the elements of your form?
Thanks,
~Logan
 
Hi,

Yes, just to be certain I took an existing form with a multiline edit
control, three buttons a menu and a single line edit control plug the code
into the form and no matter where I moved with in the confines of the form
the hook function was called.

Chris Taylor
 
Back
Top