C# 检测外来形式的按钮点击

C# 检测外来形式的按钮点击,c#,winapi,hook,C#,Winapi,Hook,是否可以在外部窗口中检测到按钮单击? 我尝试获取消息,但我的程序冻结了,什么也没有得到。 我已经知道窗口和按钮手柄了 它卡在了while循环中 private static void Main(string[] args) { IntPtr handle = ReturnHandle("Form1"); IntPtr buttonHandle = ReturnHandleEx(handle, "Login");

是否可以在外部窗口中检测到按钮单击?
我尝试获取消息,但我的程序冻结了,什么也没有得到。
我已经知道窗口和按钮手柄了 它卡在了while循环中

private static void Main(string[] args)
        {
            IntPtr handle = ReturnHandle("Form1");
            IntPtr buttonHandle = ReturnHandleEx(handle, "Login");
            Console.Out.WriteLine("handle = {0}", handle.ToString("x2"));
            Console.Out.WriteLine("buttonHandle = {0}", buttonHandle.ToString("x2"));
            MSG msg;
            sbyte ret;
            while ((ret = GetMessage(out msg, buttonHandle, 0, 0)) != -1)
            {
                if (ret == -1)
                {
                    //-1 indicates an error
                }
                else
                {
                    TranslateMessage(ref msg);
                    DispatchMessage(ref msg);
                }
                Console.Out.WriteLine("msg = {0}", msg);
            }

        }

        private static IntPtr ReturnHandle(string lpWindowName)
        {
            return FindWindow(null, lpWindowName);
        }

        private static IntPtr ReturnHandleEx(IntPtr parentHandle, string windowTitle)
        {
            return FindWindowEx(parentHandle, new IntPtr(), null, windowTitle);
        }

您无法在另一进程中从窗口接收消息


要做到这一点,您需要将代码注入到这个过程中。最简单的方法是使用。这个钩子需要一个单独的DLL。当您注册一个钩子时,这个DLL将被注入到所有正在运行的进程中。在该DLL中,您需要侦听窗口消息,过滤它们,并通过某种进程间通信(共享内存、管道、网络或其他)将感兴趣的消息发送到应用程序.

显示代码并指定程序所在的行stuck@AndyT它卡在循环中,我可以尝试接收消息,为什么你们不能在另一个表单上处理事件呢?此代码毫无意义。@CodyGray此窗口是应用程序操作中的另一个窗口。请指定“外部窗口”和“应用程序中的另一个窗口”的含义。您是指同一应用程序/进程中的另一个窗口还是远程进程中的窗口(另一个应用程序)?请注意,您不能用托管语言编写这种类型的挂钩DLL。托管DLL中支持的唯一挂钩类型是不被注入的挂钩,即
WH\u KEYBOARD\u LL
WH\u MOUSE\u LL
。无需挂钩和破解。提供所需的所有设施。您只需为按钮的默认操作注册一个自动化事件处理程序,并实现代码以响应它。