語言

TextPatternRange.MoveEndpointByUnit 方法

定義

在文件範圍內移動文本範圍的一個端點,該節點數為指定數量 TextUnit。

public:
 int MoveEndpointByUnit(System::Windows::Automation::Text::TextPatternRangeEndpoint endpoint, System::Windows::Automation::Text::TextUnit unit, int count);
public int MoveEndpointByUnit(System.Windows.Automation.Text.TextPatternRangeEndpoint endpoint, System.Windows.Automation.Text.TextUnit unit, int count);
member this.MoveEndpointByUnit : System.Windows.Automation.Text.TextPatternRangeEndpoint * System.Windows.Automation.Text.TextUnit * int -> int
Public Function MoveEndpointByUnit (endpoint As TextPatternRangeEndpoint, unit As TextUnit, count As Integer) As Integer

參數

endpoint
TextPatternRangeEndpoint

移動的終點。

unit
TextUnit

移動的文字單元。

count
Int32

需要移動的單位數量。 正數則將終點前進。 負數則向後移動。 計數為0則無效。

傳回

實際移動的單位數量,如果移動端點會落在文件的開頭或結尾,這可能比請求的數量還少。

範例

private Int32 MoveEndpointByRangeFromSelection(AutomationElement target, Int32 units)
{
    // 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 0;
    }
    TextPatternRange[] currentSelection = textpatternPattern.GetSelection();

    return currentSelection[0].MoveEndpointByUnit(
        TextPatternRangeEndpoint.Start, TextUnit.Paragraph, units);
}
Private Function MoveEndpointByRangeFromSelection(ByVal target As AutomationElement, ByVal units As Int32) As Int32
    ' 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).MoveEndpointByUnit(TextPatternRangeEndpoint.Start, TextUnit.Paragraph, units)
End Function

備註

當需要遍歷文字範圍的內容時,在幕後需要一系列步驟,才能讓 Move 方法順利執行。

  1. 文字範圍已被正規化;意味著,文字範圍會折疊為Start 端點的退化範圍,使End 端點變得多餘。 此步驟是為了消除文字範圍跨 unit 越邊界的情況中的歧義;例如,「{The U}RL https://www.microsoft.com/ is embedded in text」中,其中「{」和「}」為文字範圍的端點。

  2. 產生的範圍會移動至 DocumentRange 中,向後到達所要求 unit 界限的開頭。

  3. 然後,範圍會從退化的範圍狀態通過將 End 端點移動到一個要求的 unit 邊界來進行擴展。

依移動與擴展 調整範圍 透過移動與擴展調整範圍文字範圍如何調整 Move() 與 ExpandToEnclosingUnit() 的範例

在 UI 自動化樹狀結構的控制項檢視和內容檢視 (例如超連結或表格儲存格) 中,文字容器和內嵌物件的文字內容 (或內部文字) 會公開為單一連續的文字資料流; 物件界限會被忽略。 如果UI自動化用戶端擷取文字是為了以某種方式進行背誦、解譯或分析,則應該檢查文字範圍是否有特殊情況,例如含有文字內容或其他內嵌對象的數據表。 這可以透過呼叫 GetChildren 取得每個嵌入物件的 , AutomationElement 然後 RangeFromChild 呼叫取得每個元素的文字範圍來達成;此過程會以遞迴方式進行,直到所有文字內容都被檢索完畢。

由嵌入物件跨越的文字範圍。由包含嵌入物件及其範圍範圍的文字串流範例

MoveEndpointByUnit若給定TextUnit未被控制組支持,則 讓位給出的下一個較大TextUnit支持者。

從最小到最大單位的順序如下所示。

適用於