码迷,mamicode.com
首页 > 其他好文 > 详细

倒计时控件

时间:2014-09-19 13:34:25      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   使用   ar   

最近做一个WPF小项目需要使用到计时器,因此写了一个计时控件,记录下来,以便下次使用。

 

前台的XAML:

bubuko.com,布布扣
<UserControl x:Class="Test.CountDown"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="110" d:DesignWidth="150">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="43*" />
            <ColumnDefinition Width="13"/>
            <ColumnDefinition Width="43*" />
            <ColumnDefinition Width="13"/>
            <ColumnDefinition Width="43*" />
        </Grid.ColumnDefinitions>
        <TextBlock Text="00" Name="HourArea"  VerticalAlignment="Center" FontSize="30" Background="Transparent" Grid.Column="0" Foreground="DarkOrange" />
        <TextBlock Text=":" Name="HourSplitMinute"  VerticalAlignment="Center" FontSize="30" Background="Transparent" Grid.Column="1" Foreground="DarkOrange" />
        <TextBlock Text="00" Name="MinuteArea" VerticalAlignment="Center" FontSize="30" Background="Transparent" Grid.Column="2" Foreground="DarkOrange" />
        <TextBlock Text=":" Name="MinuteSplitSecond"  VerticalAlignment="Center" FontSize="30" Background="Transparent" Grid.Column="3" Foreground="DarkOrange" />
        <TextBlock Text="00"  Name="SecondArea" VerticalAlignment="Center" FontSize="30" Background="Transparent" Grid.Column="4" Foreground="DarkOrange" />
    </Grid>
</UserControl>
View Code

 

后台的逻辑:

bubuko.com,布布扣
public partial class CountDown : UserControl
    {
        public DispatcherTimer timer;
        public Process pro;
        public Stopwatch sw = new Stopwatch();
        public int seconds;

        public CountDown()
        {
            InitializeComponent();
            pro = new Process();

            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Tick += new EventHandler(timer_Tick);
        }

        void timer_Tick(object sender, EventArgs e)
        {
            TimeSpan ts = new TimeSpan (0,0,seconds);
            pro.totalSecond = (int)(ts - sw.Elapsed).TotalSeconds;
            if (pro .totalSecond > 0)
            {
                HourArea.Text = pro.GetHour();
                MinuteArea.Text = pro.GetMinute();
                SecondArea.Text = pro.GetSecond();
            }
            else
            {
                timer.Stop();
                sw.Stop ();
                sw.Reset();
                SecondArea.Text = string.Format("{0:D2}", 0);
            }
        }

    }

    public class Process
    {
        public int totalSecond;

        //获取小时字符串
        public string GetHour()
        {
            return string.Format("{0:D2}", totalSecond / 3600);
        }

        //获取分钟字符串
        public string GetMinute()
        {
            return string.Format("{0:D2}", (totalSecond / 60 - ((int)(totalSecond / 3600) * 60)));
        }

        //获取秒字符串
        public string GetSecond()
        {
            return string.Format("{0:D2}", totalSecond % 60);
        }
    }
View Code

 

调用:

this.countDown1.seconds = 300;//传入倒计时总时间(秒)
            this.countDown1.timer.Start();
            this.countDown1.sw.Start();

 

倒计时控件

标签:des   style   blog   http   color   io   os   使用   ar   

原文地址:http://www.cnblogs.com/ysyn/p/3981112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!