检测前台窗口中哪个子窗口被点击了?

3

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


你为什么要干扰另一个应用程序的窗口?如果这是为了测试自动化,你可以使用System.Windows.Automation命名空间。 - Raymond Chen
这不是测试自动化...解释我的应用程序意图需要很多时间,但我会快速简要地介绍一下。我必须预填活动应用程序中存在的子窗口列表,跟踪用户的移动(点击),并引导他下一步该做什么,通过使用EnumChildWindows进行查找,因此我需要确切知道哪个子窗口具有焦点。您能否请给我一些指针,告诉我如何使用System.Windows.Automation完成它...提前致谢! - Ajit
哦,这是针对基于计算机的培训的。System.Windows.Automation 仍然可能有用。您可以阅读文档以快速了解;我不会在评论中尝试教您。 - Raymond Chen
好的,谢谢。我马上开始做。 - Ajit
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);
    }

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

很棒的代码。它帮助我们通过邮件或发送消息来发送信息,并在任何情况下都能正常工作。我只是将它转换为布尔值,以确定前台窗口是否为子窗口。谢谢。 - W.M.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接