定时器写的闪光字

曾经研究wpf时写的闪光字,翻了出来,没太多技术含量,直接贴代码吧:

xaml文件:

<Window x:Class="Sparkling.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <TextBlock Width="450" Height="60" FontSize="36" Text="Welcome To Shanghai">
          <TextBlock.Foreground>
<!--渐变画刷-->
              <LinearGradientBrush x:Name="hua" StartPoint="0,0" EndPoint="1,0">
                  <GradientStop Color="Red" Offset="0"></GradientStop>
                   <GradientStop x:Name="ye" Color="Yellow" Offset="0.5"></GradientStop>
                   <GradientStop Color="Red" Offset="1"></GradientStop>
              </LinearGradientBrush>
          </TextBlock.Foreground>
        </TextBlock>

    </Grid>
</Window>

后台代码:

using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Sparkling
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        double number = 0.1;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(300);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();          //启动定时器
        }

        void timer_Tick(object sender, EventArgs e)
        {
            double dnumber = ye.Offset + number;
            if (dnumber > 1)
            {
                dnumber = 1;
            }
            if (dnumber < 0)
            {
                dnumber = 0;
            }
            ye.Offset = dnumber;
            if (ye.Offset >= 1 || ye.Offset <= 0)
            {
                number = -number;
            }
        }

    }
}

效果如下:

下载地址

posted on 2013-05-13 13:22  闪闪的幸运星  阅读(378)  评论(0编辑  收藏  举报