共用方式為


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

備註

下列任一項都會被視為符號:

另請參閱

適用於