御风而行 2012-12-27
代码如下:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CountDown { /// <summary> /// 实现倒计时功能的类 /// </summary> public class ProcessCount { private Int32 _TotalSecond; public Int32 TotalSecond { get { return _TotalSecond; } set { _TotalSecond = value; } } /// <summary> /// 构造函数 /// </summary> public ProcessCount(Int32 totalSecond) { this._TotalSecond = totalSecond; } /// <summary> /// 减秒 /// </summary> /// <returns></returns> public bool ProcessCountDown() { if (_TotalSecond == 0) return false; else { _TotalSecond--; return true; } } /// <summary> /// 获取小时显示值 /// </summary> /// <returns></returns> public string GetHour() { return String.Format("{0:D2}", (_TotalSecond / 3600)); } /// <summary> /// 获取分钟显示值 /// </summary> /// <returns></returns> public string GetMinute() { return String.Format("{0:D2}", (_TotalSecond % 3600) / 60); } /// <summary> /// 获取秒显示值 /// </summary> /// <returns></returns> public string GetSecond() { return String.Format("{0:D2}", _TotalSecond % 60); } } }
代码如下:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 <Window x:Class="CountDown.MainWin" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="400" Width="800" HorizontalAlignment="Center" VerticalAlignment="Center" Title=" " Topmost="True" WindowStyle="None" Background="Transparent" AllowsTransparency="True" WindowStartupLocation="CenterScreen"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition Width="40"/> <ColumnDefinition /> <ColumnDefinition Width="40"/> <ColumnDefinition /> </Grid.ColumnDefinitions> <TextBlock Text="00" Name="HourArea" VerticalAlignment="Center" FontSize="180" Background="Red" Grid.Column="0"/> <TextBlock Text=":" Name="HourSplitMinute" VerticalAlignment="Center" FontSize="180" Background="Red" Grid.Column="1"/> <TextBlock Text="10" Name="MinuteArea" VerticalAlignment="Center" FontSize="180" Background="Red" Grid.Column="2" /> <TextBlock Text=":" Name="MinuteSplitSecond" VerticalAlignment="Center" FontSize="180" Background="Red" Grid.Column="3"/> <TextBlock Text="00" Name="SecondArea" VerticalAlignment="Center" FontSize="180" Background="Red" Grid.Column="4"/> </Grid> </Window>
代码如下:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Windows.Threading; namespace CountDown { /// <summary> /// Interaction logic for MainWin.xaml /// </summary> public partial class MainWin : Window { private DispatcherTimer timer; private ProcessCount processCount; public MainWin() { InitializeComponent(); this.Loaded += new RoutedEventHandler(MainWin_Loaded); } /// <summary> /// 窗口加载事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MainWin_Loaded(object sender, RoutedEventArgs e) { //设置定时器 timer = new DispatcherTimer(); timer.Interval = new TimeSpan(10000000); //时间间隔为一秒 timer.Tick += new EventHandler(timer_Tick); //转换成秒数 Int32 hour= Convert.ToInt32(HourArea.Text); Int32 minute = Convert.ToInt32(MinuteArea.Text); Int32 second = Convert.ToInt32(SecondArea.Text); //处理倒计时的类 processCount = new ProcessCount(hour*3600+minute*60+second); CountDown += new CountDownHandler(processCount.ProcessCountDown); //开启定时器 timer.Start(); } /// <summary> /// Timer触发的事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer_Tick(object sender, EventArgs e) { if (OnCountDown()) { HourArea.Text = processCount.GetHour(); MinuteArea.Text = processCount.GetMinute(); SecondArea.Text = processCount.GetSecond(); } else timer.Stop(); } /// <summary> /// 处理事件 /// </summary> public event CountDownHandler CountDown; public bool OnCountDown() { if (CountDown != null) return CountDown(); return false; } } /// <summary> /// 处理倒计时的委托 /// </summary> /// <returns></returns> public delegate bool CountDownHandler(); }
代码如下:
private int countSecond=300; //记录秒数 private void UserControl_Loaded(object sender, RoutedEventArgs e) { private DispatcherTimer disTimer = new DispatcherTimer(); disTimer.Interval = new TimeSpan(0, 0, 0, 1); //参数分别为:天,小时,分,秒。此方法有重载,可根据实际情况调用。 disTimer.Tick += new EventHandler(disTimer_Tick); //每一秒执行的方法 disTimer.Start(); } void disTimer_Tick(object sender, EventArgs e) { if(countSecond==0) { MessageBox.Show("结束"); } else { //判断lblSecond是否处于UI线程上 if (lblSecond.Dispatcher.CheckAccess()) { lblSecond.Content=countSecnd.ToString(); } else { lblSecond.Dispatcher.BeginInvoke(DispatcherPriority.Normal,(Action)(() =>{ lblSecond.Content=countSecond.ToString(); })); } countSecond--; } }
<!-- Row 0 -->. <TextBlock Text="{Binding Department, ElementName = window}". <!-- Row 1 -->. <TextB
<ListBox Name="sideMenu" SelectedIndex="{Binding MenuSelectedIndex}" ItemsSource="{Binding MenuList}