2012年8月9日 星期四

C# hook


View Code
 /*  
     lanuage c#
     date:2012/3/10
     builder:charlie
     mouse hook
 */
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;
 using System.Runtime.InteropServices;
 using System.Diagnostics;
 using System.Reflection;
 using System.Threading;

 namespace myhook
 {
     public partial class Form1 : Form
     {
         #region const

         private int mousehook = 0;
         private const int WH_LBUTTONDOWN = 0x201;
         private const int WM_RBUTTONDOWN = 0x204;
         private const int WH_MOUSE_LL = 14;
         private const int WM_MOUSEWHEEL = 0x020A;

         #endregion

         #region delegate

         /// <summary>
 /// Hookproc is a pointer to application-defined or library-defined callback function
 /// </summary>
 /// <param name="nCode"></param>
 /// if nCode is greater than zero, than hook procees.
 /// else it will pass a messge to the CallNextHookEx function, and return the value it returns.
 /// <param name="wParam"></param>
 /// specifies whether the message was sent by the current thread.
 /// <param name="lParam"></param>
 /// Pointer to a CWPSTRUCT structure that contains details about the message
 /// <returns></returns>
         public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
         private HookProc MouseHookProcedure;

         #endregion

         #region api
         //find window
         [DllImport("user32.dll", EntryPoint = "FindWindow")]
         public static extern int FindWindow(
             string lpClassName,
             string lpWindowName
         );
         //get window
         [DllImport("user32.dll", EntryPoint = "GetWindowRect")]
         public static extern int GetWindowRect(
             int hwnd,
             ref Rectangle lpRect
         );
         //install hook
         [DllImport("user32.dll")]
         public static extern int SetWindowsHookEx(
             int idHook,
             HookProc lpfn,
             IntPtr hInstance,
             int threadId
         );
         //uninstall hook
         [DllImport("user32.dll", EntryPoint = "UnhookWindowsHookEx")]
         public static extern bool UnhookWindowsHookEx(
             int hHook
         );
         //next hook
         [DllImport("user32.dll")]
         public static extern int CallNextHookEx(
             int idHook,
             int nCode,
             int wParam,
             IntPtr lParam
         );
         //hook id
         [DllImport("kernel32.dll")]
         public static extern int GetCurrentThreadId();
         //handle
         [DllImport("kernel32.dll")]
         public static extern IntPtr GetModuleHandle(string name);
         //mouse structure
         [StructLayout(LayoutKind.Sequential)]
         public struct MOUSEHOOKSTRUCT
         {
             public Point pt;
             public int hwnd;
             public int wHitTestCode;
             public int dwExtraInfo;
         }
         #endregion

         #region events

         public Form1()
         {
           
             InitializeComponent();
         }

         private void StartmouseHook()
         {
             mousehook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProcedure, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
         }
         private void StopmouseHook()
         {
             bool stop = true;
             stop = UnhookWindowsHookEx(mousehook);
         }
         private int MouseHookProc(int nCode, int wParam, IntPtr lParam)
         {
             if (nCode >= 0)
             {
                 MOUSEHOOKSTRUCT mouse = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT));
                 label1.Text = "x=" + mouse.pt.X+""+"y="+mouse.pt.Y;
                 if (wParam == WM_RBUTTONDOWN || wParam == WH_MOUSE_LL || wParam == WH_LBUTTONDOWN)
                 {
                     return 1;
                 }
             }
             return CallNextHookEx(mousehook, nCode, wParam, lParam);
         }
     
         private void Form1_Load(object sender, EventArgs e)
         {
             MouseHookProcedure = new HookProc(MouseHookProc);
             this.StartmouseHook();
         }

         private void button_start_Click(object sender, EventArgs e)
         {
             if (textbox_1.Text == "robinho04")
             {
                 this.StopmouseHook();
                 this.Close();
             }
         }

         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
         {
         }
       
         #endregion
     }
 }

沒有留言:

張貼留言