an example for mouse hook

原创 2006年05月20日 23:57:00

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

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

窗体一个,按钮一个

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.
    }
}

相关文章推荐

This chapter walks you through an example of single sign-on for intranet and extranet web users who

This chapter walks you through an example of single sign-on for intranet and extranet web users who ...

Looking for an example for inserting content into the response using a servlet filter

I’ve been searching the net and stackoverflow for an example of somebody inserting content into the ...
  • gmemai
  • gmemai
  • 2015年09月01日 05:42
  • 665

Shell,Mouse,Keyboard,CBT Hook控件包

  • 2006年02月23日 09:05
  • 20KB
  • 下载

用WH_MOUSE做hook时,用调试语句OutputDebugString打印调试信息的时候出现的一个问题

直接上代码 //char buf1[50]={0}; //sprintf(buf1,"/nMouse Handle value is %d/n",g_hmouse); //OutputDebu...

全局Mouse Hook的例子

  • 2006年02月23日 09:05
  • 187KB
  • 下载

#678 – 通过IsHitTestVisible属性使界面元素不响应鼠标(Hide an Element from the Mouse with IsHitTestVisible Property)

通过设置IsHitTestVisible 属性值为false,可以使界面元素不响应鼠标,鼠标事件也不会被触发。 在下面的例子中有两个Label,我们将其中一个的IsHitTestVisible 属性设...

HOOK 记录和模拟 Keyboard Mouse

  • 2014年07月08日 15:46
  • 65KB
  • 下载

Mouse消息钩子的全新HOOK

  • 2008年10月31日 21:54
  • 381KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:an example for mouse hook
举报原因:
原因补充:

(最多只允许输入30个字)