C # global mouse hook

Source: Internet
Author: User

C # global mouse hook

////// Global hook with the mouse ///Public class MouseHook {private const int WM_MOUSEMOVE = 0x200; private const int WM_LBUTTONDOWN = 0x201; private const int WM_RBUTTONDOWN = 0x204; private const int WM_MBUTTONDOWN = 0x207; private const int WM_LBUTTONUP = 0x202; private const int WM_RBUTTONUP = 0x205; private const int WM_MBUTTONUP = 0x208; private const int WM_LBUTTONDBLCLK = 0x203; private const int WM_RBUTTONDBLCLK = 0x206; private const int WM_MBUTTONDBLCLK = 0x209 ;////// Point ///[StructLayout (LayoutKind. Sequential)] public class POINT {public int x; public int y ;}////// Hook structure ///[StructLayout (LayoutKind. sequential)] public class MouseHookStruct {public POINT pt; public int hWnd; public int wHitTestCode; public int dwExtraInfo;} public const int WH_MOUSE_LL = 14; // mouse hook constant // function of the device hook [DllImport (user32.dll, CharSet = CharSet. auto, CallingConvention = CallingConvention. stdCall)] public static extern int SetWindowsHookEx (int idHook, HookProc lpfn, IntPtr hInstance, int threadId); // remove the hook function [DllImport (user32.dll, CharSet = CharSet. auto, CallingConvention = CallingConvention. stdCall)] public static extern bool UnhookWindowsHookEx (int idHook); // function of the next hook [DllImport (user32.dll, CharSet = CharSet. auto, CallingConvention = CallingConvention. stdCall)] public static extern int CallNextHookEx (int idHook, int nCode, Int32 wParam, IntPtr lParam); // global mouse event public event MouseEventHandler OnMouseActivity; // hook callback function public delegate int HookProc (int nCode, Int32 wParam, IntPtr lParam); // declare the private HookProc _ mouseHookProcedure; private static int _ hMouseHook = 0; // mouse hook handle ////// Constructor ///Public MouseHook (){}////// Destructor ///~ MouseHook () {Stop ();}////// Start the global hook ///Public void Start () {// install the mouse hook if (_ hMouseHook = 0) {// generate a HookProc instance. _ mouseHookProcedure = new HookProc (MouseHookProc); _ hMouseHook = SetWindowsHookEx (WH_MOUSE_LL, _ mouseHookProcedure, Marshal. getHINSTANCE (System. reflection. assembly. getExecutingAssembly (). getModules () [0]), 0); // if the device fails to Stop the hook if (_ hMouseHook = 0) {Stop (); throw new Exception (SetWindowsHookEx failed .);}}} ////// Stop the global hook ///Public void Stop () {bool retMouse = true; if (_ hMouseHook! = 0) {retMouse = UnhookWindowsHookEx (_ hMouseHook); _ hMouseHook = 0;} // if the hook fails to be detached if (! (RetMouse) throw new Exception (UnhookWindowsHookEx failed .);}////// Mouse hook callback function ///Private int MouseHookProc (int nCode, Int32 wParam, IntPtr lParam) {// if the message is normal and the user wants to listen to the mouse if (nCode> = 0) & (OnMouseActivity! = Null) {MouseButtons button = MouseButtons. none; int clickCount = 0; switch (wParam) {case WM_LBUTTONDOWN: button = MouseButtons. left; clickCount = 1; break; case WM_LBUTTONUP: button = MouseButtons. left; clickCount = 1; break; case WM_LBUTTONDBLCLK: button = MouseButtons. left; clickCount = 2; break; case WM_RBUTTONDOWN: button = MouseButtons. right; clickCount = 1; break; case WM_RBUTTONUP: Ton = MouseButtons. right; clickCount = 1; break; case WM_RBUTTONDBLCLK: button = MouseButtons. right; clickCount = 2; break;} // obtain the mouse information from the callback function: MouseHookStruct MyMouseHookStruct = (MouseHookStruct) Marshal. ptrToStructure (lParam, typeof (MouseHookStruct); MouseEventArgs e = new MouseEventArgs (button, clickCount, MyMouseHookStruct.pt. x, MyMouseHookStruct.pt. y, 0); // if you want to restrict the moving area of the mouse in the screen, you can set it here. // you need to consider it later. If (! Screen. primaryScreen. bounds. contains (e. x, e. y) {// return 1;} OnMouseActivity (this, e);} // start the next hook return CallNextHookEx (_ hMouseHook, nCode, wParam, lParam );}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.