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

指定返回 TextPointer逻辑方向的 LogicalDirection 值之一。

返回

指定偏移量所指示的位置的 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;如果未找到相应的位置,则 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

注解

以下任一项都被视为符号:

另请参阅

适用于