Share via


TextBox.GetRectFromCharacterIndex(Int32, Boolean) 메서드

정의

특정 문자 인덱스에서 문자의 선행 또는 후행 가장자리에 대한 사각형 영역을 반환합니다.

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

매개 변수

charIndex
Int32

int

사각형을 검색할 문자의 인덱스(0부터 시작)입니다.

trailingEdge
Boolean

bool

true 이면 후행 에지를 가져옵니다. false 이면 문자의 선행 가장자리를 가져옵니다.

반환

지정된 인덱스에서 문자 가장자리의 사각형입니다.

예제

이 예제에서는 GetRectFromCharacterIndex를 사용하여 선택한 텍스트의 사각형을 확인하는 방법을 보여줍니다. 전체 예제는 ContextMenu 샘플의 시나리오 2를 참조하세요.

// 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);
}

설명

상황에 맞는 메뉴를 재정의하려면 ContextMenuOpening 이벤트를 처리하고 기본 메뉴를 사용자 지정 메뉴로 바꿉 있습니다. GetRectFromCharacterIndex를 사용하여 사용자 지정 메뉴의 위치를 결정합니다. 이에 대한 예제는 ContextMenu 샘플의 시나리오 2를 참조하세요. 디자인 정보는 상황에 맞는 메뉴에 대한 지침을 참조하세요.

이 메서드는 문자 가장자리를 나타내는 사각형을 반환하므로 반환되는 사각형의 너비는 항상 0입니다. 문자의 너비를 얻으려면 후행 RectX 값에서 선행 RectX 값을 빼야 합니다.

적용 대상