TextPointer.GetPositionAtOffset Метод

Определение

Возвращение TextPointer в положение, указанное заданным смещением в символах от начала содержимого.

Перегрузки

GetPositionAtOffset(Int32, LogicalDirection)

Возвращение TextPointer в положение, указанное заданным смещением в символах от начала текущего TextPointer, а также в указанном направлении.

GetPositionAtOffset(Int32)

Возвращение TextPointer в положение, указанное заданным смещением в символах от начала текущего TextPointer.

GetPositionAtOffset(Int32, LogicalDirection)

Возвращение TextPointer в положение, указанное заданным смещением в символах от начала текущего TextPointer, а также в указанном направлении.

public:
 System::Windows::Documents::TextPointer ^ GetPositionAtOffset(int offset, System::Windows::Documents::LogicalDirection direction);
public System.Windows.Documents.TextPointer GetPositionAtOffset (int offset, System.Windows.Documents.LogicalDirection direction);
member this.GetPositionAtOffset : int * System.Windows.Documents.LogicalDirection -> System.Windows.Documents.TextPointer
Public Function GetPositionAtOffset (offset As Integer, direction As LogicalDirection) As TextPointer

Параметры

offset
Int32

Смещение (в символах) на которое необходимо рассчитывать и возвращать положение. Если смешение отрицательное, возвращенный TextPointer предшествует текущему TextPointer; в противном случае, он идет следом.

direction
LogicalDirection

Одно из значений LogicalDirection, которое указывает логическое направление возвращенного TextPointer.

Возвращаемое значение

Указатель TextPointer на положение, определяемое заданным смещением, или null, если смещение выходит за рамки содержимого.

Комментарии

Символом считается любое из следующих элементов:

  • Открывающий или закрывающий тег для TextElement элемента.

  • Элемент UIElement , содержащийся в InlineUIContainer или BlockUIContainer. Обратите внимание, что такой UIElement объект всегда считается ровно одним символом; любое дополнительное содержимое или элементы, содержащиеся в UIElement , не учитываются как символы.

  • 16-разрядный символ Юникода внутри текстового Run элемента.

См. также раздел

Применяется к

GetPositionAtOffset(Int32)

Возвращение TextPointer в положение, указанное заданным смещением в символах от начала текущего TextPointer.

public:
 System::Windows::Documents::TextPointer ^ GetPositionAtOffset(int offset);
public System.Windows.Documents.TextPointer GetPositionAtOffset (int offset);
member this.GetPositionAtOffset : int -> System.Windows.Documents.TextPointer
Public Function GetPositionAtOffset (offset As Integer) As TextPointer

Параметры

offset
Int32

Смещение (в символах) на которое необходимо рассчитывать и возвращать положение. Если смещение отрицательное, положение рассчитывается в логическом направлении, противоположном заданному свойством LogicalDirection.

Возвращаемое значение

Указатель TextPointer на положение, определенное заданным смещением, или null, если невозможно найти соответствующее положение.

Примеры

В следующем примере показано использование этого метода. В примере метод используется GetPositionAtOffset для реализации пары методов, один из них вычисляет смещение до указанной позиции относительно любого размещающего абзаца, а другой — для возврата TextPointer к указанному смещению в указанном абзаце.

// Returns the offset for the specified position relative to any containing paragraph.
int GetOffsetRelativeToParagraph(TextPointer position)
{
    // Adjust the pointer to the closest forward insertion position, and look for any
    // containing paragraph.
    Paragraph paragraph = (position.GetInsertionPosition(LogicalDirection.Forward)).Paragraph;

    // Some positions may be not within any Paragraph; 
    // this method returns an offset of -1 to indicate this condition.
    return (paragraph == null) ? -1 : paragraph.ContentStart.GetOffsetToPosition(position);
}

// Returns a TextPointer to a specified offset into a specified paragraph. 
TextPointer GetTextPointerRelativeToParagraph(Paragraph paragraph, int offsetRelativeToParagraph)
{
    // Verify that the specified offset falls within the specified paragraph.  If the offset is
    // past the end of the paragraph, return a pointer to the farthest offset position in the paragraph.
    // Otherwise, return a TextPointer to the specified offset in the specified paragraph.
    return (offsetRelativeToParagraph > paragraph.ContentStart.GetOffsetToPosition(paragraph.ContentEnd)) 
        ? paragraph.ContentEnd : paragraph.ContentStart.GetPositionAtOffset(offsetRelativeToParagraph);
}
' Returns the offset for the specified position relative to any containing paragraph.
Private Function GetOffsetRelativeToParagraph(ByVal position As TextPointer) As Integer
    ' Adjust the pointer to the closest forward insertion position, and look for any
    ' containing paragraph.
    Dim paragraph As Paragraph = (position.GetInsertionPosition(LogicalDirection.Forward)).Paragraph

    ' Some positions may be not within any Paragraph 
    ' this method returns an offset of -1 to indicate this condition.
    Return If((paragraph Is Nothing), -1, paragraph.ContentStart.GetOffsetToPosition(position))
End Function

' Returns a TextPointer to a specified offset into a specified paragraph. 
Private Function GetTextPointerRelativeToParagraph(ByVal paragraph As Paragraph, ByVal offsetRelativeToParagraph As Integer) As TextPointer
    ' Verify that the specified offset falls within the specified paragraph.  If the offset is
    ' past the end of the paragraph, return a pointer to the farthest offset position in the paragraph.
    ' Otherwise, return a TextPointer to the specified offset in the specified paragraph.
    Return If((offsetRelativeToParagraph > paragraph.ContentStart.GetOffsetToPosition(paragraph.ContentEnd)), paragraph.ContentEnd, paragraph.ContentStart.GetPositionAtOffset(offsetRelativeToParagraph))
End Function

Комментарии

Символом считается любое из следующих элементов:

  • Открывающий или закрывающий тег для TextElement элемента.

  • Элемент UIElement , содержащийся в InlineUIContainer или BlockUIContainer. Обратите внимание, что такой UIElement объект всегда считается ровно одним символом; любое дополнительное содержимое или элементы, содержащиеся в UIElement , не учитываются как символы.

  • 16-разрядный символ Юникода внутри текстового Run элемента.

См. также раздел

Применяется к