TextPatternRange.GetText(Int32) 方法

定义

返回文本范围的纯文本。

public:
 System::String ^ GetText(int maxLength);
public string GetText(int maxLength);
member this.GetText : int -> string
Public Function GetText (maxLength As Integer) As String

参数

maxLength
Int32

要返回的字符串的最大长度。 如果不需要限制,请使用 -1

返回

文本范围的纯文本,可能在指定的 maxLength位置截断。

例外

如果 maxLength 小于 -1。

示例

 private String TextFromSelection(AutomationElement target, Int32 length)
{
    // Specify the control type we're looking for, in this case 'Document'
    PropertyCondition cond = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document);

    // target --> The root AutomationElement.
    AutomationElement textProvider = target.FindFirst(TreeScope.Descendants, cond);

    TextPattern textpatternPattern = textProvider.GetCurrentPattern(TextPattern.Pattern) as TextPattern;

    if (textpatternPattern == null)
    {
        Console.WriteLine("Root element does not contain a descendant that supports TextPattern.");
        return null;
    }
    TextPatternRange[] currentSelection = textpatternPattern.GetSelection();

    // GetText(-1) retrieves all characters but can be inefficient
    return currentSelection[0].GetText(length);
}
Private Function TextFromSelection(ByVal target As AutomationElement, ByVal length As Int32) As String
    ' Specify the control type we're looking for, in this case 'Document'
    Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)

    ' target --> The root AutomationElement.
    Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)

    Dim textpatternPattern As TextPattern = CType(textProvider.GetCurrentPattern(TextPattern.Pattern), TextPattern)

    If (textpatternPattern Is Nothing) Then
        Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
        Return Nothing
    End If
    Dim currentSelection As TextPatternRange() = textpatternPattern.GetSelection()
    ' GetText(-1) retrieves all characters but can be inefficient
    Return currentSelection(0).GetText(length)
End Function

注解

GetText 尊重隐藏文本和可见文本。 UI 自动化客户端可以检查 IsHiddenAttribute,了解文本可见性。

如果 maxLength 大于调用方的文本范围长度,则返回的字符串将是文本范围的纯文本。

GetText 不受文本流中的终结点顺序的影响;它将始终返回逻辑文本流顺序中文本范围的“开始”终结点和“结束”终结点之间的文本。

适用于

另请参阅