// returns a rect for selected text // if no text is selected, returns caret location // textbox 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); } rectFirst.Union(rectLast); GeneralTransform transform = textbox.TransformToVisual(null); return transform.TransformBounds(rectFirst); }
// returns a rect for selected text // if no text is selected, returns caret location // textbox 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); } var lastIndex = textbox.SelectionStart + textbox.SelectionLength; if (lastIndex == textbox.Text.Length) { rectLast = textbox.GetRectFromCharacterIndex(lastIndex - 1, true); } else { rectLast = textbox.GetRectFromCharacterIndex(lastIndex, false); } var buttonTransform = textbox.TransformToVisual(null); var 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, dx, y, dy); }
public void AnimateShift(bool reverse, TextBox textBox) { var page = (Window.Current.Content as Frame).Content as Page; page.RenderTransform = new TranslateTransform(); var upDistance = -textBox.TransformToVisual(page).TransformPoint(new Point(0, 0)).Y + textBox.Margin.Top + 32 - 8; var animation = new DoubleAnimation(){ Duration = new Duration(TimeSpan.FromMilliseconds(250)), From = reverse ? upDistance : 0, To = reverse ? 0 : upDistance }; Storyboard.SetTarget(animation, page.RenderTransform); Storyboard.SetTargetProperty(animation, "Y"); var storyboard = new Storyboard(); storyboard.Children.Add(animation); storyboard.Begin(); }
private Rect GetTextBoxRect(TextBox t) { Rect temp = t.GetRectFromCharacterIndex(t.SelectionStart, false); GeneralTransform transform = t.TransformToVisual(null); Point point = transform.TransformPoint(new Point()); point.X = point.X + temp.X; point.Y = point.Y + temp.Y; return new Rect(point, new Size(temp.Width, temp.Height)); }
// Se crea el Rectangulom y su posición 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()); 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); }