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 不受文本流中终结点顺序的影响;它将始终按逻辑文本流顺序返回文本范围开始和结束终结点之间的文本。

适用于

另请参阅