【问题标题】:Detect which childwindow of the foreground window has been clicked?检测点击了前台窗口的哪个子窗口?
【发布时间】:2012-04-10 05:00:49
【问题描述】:

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

【问题讨论】:

  • 你为什么要搞乱另一个应用程序的窗口?如果这是为了测试自动化,您可以使用 System.Windows.Automation 命名空间。
  • 这不是测试自动化....解释我的应用程序打算做什么需要很多时间,但我会快速向您简要介绍,我必须预先填充子窗口列表在活动应用程序中,跟踪用户的动作(点击)并指导他下一步必须做什么,通过参考使用 EnumChildWindows 进行的查找,因此我需要确切知道哪个子窗口具有焦点。您能否给我一些关于如何使用 System.Windows.Automation 来完成它的指示......在此先感谢!
  • 哦,这是用于基于计算机的培训。 System.Windows.Automation 可能仍然有用。您可以阅读文档以了解最新情况;我不会试图在评论中教你。
  • 好的,谢谢,我马上就开始了

标签: c# winapi childwindow


【解决方案1】:
      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);
    }

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

【讨论】:

  • 一段很棒的代码。它有助于通过邮寄或发送消息发送消息,并让它们在任何情况下都能正常工作。我只是将它转换为 bool 并返回前景窗口是否为子窗口。谢谢。
猜你喜欢
  • 2010-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-16
  • 1970-01-01
  • 2012-09-19
  • 2017-08-19
相关资源
最近更新 更多