TextBox.GetRectFromCharacterIndex(Int32, Boolean) Método

Definición

Devuelve una región rectangular para el borde inicial o final de un carácter en un índice de caracteres específico.

public:
 virtual Rect GetRectFromCharacterIndex(int charIndex, bool trailingEdge) = GetRectFromCharacterIndex;
Rect GetRectFromCharacterIndex(int const& charIndex, bool const& trailingEdge);
public Rect GetRectFromCharacterIndex(int charIndex, bool trailingEdge);
function getRectFromCharacterIndex(charIndex, trailingEdge)
Public Function GetRectFromCharacterIndex (charIndex As Integer, trailingEdge As Boolean) As Rect

Parámetros

charIndex
Int32

int

Índice de base cero del carácter para el que se va a recuperar el rectángulo.

trailingEdge
Boolean

bool

true para obtener el borde final; false para obtener el borde inicial del carácter.

Devoluciones

Rectángulo para el borde del carácter en el índice especificado.

Ejemplos

En este ejemplo se muestra cómo usar GetRectFromCharacterIndex para determinar el rectángulo del texto seleccionado. Para obtener el ejemplo completo, vea Escenario 2 del ejemplo ContextMenu.

// Returns a rect for selected text.
// If no text is selected, returns caret location.
// Text box should not be empty.
private Rect GetTextboxSelectionRect(TextBox textbox)
{
    Rect rectFirst, rectLast;
    if (textbox.SelectionStart == textbox.Text.Length)
    {
        rectFirst = textbox.GetRectFromCharacterIndex(textbox.SelectionStart - 1, true);
    }
    else
    {
        rectFirst = textbox.GetRectFromCharacterIndex(textbox.SelectionStart, false);
    }

    int lastIndex = textbox.SelectionStart + textbox.SelectionLength;
    if (lastIndex == textbox.Text.Length)
    {
        rectLast = textbox.GetRectFromCharacterIndex(lastIndex - 1, true);
    }
    else
    {
        rectLast = textbox.GetRectFromCharacterIndex(lastIndex, false);
    }

    GeneralTransform buttonTransform = textbox.TransformToVisual(null);
    Point point = buttonTransform.TransformPoint(new Point());

    // Make sure that we return a valid rect if selection is on multiple lines
    // and end of the selection is to the left of the start of the selection.
    double x, y, dx, dy;
    y = point.Y + rectFirst.Top;
    dy = rectLast.Bottom - rectFirst.Top;
    if (rectLast.Right > rectFirst.Left)
    {
        x = point.X + rectFirst.Left;
        dx = rectLast.Right - rectFirst.Left;
    }
    else
    {
        x = point.X + rectLast.Right;
        dx = rectFirst.Left - rectLast.Right;
    }

    return new Rect(x, y, dx, dy);
}

Comentarios

Para invalidar el menú contextual, controle el evento ContextMenuOpening y reemplace el menú predeterminado por un menú personalizado. Use GetRectFromCharacterIndex para determinar dónde colocar el menú personalizado. Para obtener un ejemplo de esto, consulta el escenario 2 de la muestra de ContextMenu. Para obtener información de diseño, consulta Directrices para menús contextuales.

Dado que este método devuelve un rectángulo que representa un borde de caracteres, el ancho del rectángulo que se devuelve siempre es 0. Para obtener el ancho de un carácter, debe restar el valor X del rect inicial del valor X del recting final.

Se aplica a