关闭

an example for mouse hook

1444人阅读 评论(0) 收藏 举报

还是上一个问题,看到论坛里有人指点用钩子。但是从来没有接触过

所以弄了个例子试试,作为开始吧。

窗体一个,按钮一个

using System.Runtime.InteropServices;


//
//This describes how to set a hook that is specific to a thread and to
//a hook procedure by using the mouse hook as an example. You can use hooks to monitor
//certain types of events. You can associate these events with a specific thread or with all
//of the threads in the same desktop as a calling thread.

namespace ThreadSpecificMouseHook
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
        //Declare hook handle as int.
        static int hHook = 0;

        //Declare mouse hook constant.
        //For other hook types, you can obtain these values from Winuser.h in Microsoft SDK.
        public const int WH_MOUSE = 7;


        //Declare MouseHookProcedure as HookProc type.
        HookProc MouseHookProcedure;

 

        //Declare wrapper managed POINT class.
        [StructLayout(LayoutKind.Sequential)]
        public class POINT
        {
            public int x;
            public int y;
        }

        //Declare wrapper managed MouseHookStruct class.
        [StructLayout(LayoutKind.Sequential)]
        public class MouseHookStruct
        {
            public POINT pt;
            public int hwnd;
            public int wHitTestCode;
            public int dwExtraInfo;
        }

        //Import for SetWindowsHookEx function.
        //Use this function to install thread-specific hook.
        [DllImport("user32.dll", CharSet = CharSet.Auto,
         CallingConvention = CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn,
        IntPtr hInstance, int threadId);

        //Import for UnhookWindowsHookEx.
        //Call this function to uninstall the hook.
        [DllImport("user32.dll", CharSet = CharSet.Auto,
         CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);

        //Import for CallNextHookEx.
        //Use this function to pass the hook information to next hook procedure in chain.
        [DllImport("user32.dll", CharSet = CharSet.Auto,
         CallingConvention = CallingConvention.StdCall)]
        public static extern int CallNextHookEx(int idHook, int nCode,
        IntPtr wParam, IntPtr lParam);


        //add a button
        private void button2_Click(object sender, EventArgs e)
        {
            if (hHook == 0)
            {
                // Create an instance of HookProc.
                MouseHookProcedure = new HookProc(Form1.MouseHookProc);

                hHook = SetWindowsHookEx(WH_MOUSE,
                            MouseHookProcedure,
                            (IntPtr)0,
                            AppDomain.GetCurrentThreadId());
                //If SetWindowsHookEx fails.
                if (hHook == 0)
                {
                    MessageBox.Show("SetWindowsHookEx Failed");
                    return;
                }
                button2.Text = "UnHook Windows Hook";
            }
            else
            {
                bool ret = UnhookWindowsHookEx(hHook);
                //If UnhookWindowsHookEx fails.
                if (ret == false)
                {
                    MessageBox.Show("UnhookWindowsHookEx Failed");
                    return;
                }
                hHook = 0;
                button2.Text = "Set Windows Hook";
                this.Text = "Mouse Hook";
            }

        }

        public static int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            //Marshall the data from callback.
            MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));

            if (nCode < 0)
            {
                return CallNextHookEx(hHook, nCode, wParam, lParam);
            }
            else
            {
                //Create a string variable with shows current mouse. coordinates
                String strCaption = "x = " +
                        MyMouseHookStruct.pt.x.ToString("d") +
                            "  y = " +
                MyMouseHookStruct.pt.y.ToString("d");
                //Need to get the active form because it is a static function.
                Form tempForm = Form.ActiveForm;

                //Set the caption of the form.
                tempForm.Text = strCaption;
                return CallNextHookEx(hHook, nCode, wParam, lParam);
            }
        }
    
   
  
        //
        //You cannot implement global hooks in Microsoft .NET Framework.
        //To install a global hook, a hook must have a native dynamic-link library (DLL)
        //export to inject itself in another process that requires a valid, consistent function to
        //has no concept of a consistent value for a function pointer because these function pointers
        //are proxies that are built dynamically.
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:306459次
    • 积分:4271
    • 等级:
    • 排名:第7070名
    • 原创:79篇
    • 转载:33篇
    • 译文:55篇
    • 评论:103条
    文章分类
    最新评论