TextPatternRange.Move(TextUnit, Int32) Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Verschiebt den Textbereich in die angegebene Anzahl von Texteinheiten.
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
Parameter
- unit
- TextUnit
Die Texteinheitsgrenze.
- count
- Int32
Die Anzahl der zu verschiebenden Texteinheiten. Ein positiver Wert verschiebt den Textbereich vorwärts, ein negativer Wert verschiebt den Textbereich rückwärts, und 0 hat keine Auswirkung.
Gibt zurück
Die Anzahl der tatsächlich verschobenen Einheiten. Dies kann kleiner als die angeforderte Zahl sein, wenn einer der neuen Textbereichsendpunkte größer oder kleiner als die DocumentRange Endpunkte ist.
Beispiele
/// -------------------------------------------------------------------
/// <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
Hinweise
Wenn es erforderlich ist, den Inhalt eines Textbereichs zu durchlaufen, sind hinter den Kulissen eine Reihe von Schritten beteiligt, damit die Move Methode erfolgreich ausgeführt werden kann.
Der Textbereich wird normalisiert; d. h. der Textbereich wird auf einen degenerierten Bereich am Start Endpunkt reduziert, wodurch der End Endpunkt überflüssig wird. Dieser Schritt ist erforderlich, um Mehrdeutigkeit in Situationen zu entfernen, in denen ein Textbereich Grenzen umfasst
unit, z. B. "{Die U}RL https://www.microsoft.com/ ist in Text eingebettet", wobei "{" und "}" die Endpunkte des Textbereichs sind.Der resultierende Bereich wird im DocumentRange zurück an den Anfang der angeforderten
unit-Grenze verschoben.Der Bereich wird um die angeforderte Anzahl von DocumentRange -Grenzen nach vorne oder nach hinten im
unitverschoben.Der Bereich wird dann von einem degenerierten Bereichszustand erweitert, indem der End Endpunkt um eine angeforderte
unitGrenze verschoben wird.
Beispiele für die Anpassung eines Textbereichs für Move() und ExpandToEnclosingUnit()
Der textbezogene Inhalt (oder innerer Text) eines Textcontainers und eines eingebetteten Objekts, z. B. ein Link oder eine Tabellenzelle, wird als einzelner fortlaufender Textstrom sowohl in der Steuerelementansicht als auch in der Inhaltsansicht der Benutzeroberflächenautomatisierungs-Struktur verfügbar gemacht; Objektgrenzen werden ignoriert. Wenn ein Benutzeroberflächenautomatisierung-Client den Text zum Zweck des Vorlesens, Interpretierens oder Analysierens in irgendeiner Weise abruft, sollte der Textbereich auf Sonderfälle überprüft werden, z. B. eine Tabelle mit Textinhalt oder andere eingebettete Objekte. Dies kann durch aufrufen GetChildren , um ein AutomationElement eingebettetes Objekt abzurufen und dann RangeFromChild einen Textbereich für jedes Element abzurufen. Dies erfolgt rekursiv, bis alle Textinhalte abgerufen wurden.
Beispiel für einen Textstream mit eingebetteten Objekten und deren Bereichsspannen
Move berücksichtigt ausgeblendeten und sichtbaren Text. Der Benutzeroberflächenautomatisierung-Client kann die IsHiddenAttribute auf Textsichtbarkeit überprüfen.
Move Wird auf die nächstgrößte TextUnit unterstützung zurückverzögert, wenn das angegebene TextUnit Steuerelement nicht unterstützt wird.
Die Reihenfolge, von der kleinsten Einheit bis zum größten, wird unten aufgeführt.
Note
Der Text wird in keiner Weise geändert, da sich der Textbereich nur über einen anderen Teil des Texts erstreckt.