Export (0) Print
Expand All

TextPatternRange.GetChildren Method

Retrieves a collection of all embedded objects that fall within the text range.

Namespace:  System.Windows.Automation.Text
Assembly:  UIAutomationClient (in UIAutomationClient.dll)

public AutomationElement[] GetChildren()

Return Value

Type: System.Windows.Automation.AutomationElement[]
A collection of all child objects that fall within the range. Children that overlap with the range but are not entirely enclosed by it will also be included in the collection.
Returns an empty collection if there are no child objects.

/// ------------------------------------------------------------------- 
/// <summary> 
/// Starts the target application and returns the AutomationElement  
/// obtained from the targets window handle. 
/// </summary> 
/// <param name="exe">
/// The target application. 
/// </param> 
/// <param name="filename">
/// The text file to be opened in the target application 
/// </param> 
/// <returns> 
/// An AutomationElement representing the target application. 
/// </returns> 
/// ------------------------------------------------------------------- 
private AutomationElement StartTarget(string exe, string filename)
{
    // Start text editor and load with a text file.
    Process p = Process.Start(exe, filename);

    // targetApp --> the root AutomationElement.
    AutomationElement targetApp =
        AutomationElement.FromHandle(p.MainWindowHandle);

    return targetApp;
}


...


/// ------------------------------------------------------------------- 
/// <summary> 
/// Obtain the text control of interest from the target application. 
/// </summary> 
/// <param name="targetApp">
/// The target application. 
/// </param> 
/// <returns> 
/// An AutomationElement that represents a text provider.. 
/// </returns> 
/// ------------------------------------------------------------------- 
private AutomationElement GetTextElement(AutomationElement targetApp)
{
    // The control type we're looking for; in this case 'Document'
    PropertyCondition cond1 =
        new PropertyCondition(
        AutomationElement.ControlTypeProperty,
        ControlType.Document);

    // The control pattern of interest; in this case 'TextPattern'.
    PropertyCondition cond2 = 
        new PropertyCondition(
        AutomationElement.IsTextPatternAvailableProperty, 
        true);

    AndCondition textCondition = new AndCondition(cond1, cond2);

    AutomationElement targetTextElement =
        targetApp.FindFirst(TreeScope.Descendants, textCondition);

    // If targetText is null then a suitable text control was not found. 
    return targetTextElement;
}


...


/// ------------------------------------------------------------------- 
/// <summary> 
/// Retrieves the embedded children of a document control. 
/// </summary> 
/// <param name="targetTextElement">
/// The AutomationElment that represents a text control. 
/// </param> 
/// ------------------------------------------------------------------- 
private void GetEmbeddedObjects(AutomationElement targetTextElement)
{
    TextPattern textPattern = 
        targetTextElement.GetCurrentPattern(TextPattern.Pattern) 
        as TextPattern;

    if (textPattern == null)
    {
        // Target control doesn't support TextPattern. 
        return;
    }

    // Obtain a text range spanning the entire document.
    TextPatternRange textRange = textPattern.DocumentRange;

    // Retrieve the embedded objects within the range.
    AutomationElement[] embeddedObjects = textRange.GetChildren();

    foreach (AutomationElement embeddedObject in embeddedObjects)
    {
        Console.WriteLine(embeddedObject.Current.Name);
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft