Obtenha Detalhes de Atribuos de Texto Misto Usando Automação de Interface de Usuário
Observação
Esta documentação destina.Os desenvolvedores do NET Framework que desejam usar o gerenciado UI Automation classes definidas na System.Windows.Automation namespace.As informações mais recentes sobre UI Automation, consulte API de automação do Windows: Automação da interface do usuário.
Este tópico mostra como usar Microsoft UI Automation para obter detalhes de atributo de texto de um intervalo de texto que se estende por vários valores de atributo. Um intervalo de texto pode corresponder ao local atual do cursor (ou da seleção de degenerate) dentro de um documento, uma seleção contígua de texto, uma coleção de seleções de texto separado ou todo conteúdo textual de um documento.
Exemplo
The following code example demonstrates how to obtain the FontNameAttribute from a text range where GetAttributeValue returns a MixedAttributeValue object.
'--------------------------------------------------------------------
' <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();
}
The TextPattern control pattern, in tandem with the TextPatternRange class, supports basic text attributes, properties, and methods. For control-specific functionality that is not supported by TextPattern or TextPatternRange, the AutomationElement class provides methods for a UI Automation client to access the corresponding native object model.
Consulte também
Tarefas
Acrescentar Conteúdo a um Text Box Utilizando Automação de IU
Encontre e Destaque Texto Utilizando Automação de Interface de Usuário
Obtain Text Attributes Using UI Automation
Conceitos
UI Automation TextPattern Overview
Visão Geral de Padrões de Controle de Automação de Interface de Usuário