我从很久以前挖掘了旧的尘土飞扬的 Delphi 代码,并翻译成 c# :
将枚举所有子窗口的类:
public static class WindowsEnumerator
{
private static List<IntPtr> childs = new List<IntPtr>();
public static List<WindowInfo> EnumerateChildWindows(IntPtr parentHandle)
{
var result = new List<WindowInfo>();
childs.Clear();
EnumChildWindows(parentHandle, Enumerator, IntPtr.Zero);
foreach (IntPtr handle in childs)
{
result.Add(new WindowInfo(handle));
}
return result;
}
private static bool Enumerator(IntPtr hwnd, IntPtr lparam)
{
childs.Add(hwnd);
return true;
}
internal delegate bool WindowEnumProc(IntPtr hwnd, IntPtr lparam);
[DllImport("user32.dll")]
internal static extern bool EnumChildWindows(IntPtr hwnd, WindowEnumProc func, IntPtr lParam);
}
public class WindowInfo
{
public IntPtr Handle { get; private set; }
public string Text { get; private set; }
public string ClassName { get; private set; }
public WindowInfo(IntPtr hwnd)
{
this.Handle = hwnd;
var sb = new StringBuilder(1024);
var txtLen = GetWindowText(this.Handle, sb, sb.Capacity);
if (txtLen > 0)
this.Text = sb.ToString();
else
this.Text = "";
var sbc = new StringBuilder(256);
var clsLen = GetClassName(this.Handle, sbc, sbc.Capacity);
if (clsLen > 0)
this.ClassName = sbc.ToString();
else
this.ClassName = "";
}
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
}
然后你只需要找到一个带有静态方法 WindowsEnumerator.EnumerateChildWindows 的按钮,WinForms 按钮在 ClassName 中有一个“BUTTON”字符串,让我们假设你的按钮被命名为 button1(并希望你没有使用这样的名称):
private void button1_Click(object sender, EventArgs e)
{
var exeProcess = Process.Start(@"w:\kanta\temp\ExeWithButtonInsideGroupBox.exe");
while (exeProcess.MainWindowHandle.ToInt32() == 0)
{
Thread.Sleep(10);
};
var windows = WindowsEnumerator.EnumerateChildWindows(exeProcess.MainWindowHandle);
exeProcess.Kill();
bool myButtonExists = windows.FirstOrDefault(wnd => wnd.ClassName.Contains("BUTTON") && wnd.Text == "button1") != null;
MessageBox.Show(myButtonExists.ToString());
}