完善主體資料,免費(fèi)贈(zèng)送VIP會(huì)員!
    * 主體類型
    * 企業(yè)名稱
    * 信用代碼
    * 所在行業(yè)
    * 企業(yè)規(guī)模
    * 所在職位
    * 姓名
    * 所在行業(yè)
    * 學(xué)歷
    * 工作性質(zhì)
    請(qǐng)先選擇行業(yè)
    您還可以選擇以下福利:
    行業(yè)福利,領(lǐng)完即止!

    下載app免費(fèi)領(lǐng)取會(huì)員

    NULL

    ad.jpg

    二次開發(fā)教程:C#鍵盤鉤子

    發(fā)布于:2019-08-23 17:01:11

    網(wǎng)友投稿

    更多

           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");

    //向控件發(fā)送消息

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


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

            }




    相關(guān)類:


        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

        {

            //設(shè)置鉤子 

            [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);



            //調(diào)用下一個(gè)鉤子 

            [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;

            }

        }


    本文版權(quán)歸腿腿教學(xué)網(wǎng)及原創(chuàng)作者所有,未經(jīng)授權(quán),謝絕轉(zhuǎn)載。

    未標(biāo)題-1.jpg

    上一篇:二次開發(fā)教程:Revit開發(fā)關(guān)于創(chuàng)建Tab的問題

    下一篇:二次開發(fā)教程:WPF 使用net 資源多語言

    主站蜘蛛池模板: 午夜福利一区二区三区高清视频| 性色A码一区二区三区天美传媒 | 波多野结衣中文字幕一区| 日韩精品一区二区三区影院| 久久se精品一区二区国产| 无码人妻一区二区三区精品视频| 亚洲另类无码一区二区三区| 日韩人妻一区二区三区蜜桃视频| 国产观看精品一区二区三区 | 末成年女AV片一区二区 | 国产精品香蕉一区二区三区| 国偷自产av一区二区三区| 国产福利电影一区二区三区久久老子无码午夜伦不 | 一区二区免费在线观看| 性色AV一区二区三区无码| 精品国产乱子伦一区二区三区| 久久无码人妻精品一区二区三区| 国产乱码精品一区三上| 国偷自产视频一区二区久| 国产精品一区二区毛卡片| 国产精品无码一区二区三区不卡 | 国产一区二区三区久久精品| 国产不卡视频一区二区三区| 香蕉久久AⅤ一区二区三区 | 久久99热狠狠色精品一区| 亚洲一区二区三区偷拍女厕| 亚洲色婷婷一区二区三区| 亚洲AV无一区二区三区久久| 久久精品国产一区二区三区肥胖 | 色天使亚洲综合一区二区| 国产日韩综合一区二区性色AV| 欧洲精品一区二区三区| 精品一区精品二区| 国产免费播放一区二区| 色狠狠一区二区三区香蕉蜜桃| 精品福利一区二区三| 精品日韩亚洲AV无码一区二区三区| 日韩精品无码中文字幕一区二区| 亚洲国产综合精品中文第一区| 亚洲一区二区影视| 在线|一区二区三区|