如何使用EnumWindows的查找Windows与特定的标题/标题? [英] How can I use EnumWindows to find windows with a specific caption/title?

查看:829
本文介绍了如何使用EnumWindows的查找Windows与特定的标题/标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个应用程序,最终将驱动UI试验WPF应用程序的API。

I am working on an application that will eventually be an api for driving UI Tests for a WPF application.

目前,我们正在对初步测试的一点,我们得到2 Windows安全弹出。
我们有一些code的循环10次,它就会使用FindWindowByCaption方法的弹出窗口之一的把手,进入信息,并点击确定。

At one point of the initial test we are working on, we get 2 Windows security popups. We have some code that loops 10 times, it gets the handle of one of the popups using the FindWindowByCaption method and enters the information and clicks ok.

9次了10这个工作得很好,但我们偶尔会看到什么看起来是一个竞争状态。我怀疑是在循环开始时只有一个窗口是开放的,而它的输入信息,第二个打开和抢断重点;在此之后,它只是无限期挂起。

9 times out of 10 this works just fine, however we are occasionally seeing what looks to be a race condition. My suspicion is that the loop starts when only one of the windows is open and while its entering the information the second one opens and steals focus; after this it just hangs indefinitely.

我不知道是否有任何方法来获取所有窗口的句柄对于给定的标题,这样我们就可以等到有开始循环之前是2。

What I'm wondering is if there is any method to get all of the window handles for a given caption, so that we can wait until there are 2 before starting the loop.

推荐答案

使用 EnumWindows的,并通过所有的窗户枚举,使用 GetWindowText时来获得每个窗口的文本,但此后其过滤你想要的。

Original Answer

Use EnumWindows and enumerate through all the windows, using GetWindowText to get each window's text, then filter it however you want.

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount);

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern int GetWindowTextLength(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);

// Delegate to filter which windows to include 
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

/// <summary> Get the text for the window pointed to by hWnd </summary>
public static string GetWindowText(IntPtr hWnd)
{
    int size = GetWindowTextLength(hWnd);
    if (size > 0)
    {
        var builder = new StringBuilder(size + 1);
        GetWindowText(hWnd, builder, builder.Capacity);
        return builder.ToString();
    }

    return String.Empty;
}

/// <summary> Find all windows that match the given filter </summary>
/// <param name="filter"> A delegate that returns true for windows
///    that should be returned and false for windows that should
///    not be returned </param>
public static IEnumerable<IntPtr> FindWindows(EnumWindowsProc filter)
{
  IntPtr found = IntPtr.Zero;
  List<IntPtr> windows = new List<IntPtr>();

  EnumWindows(delegate(IntPtr wnd, IntPtr param)
  {
      if (filter(wnd, param))
      {
          // only add the windows that pass the filter
          windows.Add(wnd);
      }

      // but return true here so that we iterate all windows
      return true;
  }, IntPtr.Zero);

  return windows;
}

/// <summary> Find all windows that contain the given title text </summary>
/// <param name="titleText"> The text that the window title must contain. </param>
public static IEnumerable<IntPtr> FindWindowsWithText(string titleText)
{
    return FindWindows(delegate(IntPtr wnd, IntPtr param)
    {
        return GetWindowText(wnd).Contains(titleText);
    });
} 

例如,让所有用记事本窗口的标题:

For example, to get all of the windows with "Notepad" in the title:

var windows = FindWindowsWithText("Notepad");

Win32Interop.WinHandles

这个答案足以证明受欢迎,我创建了一个开放源码软件项目, Win32Interop.WinHandles 以提供超过IntPtrs为Win32的抽象视窗。利用图书馆,把所有包含在标题为记事本的窗口:

Win32Interop.WinHandles

This answer proved popular enough that I created an OSS project, Win32Interop.WinHandles to provide an abstraction over IntPtrs for win32 windows. Using the library, to get all of the windows that contains "Notepad" in the title:

var allNotepadWindows
   = TopLevelWindowUtils.FindWindows(wh => wh.GetWindowText().Contains("Notepad"));

这篇关于如何使用EnumWindows的查找Windows与特定的标题/标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆