Partager via


Obtenir des détails d'attribut de texte mixte à l'aide d'UI Automation

RemarqueRemarque

Cette documentation s'adresse aux développeurs .NET Framework qui veulent utiliser les classes UI Automation managées définies dans l'espace de noms System.Windows.Automation.Pour obtenir les informations les plus récentes sur UI Automation, consultez API Windows Automation : UI Automation (page éventuellement en anglais).

Cette rubrique montre comment utiliser Microsoft UI Automation pour obtenir des détails d'attribut de texte depuis une plage de texte qui couvre plusieurs valeurs d'attribut. Une plage de texte peut correspondre à l'emplacement actuel du signe insertion (ou sélection dégénérée) dans un document, une sélection de texte contiguë, une collection de sélections de texte disjointes ou l'ensemble du contenu textuel d'un document.

Exemple

L'exemple de code suivant montre comment obtenir le FontNameAttribute depuis une plage de texte où GetAttributeValue retourne un objet MixedAttributeValue.

'--------------------------------------------------------------------
' <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();
}

Le TextPattern modèle de contrôle, en tandem avec la classe TextPatternRange, prend en charge les propriétés, les méthodes et les attributs du texte de base. Pour les fonctionnalités propres au contrôle qui ne sont pas prises en charge par TextPattern ou TextPatternRange, la classe AutomationElement fournit des méthodes permettant à un client UI Automation d'accéder au modèle objet natif correspondant.

Voir aussi

Tâches

Ajouter du contenu à une zone de texte à l'aide d'UI Automation

Rechercher et mettre un texte en surbrillance à l'aide d'UI Automation

Obtenir des attributs de texte à l'aide d'UI Automation

Concepts

Vue d'ensemble de TextPattern d'UI Automation

Vue d'ensemble des modèles de contrôle UI Automation

Modèles de contrôle UI Automation pour les clients