다음을 통해 공유


UI 자동화를 사용하여 혼합 텍스트 특성 정보 가져오기

참고참고

이 문서는 System.Windows.Automation 네임스페이스에 정의된 관리되는 UI Automation 클래스를 사용하려는 .NET Framework 개발자를 위해 작성되었습니다.UI Automation에 대한 최신 정보는 Windows Automation API: UI Automation을 참조하십시오.

이 항목에서는 Microsoft UI Automation을 사용하여 여러 특성 값에 걸쳐 있는 텍스트 범위에서 텍스트 특성 세부 정보를 가져오는 방법을 보여 줍니다. 텍스트 범위는 문서 전체 텍스트 내용, 연결되지 않은 텍스트 선택 영역 모음, 연속적인 텍스트 선택 영역 또는 문서 내 캐럿이나 디제너레이트 선택 영역의 현재 위치에 해당될 수 있습니다.

예제

다음 코드 예제에서는 GetAttributeValueMixedAttributeValue 개체를 반환하는 텍스트 범위에서 FontNameAttribute를 가져오는 방법을 보여 줍니다.

'--------------------------------------------------------------------
' <summary>
' Display the target selection with attribute details in client.
' </summary>
' <param name="selectedText">The current target selection.</param>
'--------------------------------------------------------------------
Private Sub DisplaySelectedTextWithAttributes(ByVal selectedText As String)
    targetSelection.Text = selectedText
    ' We're only interested in the FontNameAttribute for the purposes 
    ' of this sample.
    targetSelectionAttributes.Text = _
        ParseTextRangeByAttribute( _
        selectedText, TextPattern.FontNameAttribute)
End Sub

'--------------------------------------------------------------------
' <summary>
' Parse the target selection based on the text attribute of interest.
' </summary>
' <param name="selectedText">The current target selection.</param>
' <param name="automationTextAttribute">
' The text attribute of interest.
' </param>
' <returns>
' A string representing the requested attribute details.
' </returns>
'--------------------------------------------------------------------
Function ParseTextRangeByAttribute( _
ByVal selectedText As String, _
ByVal automationTextAttribute As AutomationTextAttribute) As String
    Dim attributeDetails As StringBuilder = New StringBuilder()
    ' Initialize the current attribute value.
    Dim attributeValue As String = ""
    ' Make a copy of the text range.
    Dim searchRangeClone As TextPatternRange = searchRange.Clone()
    ' Collapse the range to the starting endpoint.
    searchRangeClone.Move(TextUnit.Character, -1)
    ' Iterate through the range character by character.
    Dim x As Integer
    For x = 1 To selectedText.Length
        searchRangeClone.Move(TextUnit.Character, 1)
        ' Get the attribute value of the current character.
        Dim newAttributeValue As String = _
            searchRangeClone.GetAttributeValue(automationTextAttribute).ToString()
        ' If the new attribute value is not equal to the old then report 
        ' the new value along with its ___location within the range.
        If (newAttributeValue <> attributeValue) Then
            attributeDetails.Append(automationTextAttribute.ProgrammaticName) _
            .Append(":") _
            .Append(vbLf) _
            .Append("<") _
            .Append(newAttributeValue) _
            .Append("> at text range position ") _
            .AppendLine(x.ToString())
            attributeValue = newAttributeValue
        End If
    Next
    Return attributeDetails.ToString()
End Function
///--------------------------------------------------------------------
/// <summary>
/// Display the target selection with attribute details in client.
/// </summary>
/// <param name="selectedText">The current target selection.</param>
///--------------------------------------------------------------------
private void DisplaySelectedTextWithAttributes(string selectedText)
{
    targetSelection.Text = selectedText;
    // We're only interested in the FontNameAttribute for the purposes 
    // of this sample.
    targetSelectionAttributes.Text = 
        ParseTextRangeByAttribute(
        selectedText, TextPattern.FontNameAttribute);
}

///--------------------------------------------------------------------
/// <summary>
/// Parse the target selection based on the text attribute of interest.
/// </summary>
/// <param name="selectedText">The current target selection.</param>
/// <param name="automationTextAttribute">
/// The text attribute of interest.
/// </param>
/// <returns>
/// A string representing the requested attribute details.
/// </returns>
///--------------------------------------------------------------------
private string ParseTextRangeByAttribute(
    string selectedText, 
    AutomationTextAttribute automationTextAttribute)
{
    StringBuilder attributeDetails = new StringBuilder();
    // Initialize the current attribute value.
    string attributeValue = "";
    // Make a copy of the text range.
    TextPatternRange searchRangeClone = searchRange.Clone();
    // Collapse the range to the starting endpoint.
    searchRangeClone.Move(TextUnit.Character, -1);
    // Iterate through the range character by character.
    for (int x = 1; x <= selectedText.Length; x++)
    {
        searchRangeClone.Move(TextUnit.Character, 1);
        // Get the attribute value of the current character.
        string newAttributeValue = 
            searchRangeClone.GetAttributeValue(automationTextAttribute).ToString();
        // If the new attribute value is not equal to the old then report 
        // the new value along with its ___location within the range.
        if (newAttributeValue != attributeValue)
        {
            attributeDetails.Append(automationTextAttribute.ProgrammaticName)
                .Append(":\n<")
                .Append(newAttributeValue)
                .Append("> at text range position ")
                .AppendLine(x.ToString());
            attributeValue = newAttributeValue;
        }
    }
    return attributeDetails.ToString();
}

TextPatternRange 클래스와 함께 사용할 경우 TextPattern 컨트롤 패턴은 기본 텍스트 특성, 속성 및 메서드를 지원합니다. TextPattern 또는 TextPatternRange에서 지원하지 않는 컨트롤별 기능의 경우 UI 자동화 클라이언트가 해당 기본 개체 모델에 액세스하는 데 필요한 메서드를 AutomationElement 클래스에서 제공합니다.

참고 항목

작업

UI 자동화를 사용하여 텍스트 상자에 콘텐츠 추가

UI 자동화를 사용하여 텍스트 찾기 및 강조

UI 자동화를 사용하여 텍스트 특성 가져오기

개념

UI 자동화 TextPattern 개요

UI 자동화 컨트롤 패턴 개요

클라이언트용 UI 자동화 컨트롤 패턴