Die Methoden:
[cs][DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern int GetWindowText(IntPtr hWnd, [Out] StringBuilder lpString, int nMaxCount);
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
public static List<IntPtr> GetChildWindows(IntPtr parent)
{
List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
EnumWindowProc childProc = new EnumWindowProc(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;
}
const uint WM_GETTEXT = 0x0D;
const uint WM_GETTEXTLENGTH = 0x0E;
public static string GetControlText(IntPtr hWnd)
{
int length = SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0);
StringBuilder buffer = new StringBuilder(length + 1);
SendMessage(hWnd, 0x0d, buffer.Capacity, buffer);
return buffer.ToString();
}[/cs]
und der Aufruf:
[cs]List<IntPtr> childList = GetChildWindows(new IntPtr(Convert.ToInt32(textBox1.Text)));
textBox4.Text = GetControlText(childList[EnumID]);[/cs]