TextPatternRange.GetText(Int32) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳文字範圍的純文字。
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 不會受到文本流程端點順序的影響;它總是會依邏輯文字流順序回傳文字範圍的開始端點與結束端點之間的文字。