TextPointer.GetOffsetToPosition(TextPointer) 메서드

정의

현재 TextPointer 지정된 두 번째 TextPointer사이의 기호 수를 반환합니다.

public:
 int GetOffsetToPosition(System::Windows::Documents::TextPointer ^ position);
public int GetOffsetToPosition(System.Windows.Documents.TextPointer position);
member this.GetOffsetToPosition : System.Windows.Documents.TextPointer -> int
Public Function GetOffsetToPosition (position As TextPointer) As Integer

매개 변수

position
TextPointer

기호에서 거리를 찾을 위치를 지정하는 A TextPointer 입니다.

반품

현재 TextPointer 와 . 사이의 상대 기호 수입니다 position. 음수 값은 현재 TextPointer 위치가 지정한 position위치를 따른다는 것을 나타내고, 0은 위치가 같고 양수 값은 현재 TextPointer 가 지정한 position위치 앞에 있음을 나타냅니다.

예외

position 는 현재 위치와 연결된 텍스트 컨테이너 외부의 위치를 지정합니다.

예제

다음 예제에서는이 메서드에 대 한 사용을 보여 줍니다. 이 예제에서는 메서드를 GetOffsetToPosition 사용하여 두 TextPointer 인스턴스에 대한 오프셋을 찾은 다음 이 정보를 사용하여 선택 영역을 RichTextBox저장하고 복원합니다. 이 예제에서는 선택 저장과 선택 복원 간에 내용 RichTextBox 이 변경되지 않았다고 가정합니다.

struct SelectionOffsets { internal int Start; internal int End; }
 
SelectionOffsets GetSelectionOffsetsRTB(RichTextBox richTextBox)
{
    SelectionOffsets selectionOffsets;
 
    TextPointer contentStart = richTextBox.Document.ContentStart;

    // Find the offset for the starting and ending TextPointers.
    selectionOffsets.Start = contentStart.GetOffsetToPosition(richTextBox.Selection.Start);
    selectionOffsets.End = contentStart.GetOffsetToPosition(richTextBox.Selection.End);

    return selectionOffsets;
}

void RestoreSelectionOffsetsRTB(RichTextBox richTextBox, SelectionOffsets selectionOffsets)
{
    TextPointer contentStart = richTextBox.Document.ContentStart;
 
    // Use previously determined offsets to create corresponding TextPointers,
    // and use these to restore the selection.
    richTextBox.Selection.Select(
       contentStart.GetPositionAtOffset(selectionOffsets.Start),
       contentStart.GetPositionAtOffset(selectionOffsets.End)
    );
}
Private Structure SelectionOffsets
    Friend Start As Integer
    Friend [End] As Integer
End Structure

Private Function GetSelectionOffsetsRTB(ByVal richTextBox As RichTextBox) As SelectionOffsets
    Dim selectionOffsets As SelectionOffsets

    Dim contentStart As TextPointer = richTextBox.Document.ContentStart

    ' Find the offset for the starting and ending TextPointers.
    selectionOffsets.Start = contentStart.GetOffsetToPosition(richTextBox.Selection.Start)
    selectionOffsets.End = contentStart.GetOffsetToPosition(richTextBox.Selection.End)

    Return selectionOffsets
End Function

Private Sub RestoreSelectionOffsetsRTB(ByVal richTextBox As RichTextBox, ByVal selectionOffsets As SelectionOffsets)
    Dim contentStart As TextPointer = richTextBox.Document.ContentStart

    ' Use previously determined offsets to create corresponding TextPointers,
    ' and use these to restore the selection.
    richTextBox.Selection.Select(contentStart.GetPositionAtOffset(selectionOffsets.Start), contentStart.GetPositionAtOffset(selectionOffsets.End))
End Sub

설명

다음 중 하나는 기호로 간주됩니다.

  • TextElement 요소에 대한 여는 태그 또는 닫는 태그입니다.

  • UIElement 또는 InlineUIContainerBlockUIContainer 포함된 요소입니다. 이러한 UIElement 항상 정확히 하나의 기호로 계산됩니다. UIElement 포함된 추가 콘텐츠 또는 요소는 기호로 계산되지 않습니다.

  • 텍스트 Run 요소 내의 16비트 유니코드 문자입니다.

적용 대상

추가 정보