完善主體資料,免費贈送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 資源多語言

    主站蜘蛛池模板: 3D动漫精品一区二区三区| 亚洲日韩激情无码一区| 精品无码国产一区二区三区51安| 八戒久久精品一区二区三区| 日本中文字幕一区二区有码在线| 秋霞午夜一区二区| 国产精品无码一区二区在线观 | 亚洲AV色香蕉一区二区| 老鸭窝毛片一区二区三区| 精品成人乱色一区二区| 中文字幕无码一区二区免费| 福利一区二区在线| 亚洲天堂一区二区三区| 亚洲国产精品一区二区久久| 少妇人妻偷人精品一区二区| 人成精品视频三区二区一区| 国产在线观看91精品一区| 亚洲av无码一区二区三区天堂| 人妻天天爽夜夜爽一区二区| 久久国产精品视频一区| 日本一区二区三区精品中文字幕| 成人无码一区二区三区| 久久久国产一区二区三区 | 精品国产一区二区三区免费看 | 亚洲福利视频一区| 无码免费一区二区三区免费播放| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 国产午夜精品一区二区三区极品| 亚洲天堂一区二区三区四区| 亚洲中文字幕在线无码一区二区| 性色A码一区二区三区天美传媒| 国产无套精品一区二区| 国产精品视频一区二区噜噜| 亚洲AV无码一区二区乱子伦| 一区二区在线视频| 性色av无码免费一区二区三区| 精品久久久中文字幕一区| 八戒久久精品一区二区三区| 嫩B人妻精品一区二区三区| 色妞色视频一区二区三区四区| 久久伊人精品一区二区三区|