title: How to get accessibility info of child contents in top active window C# WPF
body: <p>I am able to get top active window (title) in my code but I wanted to know how to get all the children(content and controls or elements -image, link, button etc) inside the active top window in my code.
  I searched many links such as - <a href="https://stackoverflow.com/questions/3211408/retrieve-all-the-controls-of-any-window-with-their-types-and-value">Retrieve all the controls of any window with their types and value</a>  which suggests- To enumerate top level windows use EnumWindows(), to get their child windows use EnumChildWindows().
  But I am not able to understand how to implement it in my code and get the children of top active windows which is focused.</p>
  <p>It would be great if someone helped me around with some sample code. Stuck from long time exploring and checking different links.</p>
  <p>Below is the code which gives me top active window title and displaying in the grid.</p>
  <p>xaml code -</p>
  <pre><code> &lt;Grid x:Name=&quot;grid&quot;&gt;
  &lt;/Grid&gt;
  </code></pre>
  <p>.cs Class code -</p>
  <pre><code> public partial class MainWindow : Window
      {
  
       [DllImport(&quot;user32.dll&quot;)]
              static extern IntPtr GetForegroundWindow();
              private static IntPtr GetActiveWindow()
              {
                  IntPtr handle = IntPtr.Zero;
                  return GetForegroundWindow();
              }
              [DllImport(&quot;user32.dll&quot;, EntryPoint = &quot;GetWindowTextW&quot;, CharSet = CharSet.Unicode)]
              private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int maxLength);
              ListBox lst = new ListBox();
              public MainWindow()
              {
                  InitializeComponent();
      
                  DispatcherTimer dispatcherTimer = new DispatcherTimer();
                  dispatcherTimer.Tick += new EventHandler(DispatcherTimer_Tick);
                  dispatcherTimer.Interval = new TimeSpan(0, 0, 5);
                  dispatcherTimer.Start();
                  var d = ActiveWindowTitle();
                  var t = GetForegroundWindow();
                  grid.Children.Add(lst);
              }
              public static IntPtr GetTopWindow(string wClass, string wCaption)
              {
                  if (string.IsNullOrEmpty(string.Concat(wClass, wCaption)))
                  {
                      return (IntPtr)0;
                  }
                  return (IntPtr)0;
              }
              private void DispatcherTimer_Tick(object sender, EventArgs e)
              {
                  string title = ActiveWindowTitle();
                  var current = this.Title;
                  if (!current.Equals(title) &amp;&amp; !String.IsNullOrEmpty(title))
                      lst.Items.Add(&quot;Title: &quot; + title );
              }
      
              private string ActiveWindowTitle()
              {
                  const int nChar = 256;
                  StringBuilder ss = new StringBuilder(nChar);
                  IntPtr handle = IntPtr.Zero;
                  handle = GetForegroundWindow();
      
                  if (GetWindowText(handle, ss, nChar) &gt; 0)
                      return ss.ToString();
                  else return &quot;&quot;;
      
              }
  }
  </code></pre>