First, we need to use the global mouse hook to get the mouse position in the current application, because the focus is not in the current application.
We will first create a new class library globalmousehook,
Then import the namespace: system. Windows. forms;
System. runtime. interopservices;
Rename the class as mousehook. The Code is as follows:
Using system. windows. forms; <br/> using system. runtime. interopservices; </P> <p> namespace globalmousehook <br/> {<br/> public class mousehook <br/> {<br/> private const int wm_mousemove = 0x200; <br/> private const int wm_lbuttondown = 0x201; <br/> private const int wm_rbuttondown = 0x204; <br/> private const int wm_mbuttondown = 0x207; <br/> private const int wm_lbuttonup = 0x202; <br/> private cons T int wm_rbuttonup = 0x205; <br/> private const int wm_mbuttonup = 0x208; <br/> private const int wm_lbuttondblclk = 0x203; <br/> private const int wm_rbuttondblclk = 0x206; <br/> private const int wm_mbuttondblclk = 0x209; </P> <p> // global event <br/> public event mouseeventhandler onmouseactivity; </P> <p> static int hmousehook = 0; // mouse hook handle </P> <p> // mouse constant <br/> Public const int wh_mouse_ll = 14; // mouse H Ook constant </P> <p> hookproc mousehookprocedure; // declare the type of the mouse hook event. </P> <p> // declare the mail type of a point <br/> [structlayout (layoutkind. sequential)] <br/> public class point <br/>{< br/> Public int X; <br/> Public int y; <br/>}</P> <p> // declare the delivery structure type of the mouse hook. <br/> [structlayout (layoutkind. sequential)] <br/> public class mousehookstruct <br/> {<br/> Public point pt; <br/> Public int hwnd; <br/> Public int whittestcode; <B R/> Public int dwextrainfo; <br/>}</P> <p> // device hook function <br/> [dllimport ("user32.dll", charset = charset. auto, callingconvention = callingconvention. stdcall)] <br/> Public static extern int setwindowshookex (INT idhook, hookproc lpfn, intptr hinstance, int threadid ); </P> <p> // remove the hook function <br/> [dllimport ("user32.dll", charset = charset. auto, callingconvention = callingconvention. stdcall)] <br/> Public static exter N bool unhookwindowshookex (INT idhook); </P> <p> // function of the next hook <br/> [dllimport ("user32.dll", charset = charset. auto, callingconvention = callingconvention. stdcall)] <br/> Public static extern int callnexthookex (INT idhook, int ncode, int32 wparam, intptr lparam ); </P> <p> Public Delegate int hookproc (INT ncode, int32 wparam, intptr lparam ); </P> <p> /// <summary> <br/> // The constructor of the ink recognition class to construct an instance of the current class. <br/> /// </Summary> <Br/> Public mousehook () <br/>{< br/>}</P> <p> // destructor. <br/> ~ Mousehook () <br/>{< br/> stop (); <br/>}</P> <p> Public void start () <br/>{< br/> // install the mouse hook <br/> If (hmousehook = 0) <br/>{< br/> // generate a hookproc instance. <br/> mousehookprocedure = new hookproc (mousehookproc); </P> <p> hmousehook = setwindowshookex (wh_mouse_ll, mousehookprocedure, Marshal. gethinstance (system. reflection. assembly. getexecutingassembly (). getmodules () [0]), 0); </P> <p> // if the device fails to stop the hook <br/> If (Hmousehook = 0) <br/>{< br/> stop (); <br/> throw new exception ("setwindowshookex failed. "); <br/>}</P> <p> Public void stop () <br/>{< br/> bool retmouse = true; <br/> If (hmousehook! = 0) <br/>{< br/> retmouse = unhookwindowshookex (hmousehook); <br/> hmousehook = 0; <br/>}</P> <p> // If Hook removal fails <br/> If (! (Retmouse) throw new exception ("unhookwindowshookex failed. "); <br/>}</P> <p> private int mousehookproc (INT ncode, int32 wparam, intptr lparam) <br/>{< br/> // if the message is normal and the user wants to listen to the mouse <br/> If (ncode> = 0) & (onmouseactivity! = NULL) <br/>{< br/> mousebuttons button = mousebuttons. none; <br/> int clickcount = 0; </P> <p> switch (wparam) <br/>{< br/> case wm_lbuttondown: <br/> button = mousebuttons. left; <br/> clickcount = 1; <br/> break; <br/> case wm_lbuttonup: <br/> button = mousebuttons. left; <br/> clickcount = 1; <br/> break; <br/> case wm_lbuttondblclk: <br/> button = mousebuttons. left; <br/> clickcount = 2; <br/> break; <br/> case wm_rbuttondown: <br/> button = mousebuttons. right; <br/> clickcount = 1; <br/> break; <br/> case wm_rbuttonup: <br/> button = mousebuttons. right; <br/> clickcount = 1; <br/> break; <br/> case wm_rbuttondblclk: <br/> button = mousebuttons. right; <br/> clickcount = 2; <br/> break; <br/>}</P> <p> // obtain the mouse information from the callback function <br/> mousehookstruct mymousehookstruct = (mousehookstruct) Marshal. ptrtostructure (lparam, typeof (mousehookstruct); <br/> mouseeventargs E = new mouseeventargs (button, clickcount, mymousehookstruct.pt. x, mymousehookstruct.pt. y, 0); <br/> // If (E. x> 700) return 1; // you can set <br/> onmouseactivity (this, e) here to restrict the moving area of the mouse in the screen ); <br/>}< br/> return callnexthookex (hmousehook, ncode, wparam, lparam); <br/>}< br/>}
In this way, a global mouse hook is created. You only need to reference this DLL elsewhere.
Example:
Public partial class form1: Form <br/>{< br/> mousehook mouse = new mousehook (); <br/> Public form1 () <br/>{< br/> initializecomponent (); <br/> mouse. onmouseactivity + = new mouseeventhandler (mouse_onmouseactivity); <br/> mouse. start (); <br/>}</P> <p> void mouse_onmouseactivity (Object sender, mouseeventargs e) <br/>{< br/> string STR = "X: "+ E. X + "Y:" + E. y; <br/> This. TEXT = STR; <br/>}< br/>}
In this way, the cursor position can be displayed in the global state, no matter where your focus is or whether your focus is on the current form.
From: http://blog.csdn.net/sohighthesky/archive/2009/04/15/4074757.aspx