WPF Timer控制窗体颜色渐变

简介:

 本文主要写到System.Windows.Forms.Timer的四个属性,利用这四个属性,去不断改变窗体的颜色

案例:

 了解 Background 的值 “#1000” ,“#”后的第一位数字由小变大表示着本颜色由浅到深
 引用:System.Windows.Forms.dll       下载:http://download.csdn.net/tag/Timer.dll

源码:

------------------View

<Windowx:Class="Demo_Mvvm.Views.WindowView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WindowView" Height="300" Width="300">
    <Grid Background="{Binding Background}">
    </Grid>
</Window>

----------------ViewModel

using System;
using System.Windows;
using System.Threading;
using System.Collections.ObjectModel;
using SimpleMvvmToolkit;

namespace Demo_Mvvm.ViewModels
{
    public class WindowViewModel : ViewModelBase<WindowViewModel>
    {
        //引用Timer
        System.Windows.Forms.Timer CustomerSystemTimer;
        public WindowViewModel()
        {
            //实例Timer
            CustomerSystemTimer = new System.Windows.Forms.Timer();
            //设置时间
            CustomerSystemTimer.Interval = 1000;
            //事件的执行任务
            CustomerSystemTimer.Tick += new EventHandler(ChangeTopic);
            //开始执行
            CustomerSystemTimer.Start();
        }
        public void ChangeTopic(objectsender, EventArgs e)
        {
            //TimerStop
            CustomerSystemTimer.Stop();
            BackgroundWay();
            //TimerStart
            CustomerSystemTimer.Start();
        }
        public void BackgroundWay()
        {
            if (Background != null)
            {
                Background = (Convert.ToInt32((Background.Split('0')[0].Trim().ToString()).Split('#')[1].Trim().ToString()) - 9 == 0) ?"#1090" : "" + Background.Replace((Background.Split('0')[0].Trim().ToString()).Split('#')[1].Trim().ToString(),"" + (Convert.ToInt32((Background.Split('0')[0].Trim().ToString()).Split('#')[1].Trim().ToString()) + 1).ToString() +"") + "";
            }
        }
        private string  _background;
        public string  Background
        {
            get
            {
                if (_background == null)
                {
                    _background = "#1090";
                }
                return _background;
            }
            set
            {
                _background = value;
                NotifyPropertyChanged(x => x.Background);
            }
        }
    }
}

截图:







  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值