共用方式為


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

備註

下列任一項視為符號:

另請參閱

適用於

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

備註

下列任一項視為符號:

另請參閱

適用於