Share via


TextPattern.GetSelection 方法

定義

擷取與目前文字選取範圍關聯之斷續文字範圍的集合。

public:
 cli::array <System::Windows::Automation::Text::TextPatternRange ^> ^ GetSelection();
public System.Windows.Automation.Text.TextPatternRange[] GetSelection ();
member this.GetSelection : unit -> System.Windows.Automation.Text.TextPatternRange[]
Public Function GetSelection () As TextPatternRange()

傳回

TextPatternRange[]

斷續文字範圍的集合。

例外狀況

文字容器不支援文字選取範圍。

範例

private TextPatternRange CurrentSelection(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 null;
    }
    TextPatternRange[] currentSelection = textpatternPattern.GetSelection();
    return currentSelection[0];
}
Private Function CurrentSelection(ByVal target As AutomationElement) As TextPatternRange
    ' 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 currentTextSelection As TextPatternRange() = textpatternPattern.GetSelection()
    Return currentTextSelection(0)
End Function

備註

如果未選取任何文字, GetSelection 則會傳回空範圍 (空範圍) 系統游標的位置 (文字插入點) 。

如果系統游標 (文字插入點) 不存在, GetSelection 可能會傳回 null

注意

SupportedTextSelection使用 屬性來測試控制項是否支援文字插入點的文字選取範圍或位置。

適用於

另請參閱