TextPatternRange.Move(TextUnit, Int32) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定した数のテキスト単位をテキスト範囲に移動します。
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 メソッドが正常に実行されるように、一連の手順がバックグラウンドで行われます。
テキスト範囲は正規化されます。つまり、テキスト範囲は、 Start エンドポイントで縮小された範囲に折りたたまれるため、 End エンドポイントは余分になります。 この手順は、テキスト範囲が
unit境界にまたがる状況であいまいさを取り除くために必要です。たとえば、"{The U}RL https://www.microsoft.com/ is embedded in text" ("{" と "}" はテキスト範囲のエンドポイントです)。結果として得られる範囲は、 DocumentRange 内で、要求された
unit境界の先頭に向かって後方に移動されます。範囲は、要求された数のDocumentRange境界によって
unit内で前方または後方に移動されます。その後、要求された 1 つのEnd境界によって
unitエンドポイントを移動することで、範囲が縮退した範囲の状態から拡張されます。
Move() と ExpandToEnclosingUnit() に対してテキスト範囲を調整する方法の例
テキスト コンテナーのテキスト コンテンツ (または内部テキスト) と、ハイパーリンクやテーブル セルなどの埋め込みオブジェクトは、コントロール ビューと UI オートメーション ツリーのコンテンツ ビューの両方で、単一の連続テキスト ストリームとして公開されます。オブジェクトの境界は無視されます。 UI オートメーション クライアントが、何らかの方法で暗唱、解釈、または分析を目的としてテキストを取得する場合は、テキスト コンテンツや他の埋め込みオブジェクトを含むテーブルなど、特殊なケースに対してテキスト範囲をチェックする必要があります。 これを実現するには、 GetChildren を呼び出して各埋め込みオブジェクトの AutomationElement を取得し、 RangeFromChild を呼び出して各要素のテキスト範囲を取得します。これは、すべてのテキスト コンテンツが取得されるまで再帰的に行われます。
埋め込みオブジェクトとその範囲スパンを含むテキスト ストリームの例
Move は、非表示のテキストと表示されるテキストの両方を尊重します。 UI オートメーション クライアントは、IsHiddenAttributeでテキストの表示を確認できます。
Moveは、指定されたTextUnitがコントロールでサポートされていない場合にサポートされる次の最大TextUnitに遅延します。
最小単位から最大値までの順序を次に示します。
Note
テキスト範囲はテキストの別の部分にまたがるだけなので、テキストは変更されません。