完善主体资料,免费赠送VIP会员!
* 主体类型
* 企业名称
* 信用代码
* 所在行业
* 企业规模
* 所在职位
* 姓名
* 所在行业
* 学历
* 工作性质
请先选择行业
您还可以选择以下福利:
行业福利,领完即止!

下载app免费领取会员

NULL

ad.jpg

二次开发教程:C#键盘钩子

发布于:2019-08-23 17:01:11

网友投稿

更多

       HookProc hookProc = null;

        IntPtr hookValue = IntPtr.Zero;

        const int WM_CLICK = 0x00F5;

        public Form1()

        {

            InitializeComponent();

        }


        //安装

        private void button1_Click(object sender, EventArgs e)

        {

            if (hookProc == null)

            {

                hookProc = new HookProc(this.MyProMethod);

                IntPtr hModule = NativeMethods.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);

                //局部钩子

                hookValue = NativeMethods.SetWindowsHookEx(HookType.WH_KEYBOARD, hookProc, hModule, NativeMethods.GetCurrentThreadId());

                //全局钩子

                //hookValue = NativeMethods.SetWindowsHookEx(HookType.WH_KEYBOARD_LL, hookProc, hModule, 0);

            }

        }


        private int MyProMethod(int nCode, IntPtr wParam, IntPtr lParam)

        {

            int code = nCode;

            int w = wParam.ToInt32();

            int l = lParam.ToInt32();


            //直接用IntPrt无法返回

            List<IntPtr> prts = GetHandle(this.Handle, IntPtr.Zero, null, "Test");

//向控件发送消息

            IntPtr tp = NativeMethods.SendMessage(prts.First(), WM_CLICK, IntPtr.Zero, IntPtr.Zero);


            return NativeMethods.CallNextHookEx(hookValue, nCode, wParam, lParam);

        }




相关类:


    public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

    public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);

    public enum HookType : int

    {

        WH_JOURNALRECORD = 0,

        WH_JOURNALPLAYBACK = 1,

        WH_KEYBOARD = 2,

        WH_GETMESSAGE = 3,

        WH_CALLWNDPROC = 4,

        WH_CBT = 5,

        WH_SYSMSGFILTER = 6,

        WH_MOUSE = 7,

        WH_HARDWARE = 8,

        WH_DEBUG = 9,

        WH_SHELL = 10,

        WH_FOREGROUNDIDLE = 11,

        WH_CALLWNDPROCRET = 12,

        WH_KEYBOARD_LL = 13,

        WH_MOUSE_LL = 14

    }

    public class NativeMethods

    {

        //设置钩子 

        [DllImport("user32.dll")]

        public static extern IntPtr SetWindowsHookEx(HookType idHook, HookProc lpfn, IntPtr hInstance, int threadId);



        //卸载钩子

        [DllImport("user32.dll", SetLastError = true)]

        [return: MarshalAs(UnmanagedType.Bool)]

        public static extern bool UnhookWindowsHookEx(IntPtr hhk);



        //调用下一个钩子 

        [DllImport("user32.dll")]

        public static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);



        [DllImport("kernel32.dll")]

        public static extern int GetCurrentThreadId();



        [DllImport("user32.dll")]

        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);


        [DllImport("user32.dll")]

        public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);


        [DllImport("Kernel32.dll")]

        public static extern IntPtr GetModuleHandle(string lpModuleName);



        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]

        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);



        [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]

        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);



        [DllImport("user32")]

        [return: MarshalAs(UnmanagedType.Bool)]

        public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);

    }


//t通过className和windowTitle来寻找控件

    public class ControlIntPrtUtils

    {

        private string className = string.Empty;

        private string windowTitle = string.Empty;

        public ControlIntPrtUtils(string className,string windowTitle)

        {

            this.className = className;

            this.windowTitle = windowTitle;

        }

        public List<IntPtr> GetHandle(IntPtr parentHandle, IntPtr childAfter)

        {

            List<IntPtr> prts = new List<IntPtr>();

            IntPtr hwnd = NativeMethods.FindWindowEx(parentHandle, IntPtr.Zero, className, windowTitle);

            if (hwnd != IntPtr.Zero)

            {

                prts.Add(hwnd);

                return prts;

            }

            GCHandle gch = GCHandle.Alloc(prts);

            NativeMethods.EnumChildWindows(parentHandle, new EnumWindowProc(EnumCallBack), GCHandle.ToIntPtr(gch));

            return prts;

        }

        private bool EnumCallBack(IntPtr hWnd, IntPtr parameter)

        {

            IntPtr hwnd = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, className, windowTitle);

            if (hwnd != IntPtr.Zero)

            {

                GCHandle gch = GCHandle.FromIntPtr(parameter);

                List<IntPtr> prts = gch.Target as List<IntPtr>;

                prts.Add(hwnd);

                return false;

            }

            return true;

        }

    }


本文版权归腿腿教学网及原创作者所有,未经授权,谢绝转载。

未标题-1.jpg

上一篇:二次开发教程:Revit开发关于创建Tab的问题

下一篇:二次开发教程:WPF 使用net 资源多语言

60acb4e0ef112.png