TextPattern.RangeFromChild(AutomationElement) 메서드

정의

이미지, 하이퍼링크, Microsoft Excel 스프레드시트 또는 기타 포함된 개체와 같은 자식 요소를 묶는 텍스트 범위를 검색합니다.

public System.Windows.Automation.Text.TextPatternRange RangeFromChild (System.Windows.Automation.AutomationElement childElement);

매개 변수

childElement
AutomationElement

둘러싸인 개체입니다.

반환

TextPatternRange

자식 요소가 모두 포함된 범위입니다.

예외

자식 요소가 null인 경우

요소가 텍스트 컨테이너의 자식이 아닌 경우

예제

/// -------------------------------------------------------------------
/// <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>
/// Obtains a text range spanning an embedded child 
/// of a document control and displays the content of the range.
/// </summary>
/// <param name="targetTextElement">
/// The AutomationElment that represents a text control.
/// </param>
/// -------------------------------------------------------------------
private void GetRangeFromChild(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();

    // Retrieve and display text value of embedded object.
    foreach (AutomationElement embeddedObject in embeddedObjects)
    {
        if ((bool)embeddedObject.GetCurrentPropertyValue(
            AutomationElement.IsTextPatternAvailableProperty))
        {
           // For full functionality a secondary TextPattern should
           // be obtained from the embedded object.
           // embeddedObject must be a child of the text provider.
            TextPatternRange embeddedObjectRange =
                textPattern.RangeFromChild(embeddedObject);
            // GetText(-1) retrieves all text in the range.
            // Typically a more limited amount of text would be 
            // retrieved for performance and security reasons.
            Console.WriteLine(embeddedObjectRange.GetText(-1));
        }
    }
}

설명

자식 요소가 있는 범위의 텍스트가 없는 경우 (비어 있음) 중복 제거 범위로 반환 됩니다.

childElement 매개 변수는의 자식 이거나를 AutomationElement 연관를 TextPattern 의 자식 배열를 TextPatternRange합니다.

적용 대상

추가 정보