来瞧瞧,WPF 炫酷走马灯!

打印 上一主题 下一主题

主题 848|帖子 848|积分 2546

来瞧瞧,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 代码如下;
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. using System.Windows.Media.Animation;
  6. namespace WPFDevelopers.Controls
  7. {
  8.     [TemplatePart(Name = TextBlockBottomTemplateName, Type = typeof(TextBlock))]
  9.     [TemplatePart(Name = TextBlockTopTemplateName, Type = typeof(TextBlock))]
  10.     [TemplatePart(Name = EllipseGeometryTemplateName, Type = typeof(EllipseGeometry))]
  11.     public class SpotLight : Control
  12.     {
  13.         private const string TextBlockBottomTemplateName = "PART_TextBlockBottom";
  14.         private const string TextBlockTopTemplateName = "PART_TextBlockTop";
  15.         private const string EllipseGeometryTemplateName = "PART_EllipseGeometry";
  16.         public static readonly DependencyProperty TextProperty =
  17.             DependencyProperty.Register("Text", typeof(string), typeof(SpotLight),
  18. <UniformGrid Rows="2" Background="#222222">
  19.         <wpfdev:SpotLight FontSize="50" Text="YanJinHua"
  20.                           DefaultForeground="Crimson"
  21.                           Foreground="Fuchsia"
  22.                           Duration="00:00:05" />
  23.         <wpfdev:SpotLight/>
  24. </UniformGrid>new PropertyMetadata("WPFDevelopers"));
  25.         public static readonly DependencyProperty DefaultForegroundProperty =
  26.             DependencyProperty.Register("DefaultForeground", typeof(Brush), typeof(SpotLight),
  27. <UniformGrid Rows="2" Background="#222222">
  28.         <wpfdev:SpotLight FontSize="50" Text="YanJinHua"
  29.                           DefaultForeground="Crimson"
  30.                           Foreground="Fuchsia"
  31.                           Duration="00:00:05" />
  32.         <wpfdev:SpotLight/>
  33. </UniformGrid>new PropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#323232"))));
  34.         public static readonly DependencyProperty DurationProperty =
  35.             DependencyProperty.Register("Duration", typeof(TimeSpan), typeof(SpotLight),
  36. <UniformGrid Rows="2" Background="#222222">
  37.         <wpfdev:SpotLight FontSize="50" Text="YanJinHua"
  38.                           DefaultForeground="Crimson"
  39.                           Foreground="Fuchsia"
  40.                           Duration="00:00:05" />
  41.         <wpfdev:SpotLight/>
  42. </UniformGrid>new PropertyMetadata(TimeSpan.FromSeconds(3)));
  43.         private EllipseGeometry _ellipseGeometry;
  44.         private TextBlock _textBlockBottom, _textBlockTop;
  45.         static SpotLight()
  46.         {
  47.             DefaultStyleKeyProperty.OverrideMetadata(typeof(SpotLight),
  48. <UniformGrid Rows="2" Background="#222222">
  49.         <wpfdev:SpotLight FontSize="50" Text="YanJinHua"
  50.                           DefaultForeground="Crimson"
  51.                           Foreground="Fuchsia"
  52.                           Duration="00:00:05" />
  53.         <wpfdev:SpotLight/>
  54. </UniformGrid>new FrameworkPropertyMetadata(typeof(SpotLight)));
  55.         }
  56.       
  57.         public TimeSpan Duration
  58.         {
  59.             get => (TimeSpan)GetValue(DurationProperty);
  60.             set => SetValue(DurationProperty, value);
  61.         }
  62.         public Brush DefaultForeground
  63.         {
  64.             get => (Brush)GetValue(DefaultForegroundProperty);
  65.             set => SetValue(DefaultForegroundProperty, value);
  66.         }
  67.         public string Text
  68.         {
  69.             get => (string)GetValue(TextProperty);
  70.             set => SetValue(TextProperty, value);
  71.         }
  72.         
  73.         public override void OnApplyTemplate()
  74.         {
  75.             base.OnApplyTemplate();
  76.             _textBlockBottom = GetTemplateChild(TextBlockBottomTemplateName) as TextBlock;
  77.             _textBlockTop = GetTemplateChild(TextBlockTopTemplateName) as TextBlock;
  78.             _ellipseGeometry = GetTemplateChild(EllipseGeometryTemplateName) as EllipseGeometry;
  79.             var center = new Point(FontSize / 2, FontSize / 2);
  80.             _ellipseGeometry.RadiusX = FontSize;
  81.             _ellipseGeometry.RadiusY = FontSize;
  82.             _ellipseGeometry.Center = center;
  83.             if (_textBlockBottom != null && _textBlockTop != null && _ellipseGeometry != null)
  84. <UniformGrid Rows="2" Background="#222222">
  85.         <wpfdev:SpotLight FontSize="50" Text="YanJinHua"
  86.                           DefaultForeground="Crimson"
  87.                           Foreground="Fuchsia"
  88.                           Duration="00:00:05" />
  89.         <wpfdev:SpotLight/>
  90. </UniformGrid>_textBlockTop.Loaded += _textBlockTop_Loaded;
  91.         }
  92.         private void _textBlockTop_Loaded(object sender, RoutedEventArgs e)
  93.         {
  94.             var doubleAnimation = new DoubleAnimation
  95.             {
  96. <UniformGrid Rows="2" Background="#222222">
  97.         <wpfdev:SpotLight FontSize="50" Text="YanJinHua"
  98.                           DefaultForeground="Crimson"
  99.                           Foreground="Fuchsia"
  100.                           Duration="00:00:05" />
  101.         <wpfdev:SpotLight/>
  102. </UniformGrid>From = 0,
  103. <UniformGrid Rows="2" Background="#222222">
  104.         <wpfdev:SpotLight FontSize="50" Text="YanJinHua"
  105.                           DefaultForeground="Crimson"
  106.                           Foreground="Fuchsia"
  107.                           Duration="00:00:05" />
  108.         <wpfdev:SpotLight/>
  109. </UniformGrid>To = _textBlockTop.ActualWidth,
  110. <UniformGrid Rows="2" Background="#222222">
  111.         <wpfdev:SpotLight FontSize="50" Text="YanJinHua"
  112.                           DefaultForeground="Crimson"
  113.                           Foreground="Fuchsia"
  114.                           Duration="00:00:05" />
  115.         <wpfdev:SpotLight/>
  116. </UniformGrid>Duration = Duration
  117.             };
  118.             Storyboard.SetTarget(doubleAnimation, _textBlockTop);
  119.             Storyboard.SetTargetProperty(doubleAnimation,
  120. <UniformGrid Rows="2" Background="#222222">
  121.         <wpfdev:SpotLight FontSize="50" Text="YanJinHua"
  122.                           DefaultForeground="Crimson"
  123.                           Foreground="Fuchsia"
  124.                           Duration="00:00:05" />
  125.         <wpfdev:SpotLight/>
  126. </UniformGrid>new PropertyPath("(UIElement.Clip).(EllipseGeometry.Transform).(TranslateTransform.X)"));
  127.             var storyboard = new Storyboard
  128.             {
  129. <UniformGrid Rows="2" Background="#222222">
  130.         <wpfdev:SpotLight FontSize="50" Text="YanJinHua"
  131.                           DefaultForeground="Crimson"
  132.                           Foreground="Fuchsia"
  133.                           Duration="00:00:05" />
  134.         <wpfdev:SpotLight/>
  135. </UniformGrid>RepeatBehavior = RepeatBehavior.Forever,
  136. <UniformGrid Rows="2" Background="#222222">
  137.         <wpfdev:SpotLight FontSize="50" Text="YanJinHua"
  138.                           DefaultForeground="Crimson"
  139.                           Foreground="Fuchsia"
  140.                           Duration="00:00:05" />
  141.         <wpfdev:SpotLight/>
  142. </UniformGrid>AutoReverse = true
  143.             };
  144.             storyboard.Children.Add(doubleAnimation);
  145.             storyboard.Begin();
  146.         }
  147.     }
  148. }
复制代码
2)SpotLight.xaml 代码如下;
  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2. <UniformGrid Rows="2" Background="#222222">
  3.         <wpfdev:SpotLight FontSize="50" Text="YanJinHua"
  4.                           DefaultForeground="Crimson"
  5.                           Foreground="Fuchsia"
  6.                           Duration="00:00:05" />
  7.         <wpfdev:SpotLight/>
  8. </UniformGrid>    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  9. <UniformGrid Rows="2" Background="#222222">
  10.         <wpfdev:SpotLight FontSize="50" Text="YanJinHua"
  11.                           DefaultForeground="Crimson"
  12.                           Foreground="Fuchsia"
  13.                           Duration="00:00:05" />
  14.         <wpfdev:SpotLight/>
  15. </UniformGrid>    xmlns:controls="clr-namespace:WPFDevelopers.Controls">
  16.    
  17.     <ResourceDictionary.MergedDictionaries>
  18.         <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
  19.     </ResourceDictionary.MergedDictionaries>
  20.     <LinearGradientBrush x:Key="RainbowBrush" EndPoint="1,1" MappingMode="RelativeToBoundingBox" StartPoint="0,0">
  21.         <GradientStop Color="#FF9C1031" Offset="0.1"/>
  22.         <GradientStop Color="#FFBE0E20" Offset="0.2"/>
  23.         <GradientStop Color="#FF9C12AC" Offset="0.7"/>
  24.         <GradientStop Color="#FF0A8DC3" Offset="0.8"/>
  25.         <GradientStop Color="#FF1AEBCC" Offset="1"/>
  26.     </LinearGradientBrush>
  27.    
  28. </ResourceDictionary>
复制代码
3)SpotLightExample.xaml代码如下如何使用
  1. <UniformGrid Rows="2" Background="#222222">
  2.         <wpfdev:SpotLight FontSize="50" Text="YanJinHua"
  3.                           DefaultForeground="Crimson"
  4.                           Foreground="Fuchsia"
  5.                           Duration="00:00:05" />
  6.         <wpfdev:SpotLight/>
  7. </UniformGrid>
复制代码

SpotLight.cs|Github
SpotLight.cs|码云
SpotLight.xaml|Github
SpotLight.xaml|码云

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

祗疼妳一个

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表