0

我需要从其他应用程序获取控件,但只需要文本框控件。

我该怎么做?

我现在有这个代码:

public static List<IntPtr> FindWindowControl(IntPtr hWnd)
{
    List<IntPtr> result = new List<IntPtr>();
    GCHandle listHandle = GCHandle.Alloc(result);

    try
    {
        Helpers.WinApi.EnumWindowProc childProc = new Helpers.WinApi.EnumWindowProc(EnumWindow);
        Helpers.WinApi.EnumChildWindows(hWnd, childProc, GCHandle.ToIntPtr(listHandle));
    }
    finally
    {
        if (listHandle.IsAllocated)
            listHandle.Free();
    }
    return result;
}

private static bool EnumWindow(IntPtr handle, IntPtr pointer)
{
    GCHandle gch = GCHandle.FromIntPtr(pointer);
    List<IntPtr> list = gch.Target as List<IntPtr>;
    if (list == null)
        throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");

    string className = Helpers.WinApi.GetWinClass(handle);
    if (className.Equals("Edit") || className.Equals("RICHEDIT60W"))
    {
        list.Add(handle);
        return false;
    }
    return true;
}

问题是对于在 .NET 中创建的应用程序,TextBoxes 是使用其他类创建的,因此找不到它们。

使用 Spy++ 时,我看到文本框控件有一个名为:WindowsForms10.EDIT.app.0.141b42a_r14_ad1 的类。

这表明我不能使用类名,或者,我可以安全地在类名中查找单词 EDIT 吗?

4

0 回答 0