• 来瞧瞧,WPF 炫酷走马灯!


    🚀 优质资源分享 🚀

    学习路线指引(点击解锁)知识定位人群定位
    🧡 Python实战微信订餐小程序 🧡进阶级本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。
    💛Python量化交易实战💛入门级手把手带你打造一个易扩展、更安全、效率更高的量化交易系统

    来瞧瞧,WPF 炫酷走马灯!

    控件名:SpotLight

    作者:WPFDevelopersOrg

    原文链接: https://github.com/WPFDevelopersOrg/WPFDevelopers

    • 框架使用大于等于.NET40
    • Visual Studio 2022;
    • 项目使用 MIT 开源许可协议;
    • Canvas做容器方便针对文本TextBlock做裁剪Clip动画操作;
    • Canvas内部创建两个TextBlock
    • 第一个做为背景字体设置字体颜色为浅灰Foreground="#323232",也可以通过依赖属性设置DefaultForeground
    • 第二个字体设置会彩虹色当聚光灯走到某个区域后并显示;
    • Duration可设置动画的从左到右的时长,默认3秒;
    • 根据字体的实际宽度ActualWidth做动画展示从左到右并循环Forever播放;

    1)SpotLight.cs 代码如下;

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    
    namespace WPFDevelopers.Controls
    {
        [TemplatePart(Name = TextBlockBottomTemplateName, Type = typeof(TextBlock))]
        [TemplatePart(Name = TextBlockTopTemplateName, Type = typeof(TextBlock))]
        [TemplatePart(Name = EllipseGeometryTemplateName, Type = typeof(EllipseGeometry))]
        public class SpotLight : Control
        {
            private const string TextBlockBottomTemplateName = "PART\_TextBlockBottom";
            private const string TextBlockTopTemplateName = "PART\_TextBlockTop";
            private const string EllipseGeometryTemplateName = "PART\_EllipseGeometry";
    
            public static readonly DependencyProperty TextProperty =
                DependencyProperty.Register("Text", typeof(string), typeof(SpotLight),
                    new PropertyMetadata("WPFDevelopers"));
    
            public static readonly DependencyProperty DefaultForegroundProperty =
                DependencyProperty.Register("DefaultForeground", typeof(Brush), typeof(SpotLight),
                    new PropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#323232"))));
    
    
            public static readonly DependencyProperty DurationProperty =
                DependencyProperty.Register("Duration", typeof(TimeSpan), typeof(SpotLight),
                    new PropertyMetadata(TimeSpan.FromSeconds(3)));
    
    
            private EllipseGeometry _ellipseGeometry;
            private TextBlock _textBlockBottom, _textBlockTop;
    
            static SpotLight()
            {
                DefaultStyleKeyProperty.OverrideMetadata(typeof(SpotLight),
                    new FrameworkPropertyMetadata(typeof(SpotLight)));
            }
    
           
            public TimeSpan Duration
            {
                get => (TimeSpan)GetValue(DurationProperty);
                set => SetValue(DurationProperty, value);
            }
            public Brush DefaultForeground
            {
                get => (Brush)GetValue(DefaultForegroundProperty);
                set => SetValue(DefaultForegroundProperty, value);
            }
    
            public string Text
            {
                get => (string)GetValue(TextProperty);
                set => SetValue(TextProperty, value);
            }
            
            public override void OnApplyTemplate()
            {
                base.OnApplyTemplate();
                _textBlockBottom = GetTemplateChild(TextBlockBottomTemplateName) as TextBlock;
                _textBlockTop = GetTemplateChild(TextBlockTopTemplateName) as TextBlock;
    
                _ellipseGeometry = GetTemplateChild(EllipseGeometryTemplateName) as EllipseGeometry;
                var center = new Point(FontSize / 2, FontSize / 2);
                _ellipseGeometry.RadiusX = FontSize;
                _ellipseGeometry.RadiusY = FontSize;
                _ellipseGeometry.Center = center;
                if (_textBlockBottom != null && _textBlockTop != null && _ellipseGeometry != null)
                    _textBlockTop.Loaded += _textBlockTop_Loaded;
            }
    
    
            private void _textBlockTop_Loaded(object sender, RoutedEventArgs e)
            {
                var doubleAnimation = new DoubleAnimation
                {
                    From = 0,
                    To = _textBlockTop.ActualWidth,
                    Duration = Duration
                };
    
                Storyboard.SetTarget(doubleAnimation, _textBlockTop);
                Storyboard.SetTargetProperty(doubleAnimation,
                    new PropertyPath("(UIElement.Clip).(EllipseGeometry.Transform).(TranslateTransform.X)"));
                var storyboard = new Storyboard
                {
                    RepeatBehavior = RepeatBehavior.Forever,
                    AutoReverse = true
                };
                storyboard.Children.Add(doubleAnimation);
                storyboard.Begin();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97

    2)SpotLight.xaml 代码如下;

    
        
        
            
        ResourceDictionary.MergedDictionaries>
        
            
            
            
            
            
        LinearGradientBrush>