检测前景窗口的哪个子窗口已被点击?

时间:2012-04-10 05:00:49

标签: c# winapi childwindow

我的任务是找到前台窗口(使用GetForegroundWindow API完成)然后我必须预先填充一个包含前台窗口的所有子窗口的列表(使用EnumChildWindows API完成)。现在我需要检测鼠标光标在哪个子窗口上,即我需要找出哪个子窗口(可能是前景窗口中的按钮或文本框)是活动的。  是否有一些API用于我可以获得已被点击的ChildWindows的句柄? 即使我只得到焦点所在的ChildWindow(活动前景窗口)的名称,对我来说也足够了。  在此先感谢。

1 个答案:

答案 0 :(得分:3)

      InPtr hwnd = GetForegroundWindow();

      public static void GetAppActiveWindow(IntPtr hwnd)
    {
        uint remoteThreadId = GetWindowThreadProcessId(hwnd, IntPtr.Zero);
        uint currentThreadId = GetCurrentThreadId();
        //AttachTrheadInput is needed so we can get the handle of a focused window in another app
        AttachThreadInput(remoteThreadId, currentThreadId, true);
        //Get the handle of a focused window
        IntPtr focussed = GetFocus();

        StringBuilder activechild = new StringBuilder(256);
        GetWindowText(focussed, activechild, 256);
        string textchld = activechild.ToString();
        if (textchld.Length > 0)
        {
            Console.WriteLine("The active Child window is " + textchld + " .");

        }
        //Now detach since we got the focused handle
        AttachThreadInput(remoteThreadId, currentThreadId, false);
    }

这是最终解决了问题的原因:)