TextPatternRange.Move(TextUnit, Int32) メソッド

定義

指定されたテキスト単位数の分、テキスト範囲を移動します。

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

パラメーター

unit
TextUnit

テキスト単位の境界。

count
Int32

移動するテキスト単位の数。 正の値はテキスト範囲を前方に移動し、負の値はテキスト範囲を後方に移動します。0 の場合は移動されません。

戻り値

実際に移動された単位の数。 移動後のテキスト範囲のエンドポイントのいずれかが DocumentRange エンドポイントよりも大きい、または小さい場合、要求した数よりも小さくなる場合があります。

/// -------------------------------------------------------------------
/// <summary>
/// Starts the target application and returns the AutomationElement 
/// obtained from the targets window handle.
/// </summary>
/// <param name="exe">
/// The target application.
/// </param>
/// <param name="filename">
/// The text file to be opened in the target application
/// </param>
/// <returns>
/// An AutomationElement representing the target application.
/// </returns>
/// -------------------------------------------------------------------
private AutomationElement StartTarget(string exe, string filename)
{
    // Start text editor and load with a text file.
    Process p = Process.Start(exe, filename);

    // targetApp --> the root AutomationElement.
    AutomationElement targetApp =
        AutomationElement.FromHandle(p.MainWindowHandle);

    return targetApp;
}
''' -------------------------------------------------------------------
''' <summary>
''' Starts the target application and returns the AutomationElement 
''' obtained from the targets window handle.
''' </summary>
''' <param name="exe">
''' The target application.
''' </param>
''' <param name="filename">
''' The text file to be opened in the target application
''' </param>
''' <returns>
''' An AutomationElement representing the target application.
''' </returns>
''' -------------------------------------------------------------------
Private Function StartTarget( _
ByVal exe As String, ByVal filename As String) As AutomationElement
    ' Start text editor and load with a text file.
    Dim p As Process = Process.Start(exe, filename)

    ' targetApp --> the root AutomationElement.
    Dim targetApp As AutomationElement
    targetApp = AutomationElement.FromHandle(p.MainWindowHandle)

    Return targetApp
End Function
/// -------------------------------------------------------------------
/// <summary>
/// Obtain the text control of interest from the target application.
/// </summary>
/// <param name="targetApp">
/// The target application.
/// </param>
/// <returns>
/// An AutomationElement that represents a text provider..
/// </returns>
/// -------------------------------------------------------------------
private AutomationElement GetTextElement(AutomationElement targetApp)
{
    // The control type we're looking for; in this case 'Document'
    PropertyCondition cond1 =
        new PropertyCondition(
        AutomationElement.ControlTypeProperty,
        ControlType.Document);

    // The control pattern of interest; in this case 'TextPattern'.
    PropertyCondition cond2 = 
        new PropertyCondition(
        AutomationElement.IsTextPatternAvailableProperty, 
        true);

    AndCondition textCondition = new AndCondition(cond1, cond2);

    AutomationElement targetTextElement =
        targetApp.FindFirst(TreeScope.Descendants, textCondition);

    // If targetText is null then a suitable text control was not found.
    return targetTextElement;
}
''' -------------------------------------------------------------------
''' <summary>
''' Obtain the text control of interest from the target application.
''' </summary>
''' <param name="targetApp">
''' The target application.
''' </param>
''' <returns>
''' An AutomationElement. representing a text control.
''' </returns>
''' -------------------------------------------------------------------
Private Function GetTextElement(ByVal targetApp As AutomationElement) As AutomationElement
    ' The control type we're looking for; in this case 'Document'
    Dim cond1 As PropertyCondition = _
        New PropertyCondition( _
        AutomationElement.ControlTypeProperty, _
        ControlType.Document)

    ' The control pattern of interest; in this case 'TextPattern'.
    Dim cond2 As PropertyCondition = _
        New PropertyCondition( _
        AutomationElement.IsTextPatternAvailableProperty, _
        True)

    Dim textCondition As AndCondition = New AndCondition(cond1, cond2)

    Dim targetTextElement As AutomationElement = _
        targetApp.FindFirst(TreeScope.Descendants, textCondition)

    ' If targetText is null then a suitable text control was not found.
    Return targetTextElement
End Function
/// -------------------------------------------------------------------
/// <summary>
/// Moves a text range a specified number of text units. The text range 
/// is the current selection.
/// </summary>
/// <param name="targetTextElement">
/// The AutomationElment that represents a text control.
/// </param>
/// <param name="textUnit">
/// The text unit value.
/// </param>
/// <param name="units">
/// The number of text units to move.
/// </param>
/// <param name="direction">
/// Direction to move the text range. Valid values are -1, 0, 1.
/// </param>
/// <returns>
/// The number of text units actually moved. This can be less than the 
/// number requested if either of the new text range endpoints is 
/// greater than or less than the DocumentRange endpoints. 
/// </returns>
/// <remarks>
/// Moving the text range does not modify the text source in any way. 
/// Only the text range starting and ending endpoints are modified.
/// </remarks>
/// -------------------------------------------------------------------
private Int32 MoveSelection(
    AutomationElement targetTextElement, 
    TextUnit textUnit,
    int units,
    int direction)
{
    TextPattern textPattern =
        targetTextElement.GetCurrentPattern(TextPattern.Pattern) 
        as TextPattern;

    if (textPattern == null)
    {
        // Target control doesn't support TextPattern.
        return -1;
    }

    TextPatternRange[] currentSelection = textPattern.GetSelection();

    if (currentSelection.Length > 1)
    {
        // For this example, we cannot move more than one text range.
        return -1;
    }

    return currentSelection[0].Move(textUnit, Math.Sign(direction) * units);
}
''' -------------------------------------------------------------------
''' <summary>
''' Moves a text range a specified number of text units.
''' </summary>
''' <param name="targetTextElement">
''' The AutomationElement that represents a text control.
''' </param>
''' <param name="textUnit">
''' The text unit value.
''' </param>
''' <param name="units">
''' The number of text units to move.
''' </param>
''' <param name="direction">
''' Direction to move the text range. Valid values are -1, 0, 1.
''' </param>
''' <returns>
''' The number of text units actually moved. This can be less than the 
''' number requested if either of the new text range endpoints is 
''' greater than or less than the DocumentRange endpoints. 
''' </returns>
''' <remarks>
''' Moving the text range does not modify the text source in any way. 
''' Only the text range starting and ending endpoints are modified.
''' </remarks>
''' -------------------------------------------------------------------
Private Function MoveSelection( _
    ByVal targetTextElement As AutomationElement, _
    ByVal textUnit As TextUnit, _
    ByVal units As Integer, _
    ByVal direction As Integer) As Integer

    Dim textPattern As TextPattern = _
    DirectCast( _
    targetTextElement.GetCurrentPattern(textPattern.Pattern), _
    TextPattern)

    If (textPattern Is Nothing) Then
        ' Target control doesn't support TextPattern.
        Return -1
    End If

    Dim currentSelection As TextPatternRange() = _
    textPattern.GetSelection()

    If (currentSelection.Length > 1) Then
        ' For this example, we cannot move more than one text range.
        Return -1
    End If

    Return currentSelection(0).Move(textUnit, Math.Sign(direction) * units)
End Function

注釈

テキスト範囲の内容を走査する必要がある場合、 Move メソッドを正常に実行するために、一連の手順がその背後で関係しています。

  1. テキスト範囲は正規化されます。つまり、テキスト範囲は Start エンドポイントで低次元テキスト範囲に縮小されるため、 End エンドポイントは不要になります。 この手順は、テキスト範囲が境界をまた unit ぐ状況であいまいさを取り除くために必要です。たとえば、"{The U}RL https://www.microsoft.com/ is embedded in text" ("{" と "}" はテキスト範囲のエンドポイント) です。

  2. 結果として得られる範囲は、 DocumentRange 内で、要求された unit 境界の先頭に向かって後方に移動されます。

  3. この範囲は、 DocumentRange 内で、 unit 境界の要求された数だけ、前方または後方に移動されます。

  4. その後、この範囲は、要求された 1 つの End 境界の分、 unit エンドポイントを移動することによって、低次元テキスト範囲の状態から展開されます。

Move & ExpandToEnclosingUnit Range
テキスト範囲を Move() と ExpandToEnclosingUnit() に対して調整する方法の例

テキスト コンテナーのテキスト コンテンツ (内部テキスト) と埋め込みオブジェクト (ハイパーリンクやテーブルのセルなど) は、連続する単一のテキスト ストリームとして、UI オートメーション ツリーのコントロール ビューとコンテンツ ビューの両方で公開されます。そのため、オブジェクトの境界は無視されます。 UI オートメーション クライアントがなんらかの方法で列挙、解釈、分析を目的としてテキストを取得している場合、テキスト コンテンツを含むテーブルやその他の埋め込みオブジェクトなど、特殊なケースについて、テキスト範囲を確認する必要があります。 これを実現するには、 を呼び出 GetChildren して各埋め込みオブジェクトの を AutomationElement 取得し、 を呼び出 RangeFromChild して各要素のテキスト範囲を取得します。これは、すべてのテキスト コンテンツが取得されるまで再帰的に行われます。

埋め込みオブジェクトがまたがるテキスト範囲。
埋め込みオブジェクトとその範囲を含むテキスト ストリームの例

Move は、非表示のテキストと表示されるテキストの両方を尊重します。 UI オートメーション クライアントは、 をチェックしてIsHiddenAttributeテキストを表示できます。

Moveは、指定された がコントロールでサポートされていない場合TextUnit、サポートされている次の最大TextUnitに遅延します。

最小単位から最大までの順序を以下に示します。

注意

テキスト範囲はテキストの別の部分にまたがるだけなので、テキストは変更されません。

適用対象

こちらもご覧ください