Share via


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/ 嵌入文本”,其中“{”和“}”是文本范围终结点。

  2. 生成的范围在 DocumentRange 中向后移动到所请求的 unit 边界的开头。

  3. 然后通过将 End 终结点移动一个所请求的 unit 边界,范围从退化范围状态扩展开来。

通过移动 & ExpandToEnclosingUnit
如何针对 Move() 和 ExpandToEnclosingUnit() 调整文本范围的示例

文本容器和嵌入对象(如超链接或表格单元格)的文本内容(或内部文本)在 UI 自动化树的控件视图和内容视图中作为单个连续文本流公开,对象边界被忽略。 如果 UI 自动化客户端检索文本的目的是以某种方式进行叙述、解释或分析,则应检查文本范围中的特殊情况,例如含有文本内容或其他嵌入对象的表格。 这可以通过调用 GetChildren 来获取 AutomationElement 每个嵌入对象的 ,然后调用 RangeFromChild 以获取每个元素的文本范围;这是以递归方式完成的,直到检索到所有文本内容。

嵌入对象跨越的文本范围。
含有嵌入对象及其范围跨度的文本流示例

MoveEndpointByUnit如果控件不支持给定TextUnit的 ,则 延迟到支持的下一个最大 TextUnit

下面列出了从最小单位到最大的订单。

适用于