共用方式為


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 會同時遵守隱藏和可見的文字。 使用者介面自動化用戶端可以檢查 IsHiddenAttribute 文字可見度。

如果 maxLength 大於呼叫端的文字範圍長度,則傳回的字串會是文字範圍的純文字。

GetText 不會受到文字流程中端點的順序影響;它一律會以邏輯文字流程順序傳回文字範圍開始和結束端點之間的文字。

適用於

另請參閱