TextBox.GetRectFromCharacterIndex(Int32, Boolean) Méthode

Définition

Renvoie une région rectangulaire pour le bord de début ou de fin d’un caractère à un index de caractères spécifique.

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

Paramètres

charIndex
Int32

int

Index de base zéro du caractère pour lequel récupérer le rectangle.

trailingEdge
Boolean

bool

true pour obtenir le bord de fin; false pour obtenir le bord d’avant du caractère.

Retours

Rectangle pour le bord du caractère à l’index spécifié.

Exemples

Cet exemple montre comment utiliser GetRectFromCharacterIndex pour déterminer le rectangle du texte sélectionné. Pour obtenir l’exemple complet, consultez Scénario 2 de l’exemple 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);
}

Remarques

Pour remplacer le menu contextuel, gérez l’événement ContextMenuOpening et remplacez le menu par défaut par un menu personnalisé. Utilisez GetRectFromCharacterIndex pour déterminer où positionner le menu personnalisé. Pour découvrir un exemple de la procédure à suivre, voir le scénario 2 de l’exemple de menu contextuel. Pour plus d’informations sur la conception, consultez Recommandations pour les menus contextuels.

Étant donné que cette méthode retourne un rectangle qui représente un bord de caractère, la largeur du rectangle retourné est toujours 0. Pour obtenir la largeur d’un caractère, vous devez soustraire la valeur X du Rect en tête de la valeur X du Rect de fin.

S’applique à