Reputation: 528
I have a program not written by me (closed source) and I need to read the text from what looks like a multi-line text edit.
The program has a TabControl at the highest level with 15 tabs. Within the 15th tab, there is the text edit that I need to get to.
AutomationElement aeEntireApplication = AutomationElement.FromHandle(hwd);
AutomationElementCollection aeEditCollection = aeEntireApplication.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty,"Edit"));
foreach (AutomationElement aeEdit in aeEditCollection )
{
object patternObj;
if (edit.TryGetCurrentPattern(TextPattern.Pattern, out patternObj))
{
var textPattern = (TextPattern)patternObj;
Console.WriteLine(textPattern.DocumentRange.GetText(-1).TrimEnd('\r')); // often there is an extra '\r' hanging off the end.
}
}
With this code, it only will print the contents of the Text Edits for the tab I am currently on. Is it possible to get to the contents of tab #15 without having to have that tab open?
Upvotes: 1
Views: 2026
Reputation: 574
It's always worth pointing the Inspect SDK tool at your UI, and checking what's being exposed through the UIA Raw view. The Raw view contains everything being exposed through UIA for the UI. (The view being shown in Inspect can be selected from Inspect's Options menu.) If Inspect doesn't show you the UI that you're interested in, then that UI isn't being exposed by the app (or the UI framework the app's using), and your own UIA client code won't be able to access it either.
I just created a test WinForm app with a TabControl. The TabControl has two tabs, and a TextBox for each tab. Inspect shows me that at any given time, only the UI contained in the active tab page is being exposed through UIA. So you won't be able to use UIA to access the UI on the other tabs.
Thanks,
Guy
Upvotes: 1
Reputation: 528
One solution that works it to use
internal static extern bool EnumChildWindows(IntPtr hwnd, WindowEnumProc func, IntPtr lParam);
I enumerate through all children of the entire program, then find a button next to the edit which has a unique name. Then I use
uint GW_HWNDPREV = 3;
logTextBoxHandle = GetWindow(hwnd, GW_HWNDPREV);
This gets me the handle to the Text Edit I need.
Not elegant, but it seems to work.
Upvotes: 0