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

一个指向由指定偏移量指示的位置的 TextPointer;或者,如果偏移量超过了内容的结束位置,则为 null

注解

以下任一项被视为符号:

另请参阅

适用于

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

一个指向由指定偏移量指示的位置的 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

注解

以下任一项被视为符号:

另请参阅

适用于