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边界的情况下,需要执行此步骤来消除歧义;例如,“{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受支持。
下面列出了从最小单位到最大单位的顺序。
注释
文本不会以任何方式更改,因为文本范围只是跨越文本的不同部分。