Ok. there exists a way to get what you expected to get. try this method it works for me.
1. First Find your window using class name or title (i used messagebox title in my test)
2. For Finding your window pass set ParentHandle and ChildHandle both to Null(IntPtr.Zero) so it uses desktopwindow as the parent window and search through all the opened windows.
3. on getting the windowHandle from step 2 Enumerate through its child windows
4. Use the combination of GetWindowTextLength and GetWindowText to get your text.
sample code
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);
[DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool IsChild(IntPtr hWndParent, IntPtr hwnd);
private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetWindowTextLength(IntPtr hWnd);
Method definitions
public static List<intptr> GetChildWindows(IntPtr parent)
{
List<intptr> result = new List<intptr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
EnumWindowsProc childProc = new EnumWindowsProc(EnumWindow);
EnumChildWindows(parent, 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>;");
}
list.Add(handle);
return true;
}
private void button1_Click(object sender, EventArgs e)
{
IntPtr windowHandle = FindWindowEx(IntPtr.Zero, IntPtr.Zero, null, "Save As");
List<intptr> childs = GetChildWindows(windowHandle);
foreach (IntPtr child in childs)
{
bool isChild = IsChild(windowHandle, child);
int length = GetWindowTextLength(child);
StringBuilder bb = new StringBuilder(length+1);
GetWindowText(child, bb, bb.Capacity);
}
}</intptr></intptr></intptr></intptr></intptr></intptr></intptr>