3

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

4

1 回答 1

4
      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);
    }

这就是最终解决问题的方法:)

于 2012-04-11T12:06:08.810 回答