得物钩子什么用

得物钩子什么用,第1张

这种问题其实没什么必要来问,自己先查MSDN想想办法,对自己有好处。

既然是求所有元素,那不管子节点是否展开了。

估计代码:

HTREEITEM hSelectItem = m_treeGetSelectedItem();

all_item(hSelectItem);

void all_item(HTREEITEM hItem)

{

HTREEITEM hChildItem = m_treeGetChildItem(hItem);

HTREEITEM hNext = NULL;

while(hChildItem)

{

//GetItemData(hChildItem)还是GetItemText自己取

if(hChildItemItemHasChildren(hItem))

{

all_item(hChildItem);

}

hNext = m_treeGetNextItem(hChildItem);

hChildItem = hNext;

}

}

自己测试了下有点问题,新代码

void ListTree(HTREEITEM hItem)

{

HTREEITEM hChildItem = m_treeGetChildItem(hItem);

HTREEITEM hNext = NULL;

while(hChildItem)

{

//GetItemData(hChildItem)还是GetItemText自己取

TRACE( m_treeGetItemText(hChildItem) + _T("\n"));

if(m_treeItemHasChildren(hChildItem))

{

ListTree(hChildItem);

}

hNext = m_treeGetNextItem(hChildItem,TVGN_NEXT);

hChildItem = hNext;

}

}

首先SetCapture,WindowFromPoint()找到那个窗口的句柄,然后枚举窗口子控件EnumChildWindows。得到控件类名是不是syslistview32,得到控件句柄就好说了。

这个过程比较复杂。很多我自己没实际 *** 作过。。。

相信对于SPY++的模拟,网上有很多开源,这只是个思路。

hook没有写到,你要做的是一个鼠标钩子,网上很多资料,我这里只是把树形控件的某个节点的子节点列出的方法。

你说的那个啥

wcHandle

压根就是一个窗口句柄

而SetWindowsHookEx

的第三个参数是

模块句柄

也就是说

你回调函数所在的模块的一个句柄

然后

最后一个参数是

进程id

和你需要进程绑定就可以了

局部钩子

没有写过

一般都写全局

HHOOK

SetWindowsHookEx(int

idHook,

//

钩子的类型,即它处理的消息类型HOOKPROC

lpfn,

//

钩子子程的地址指针。如果dwThreadId参数为0//

或是一个由别的进程创建的线程的标识,//

lpfn必须指向DLL中的钩子子程。//

除此以外,lpfn可以指向当前进程的一段钩子子程代码。// 钩子函数的入口地址,当钩子钩到任何消息后便调用这个函数。HINSTANCE

hMod,

//

应用程序实例的句柄。标识包含lpfn所指的子程的DLL。//

如果dwThreadId

标识当前进程创建的一个线程,//

而且子程代码位于当前进程,hMod必须为NULL。//

可以很简单的设定其为本应用程序的实例句柄。DWORD

dwThreadId

//

与安装的钩子子程相关联的线程的标识符。//

如果为0,钩子子程与所有的线程关联,即为全局钩子。);

百科过来的

函数说明

1钩子到底是什么意思呢?为啥按Ctrl+Alt+Z就把QQ主面板d出来?这就是用到了键盘钩子,他能截获系统的键盘事件。

而键盘钩子就可以在用户按Ctrl+Alt+Z的时候截取到这个按键消息。

然后对这个消息进行处理,比如说QQ的处理就是把他的主面板d出来。

2C#键盘鼠标勾子的例程:

using System;

using SystemRuntimeInteropServices;

using SystemReflection;

using SystemThreading;

using SystemWindowsForms;

using SystemDiagnostics;

using SystemCollectionsGeneric;

namespace ConsoleApplicationTest

{

    /// <summary> 

    /// 这个类可以让你得到一个在运行中程序的所有鼠标和键盘事件 (系统钩子)

    /// 并且引发一个带KeyEventArgs和MouseEventArgs参数的NET事件以便你很容易使用这些信息

    /// 鼠标钩子处理函数:MouseHookProc

    /// 键盘钩子处理函数:KeyboardHookProc

    /// 使用方法:

    ///   1新建该类。

    ///   2声明OnMouseActivity、OnKeyDownEvent、OnKeyUpEvent、OnKeyPressEvent

    ///   3使用时调用相应的事件即可。

    ///   4要添加应用请在MouseHookProc()和KeyboardHookProc()中自己添加。

    /// 使用示例:

    /// public mkhook = new KeyBordHook();

    /// mkhookOnKeyDownEvent += new KeyEventHandler(OnKeyDownEventHandler);

    /// mkhookOnMouseActivity += new MouseEventHandler(OnMouseEventHandler);

    /// private void OnKeyDownEventHandler(object sender, KeyEventArgs e)

    /// {

    ///     ConsoleWriteLine("KeyDown");

    /// }

    /// private void OnMouseEventHandler(object sender, MouseEventArgs e)

    /// {

    ///     ConsoleWriteLine("MouseEvent");

    /// }

    /// </summary> 

    /// <remarks> 

    /// 修改:xyan nay6@163com

    /// 修改时间:100611 

    /// </remarks> 

    public class KeyBordHook : IDisposable

    {

        private const int WM_KEYDOWN = 0x100;

        private const int WM_KEYUP = 0x101;

        private const int WM_SYSKEYDOWN = 0x104;

        private const int WM_SYSKEYUP = 0x105;

        //全局的事件 

        public event MouseEventHandler OnMouseActivity;//鼠标事件

        public event KeyEventHandler OnKeyDownEvent;//键按下

        public event KeyEventHandler OnKeyUpEvent;//键放下

        public event KeyPressEventHandler OnKeyPressEvent;//键按下

        static int hMouseHook = 0;   //鼠标钩子句柄

        static int hKeyboardHook = 0; //键盘钩子句柄

        //鼠标常量 

        public const int WH_MOUSE = 7;

        public const int WH_MOUSE_LL = 14; //鼠标常量 

        public const int WH_KEYBOARD_LL = 13; //键盘常量 

        HookProc MouseHookProcedure;   //声明鼠标钩子事件类型

        HookProc KeyboardHookProcedure; //声明键盘钩子事件类型

        //Declare   wrapper   managed   POINT   class 

        [StructLayout(LayoutKindSequential)]

        public class POINT

        {

            public int x;

            public int y;

        }

        //声明鼠标钩子的封送结构类型 

        [StructLayout(LayoutKindSequential)]

        public class MouseHookStruct

        {

            public POINT pt;

            public int hwnd;

            public int wHitTestCode;

            public int dwExtraInfo;

        }

        //声明键盘钩子的封送结构类型 

        [StructLayout(LayoutKindSequential)]

        public class KeyboardHookStruct

        {

            public int vkCode; //表示一个在1到254间的虚似键盘码 

            public int scanCode; //表示硬件扫描码 

            public int flags;

            public int time;

            public int dwExtraInfo;

        }

        //装置钩子的函数 

        [DllImport("user32dll", CharSet = CharSetAuto, CallingConvention = CallingConventionStdCall)]

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

        //卸下钩子的函数 

        [DllImport("user32dll", CharSet = CharSetAuto, CallingConvention = CallingConventionStdCall)]

        public static extern bool UnhookWindowsHookEx(int idHook);

        //下一个钩挂的函数 

        [DllImport("user32dll", CharSet = CharSetAuto, CallingConvention = CallingConventionStdCall)]

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

        [DllImport("user32")]

        public static extern int ToAscii(int uVirtKey, int uScanCode, byte[] lpbKeyState, byte[] lpwTransKey, int fuState);

        [DllImport("user32")]

        public static extern int GetKeyboardState(byte[] pbKeyState);

        [DllImport("kernel32dll", CharSet = CharSetAuto,

            CallingConvention = CallingConventionStdCall)]

        private static extern IntPtr GetModuleHandle(string lpModuleName);

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

        //先前按下的键

        public List<Keys> preKeys = new List<Keys>();

        /// <summary> 

        /// 墨认的构造函数构造当前类的实例并自动的运行起来 

        /// </summary> 

        public KeyBordHook()

        {

            Start();

        }

        //析构函数 

        ~KeyBordHook()

        {

            Stop();

        }

        public void Dispose()

        {

            Stop();

        }

        public void Start()

        {

            //安装键盘钩子 

            if (hKeyboardHook == 0)

            {

                KeyboardHookProcedure = new HookProc(KeyboardHookProc);

                //hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, MarshalGetHINSTANCE(AssemblyGetExecutingAssembly()GetModules()[0]), 0);

                Process curProcess = ProcessGetCurrentProcess();

                ProcessModule curModule = curProcessMainModule;

                hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL,

                    KeyboardHookProcedure,

                    GetModuleHandle(curModuleModuleName),

                    0);

                if (hKeyboardHook == 0)

                {

                    Stop();

                    ConsoleWriteLine("SetWindowsHookEx ist failed");

                    throw new Exception("SetWindowsHookEx ist failed");

                }

            }

            //   install   Mouse   hook   

            if (hMouseHook == 0)

            {

                //   Create   an   instance   of   HookProc 

                MouseHookProcedure = new HookProc(thisMouseHookProc);

                Process curProcess = ProcessGetCurrentProcess();

                ProcessModule curModule = curProcessMainModule;

                //curProcessThreads[0]Id;

                hMouseHook = SetWindowsHookEx(WH_MOUSE_LL,

                    MouseHookProcedure,

                    GetModuleHandle(curModuleModuleName),

                    0);//curProcessId//ProcessGetCurrentProcess()Id

                //If   SetWindowsHookEx   fails 

                if (hMouseHook == 0)

                {

                    Stop();

                    ConsoleWriteLine("SetWindowsHookEx   failed ");

                    throw new Exception("SetWindowsHookEx   failed ");

                }

            }

        }

        public void Stop()

        {

            bool retMouse = true;

            bool retKeyboard = true;

            if (hMouseHook != 0)

            {

                retMouse = UnhookWindowsHookEx(hMouseHook);

                hMouseHook = 0;

            }

            if (hKeyboardHook != 0)

            {

                retKeyboard = UnhookWindowsHookEx(hKeyboardHook);

                hKeyboardHook = 0;

            }

            //如果卸下钩子失败 

            if (!(retKeyboard))

            {

                ConsoleWriteLine("UnhookWindowsHookEx failed");

                throw new Exception("UnhookWindowsHookEx failed");

            }

        }

        private const int WM_MOUSEMOVE = 0x200;

        private const int WM_LBUTTONDOWN = 0x201;

        private const int WM_RBUTTONDOWN = 0x204;

        private const int WM_MBUTTONDOWN = 0x207;

        private const int WM_LBUTTONUP = 0x202;

        private const int WM_RBUTTONUP = 0x205;

        private const int WM_MBUTTONUP = 0x208;

        private const int WM_LBUTTONDBLCLK = 0x203;

        private const int WM_RBUTTONDBLCLK = 0x206;

        private const int WM_MBUTTONDBLCLK = 0x209;

        private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)

        {

            //   if   ok   and   someone   listens   to   our   events 

            if ((nCode >= 0) && (OnMouseActivity != null))

            {

                MouseButtons button = MouseButtonsNone;

                switch (wParam)

                {

                    case WM_LBUTTONDOWN://鼠标左键

                    case WM_LBUTTONUP:

                    case WM_LBUTTONDBLCLK:

                        button = MouseButtonsLeft;

                        break;

                    case WM_RBUTTONDOWN://鼠标右键

                    case WM_RBUTTONUP:

                    case WM_RBUTTONDBLCLK:

                        button = MouseButtonsRight;

                        break;

                    //case WM_MBUTTONDOWN:

                    //    //case   WM_MBUTTONUP:   

                    //    //case   WM_MBUTTONDBLCLK:   

                    //    button = MouseButtonsMiddle;

                    //    break;

                    default:

                        break;

                }

                int clickCount = 0;

                if (button != MouseButtonsNone)

                {

                    if (wParam == WM_LBUTTONDBLCLK || wParam == WM_RBUTTONDBLCLK || wParam == WM_MBUTTONDBLCLK)

                    {

                        clickCount = 2;

                    }

                    else

                    {

                        clickCount = 1;

                    }

                }

                //}

                //ConsoleWriteLine(clickCountToString());

                //Marshall   the   data   from   callback 

                MouseHookStruct MyMouseHookStruct = (MouseHookStruct)MarshalPtrToStructure(lParam, typeof(MouseHookStruct));

                MouseEventArgs e = new MouseEventArgs(

                button,

                clickCount,

                MyMouseHookStructptx,

                MyMouseHookStructpty,

                0);

                OnMouseActivity(this, e);

            }

            return CallNextHookEx(hMouseHook, nCode, wParam, lParam);

        }

    }

}

以上就是关于得物钩子什么用全部的内容,包括:得物钩子什么用、C++怎样简单实现全局钩子或者键盘监控、hook技术需要了解windows消息机制吗等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9827245.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存