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/ 嵌入文本”,其中“{”和“}”是文本范围终结点。生成的范围在 DocumentRange 中向后移动到所请求的
unit
边界的开头。范围在 DocumentRange 中向前或向后移动所请求的
unit
边界的数目。然后通过将 End 终结点移动一个所请求的
unit
边界,范围从退化范围状态扩展开来。
如何针对 Move() 和 ExpandToEnclosingUnit() 调整文本范围的示例
文本容器和嵌入对象(如超链接或表格单元格)的文本内容(或内部文本)在 UI 自动化树的控件视图和内容视图中作为单个连续文本流公开,对象边界被忽略。 如果 UI 自动化客户端检索文本的目的是以某种方式进行叙述、解释或分析,则应检查文本范围中的特殊情况,例如含有文本内容或其他嵌入对象的表格。 这可以通过调用 GetChildren 来获取 AutomationElement 每个嵌入对象的 ,然后调用 RangeFromChild 以获取每个元素的文本范围;这是以递归方式完成的,直到检索到所有文本内容。
含有嵌入对象及其范围跨度的文本流示例
Move 尊重隐藏文本和可见文本。 UI 自动化客户端可以检查 文本IsHiddenAttribute可见性。
Move如果控件不支持给定TextUnit的 ,则 延迟到支持的下一个最大 TextUnit 。
下面列出了从最小单位到最大的订单。
注意
文本不会以任何方式更改,因为文本范围仅跨越文本的不同部分。