Share via


TextPointer.GetOffsetToPosition(TextPointer) Método

Definição

Retorna a contagem de símbolos entre o TextPointer atual e um segundo TextPointer especificado.

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

Parâmetros

position
TextPointer

Um TextPointer que especifica uma posição da qual encontrar a distância (em símbolos).

Retornos

Int32

O número relativo de símbolos entre o TextPointer atual e position. Um valor negativo indica que o TextPointer atual segue a posição especificada por position, 0 indica que as posições são iguais e um valor positivo indica que o TextPointer atual precede a posição especificada por position.

Exceções

position especifica uma posição fora do contêiner de texto associado à posição atual.

Exemplos

O exemplo a seguir demonstra um uso para este método. O exemplo usa o GetOffsetToPosition método para localizar os deslocamentos para duas TextPointer instâncias e usa essas informações para salvar e restaurar a seleção em um RichTextBox. O exemplo pressupõe que o conteúdo do RichTextBox não tenha sido alterado entre um salvamento de seleção e uma restauração de seleção.

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

Comentários

Qualquer um dos seguintes é considerado um símbolo:

  • Uma marca de abertura ou fechamento para um TextElement elemento.

  • Um UIElement elemento contido em um InlineUIContainer ou BlockUIContainer. Observe que esse UIElement tipo sempre é contado como exatamente um símbolo; qualquer conteúdo adicional ou elementos contidos pelo UIElement não são contados como símbolos.

  • Um caractere Unicode de 16 bits dentro de um elemento de texto Run .

Aplica-se a

Confira também