Want more features on Pastebin? Sign Up, it's FREE!
Guest

mouse hook - param problem

By: a guest on Aug 24th, 2010  |  syntax: C#  |  size: 2.40 KB  |  views: 156  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class globalMouse
  2.     {
  3.         public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
  4.         public delegate void MouseEventHandle(MouseHookStruct Info);
  5.  
  6.         [StructLayout(LayoutKind.Sequential)]
  7.         public class POINT
  8.         {
  9.             public int x;
  10.             public int y;
  11.         }
  12.  
  13.         [StructLayout(LayoutKind.Sequential)]
  14.         public class MouseHookStruct
  15.         {
  16.             public POINT pt;
  17.             public IntPtr hwnd;
  18.             public int wHitTestCode;
  19.             public int dwExtraInfo;
  20.         }
  21.  
  22.         const int WH_MOUSE = 14;
  23.  
  24.         int hhook = 0;
  25.  
  26.         public event MouseEventHandle MouseEvent;
  27.  
  28.         public globalMouse()
  29.         {
  30.             hook();
  31.         }
  32.  
  33.         ~globalMouse()
  34.         {
  35.             unhook();
  36.         }
  37.  
  38.         public void hook()
  39.         {
  40.             IntPtr hInstance = LoadLibrary("User32");
  41.             hhook = SetWindowsHookEx(WH_MOUSE, hookProc, hInstance, 0);
  42.         }
  43.  
  44.         public void unhook()
  45.         {
  46.             UnhookWindowsHookEx(hhook);
  47.         }
  48.  
  49.         /*
  50.          *
  51.          *
  52.          *
  53.          * problem - lParam.hwnd = 0.
  54.          * MyMouseHookStruct.hwnd = 0.
  55.          *
  56.          */
  57.         public int hookProc(int code, IntPtr wParam, IntPtr lParam)
  58.         {
  59.             if (code >= 0)
  60.             {
  61.                 if ((int)wParam == 0x204)
  62.                 {
  63.                     MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
  64.                     MouseEvent(MyMouseHookStruct);
  65.                 }
  66.             }
  67.             return CallNextHookEx(hhook, code, wParam, lParam);
  68.         }
  69.  
  70.         [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  71.         public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
  72.  
  73.         [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  74.         public static extern bool UnhookWindowsHookEx(int idHook);
  75.  
  76.         [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  77.         public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
  78.  
  79.         [DllImport("kernel32.dll")]
  80.         static extern IntPtr LoadLibrary(string lpFileName);
  81.     }
clone this paste RAW Paste Data