TextPointer.GetPositionAtOffset 方法

定義

將 a TextPointer 返回到指定偏移量所指示的位置,以符號表示,從內容開始。

多載

名稱 Description
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 A 移動到指定偏移量所指示的位置,或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 A 與指定偏移量所示的位置相符,或null若找不到對應位置。

範例

以下範例展示了此方法的應用。 範例中使用此 GetPositionAtOffset 方法實作一對方法,一組計算相對於任一寄存段落的偏移量至指定位置,另一組則將 a 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

備註

以下任一都被視為符號:

另請參閱

適用於