コード例 #1
0
        public CaretAdorner(TextBox adornedElement)
            : base(adornedElement)
        {
            var fontRect = adornedElement.GetRectFromCharacterIndex(0);

            this.caret = new Rectangle {
                Width               = 1,
                Height              = fontRect.Height,
                Fill                = Brushes.Black,
                RenderTransform     = new TranslateTransform(fontRect.Left, fontRect.Top),
                Opacity             = 0,
                SnapsToDevicePixels = true
            };

            adornedElement.SizeChanged       += OnSizeChanged;
            adornedElement.SelectionChanged  += OnSelectionChanged;
            adornedElement.PreviewMouseDown  += OnPreviewMouseDown;
            adornedElement.GotKeyboardFocus  += OnGotFocus;
            adornedElement.LostKeyboardFocus += OnLostFocus;

            this.children = new VisualCollection(this);
            this.children.Add(this.caret);

            double   transitionTime = SystemInformation.CaretBlinkTime;
            TimeSpan fadeTime       = TimeSpan.FromMilliseconds(transitionTime * (1d / 3d));
            TimeSpan holdTime       = TimeSpan.FromMilliseconds(transitionTime * (2d / 3d));

            var storyboard = new Storyboard {
                RepeatBehavior = RepeatBehavior.Forever
            };

            var fade = new DoubleAnimationUsingKeyFrames {
                KeyFrames =
                {
                    new EasingDoubleKeyFrame(0,   KeyTime.FromTimeSpan(fadeTime)),
                    new DiscreteDoubleKeyFrame(0, KeyTime.FromTimeSpan(fadeTime + holdTime)),
                    new EasingDoubleKeyFrame(1,   KeyTime.FromTimeSpan(fadeTime + holdTime + fadeTime)),
                    new DiscreteDoubleKeyFrame(1, KeyTime.FromTimeSpan(fadeTime + holdTime + fadeTime + holdTime))
                },

                Duration = new Duration(TimeSpan.FromMilliseconds(transitionTime * 2))
            };

            Storyboard.SetTargetProperty(fade, new PropertyPath("Opacity"));
            fade.Freeze();

            storyboard.Children.Add(fade);
            this.blinkStoryboard = storyboard;
        }
コード例 #2
0
 ///for System.Windows.Controls.TextBox
 public static System.Windows.Point GetCaretPont(System.Windows.Controls.TextBox textBox)
 {
     return(textBox.GetRectFromCharacterIndex(textBox.SelectionStart).Location);
 }