title: Get TitleBar Caption of any application using Microsoft UI Automation?
body: <p>In <strong>C#</strong> or else <strong>VB.Net</strong>, how I could use <strong>Microsoft UI Automation</strong> to retrieve the text of any control that contains text?.</p>
  
  <p>I've been researching in the MSDN Docs, but I don't get it.</p>
  
  <p><strong><a href="https://msdn.microsoft.com/en-us/library/ms788751%28v=vs.110%29.aspx?cs-save-lang=1&amp;cs-lang=vb#code-snippet-1" rel="noreferrer">Obtain Text Attributes Using UI Automation</a></strong></p>
  
  <p>Then, for example, with the code below I'm trying to retrieve the text of the Window titlebar by giving the hwnd of that window, but I don't know exactlly how to follow the titlebar to find the child control (label?) that really contains the text.</p>
  
  <pre><code>Imports System.Windows.Automation
  Imports System.Windows.Automation.Text
  </code></pre>
  
  <p>.</p>
  
  <pre><code>Dim hwnd As IntPtr = Process.GetProcessesByName("notepad").First.MainWindowHandle
  
  Dim targetApp As AutomationElement = AutomationElement.FromHandle(hwnd)
  
  ' The control type we're looking for; in this case 'TitleBar' 
  Dim cond1 As New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TitleBar)
  
  Dim targetTextElement As AutomationElement =
      targetApp.FindFirst(TreeScope.Descendants, cond1)
  
  Debug.WriteLine(targetTextElement Is Nothing)
  </code></pre>
  
  <p>In the example above I'm trying with the titlebar, but just I would like to do it with any other control that contains text ...like a titlebar.</p>
  
  <p>PS: I'm aware of P/Invoking <code>GetWindowText</code> API.</p>