Share via


TextPatternRange.ExpandToEnclosingUnit(TextUnit) 方法

定义

将文本范围扩展到指定的 TextUnit

public:
 void ExpandToEnclosingUnit(System::Windows::Automation::Text::TextUnit unit);
public void ExpandToEnclosingUnit (System.Windows.Automation.Text.TextUnit unit);
member this.ExpandToEnclosingUnit : System.Windows.Automation.Text.TextUnit -> unit
Public Sub ExpandToEnclosingUnit (unit As TextUnit)

参数

unit
TextUnit

文本单位。

示例

 private void ExpandSelection(AutomationElement target)
{
    // 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;
    }
    TextPatternRange[] currentSelection = textpatternPattern.GetSelection();
    // Expand selection to include entire document
    currentSelection[0].ExpandToEnclosingUnit(TextUnit.Document);
}
Private Sub ExpandSelection(ByVal target As AutomationElement)
    ' 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
    End If
    Dim currentSelection As TextPatternRange() = textpatternPattern.GetSelection()
    currentSelection(0).ExpandToEnclosingUnit(TextUnit.Document)
End Sub

注解

如果范围已经是指定单位的确切数量,则它保持不变。

为了使 ExpandToEnclosingUnit 方法成功执行,将在后台执行一系列操作。

  1. 文本范围已规范化;也就是说,文本范围折叠到终结点处 Start 的退化范围,使 End 终结点变得多余。 若要在文本范围跨越 unit 边界的情况下消除歧义,必须执行此步骤;例如,“{U}RL https://www.microsoft.com/ 嵌入在文本中”,其中“{”和“}”是文本范围终结点。

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

  3. 范围在 DocumentRange 中向前或向后移动所请求的 unit 边界的数目。

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

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

注意

这些步骤是必需的,因为屏幕阅读器通常会在插入点或任何虚拟光标位置读出完整的单词、句子或整个段落。

ExpandToEnclosingUnit 尊重隐藏文本和可见文本。 UI 自动化客户端可以检查 文本IsHiddenAttribute可见性。

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

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

适用于

另请参阅