WPF初探——制作一个简单的倒计时器

     早上起来后闲的无事,于是想到前些日子学院的某个老师让大家给他找个什么倒计时的小软件,当时大家忙于复习所以也懒得搭理这件事,囧~。既然早上没事干,何不写个玩玩~既然要写,就用以前没怎么捣鼓过的WPF写一个吧,也算是一次学习WPF的初探吧(感觉自己很落后了)!

     在Vs2008和Vs2010之间徘徊了许久之后,最终还是选择了Vs2008做开发IDE。在Vs2008中建了个WPF工程后,浏览了下默认生成的工程文件结构,一个App.xaml(当然还有App.xaml.cs)和一个Windows1.xaml(Windows1.xaml.cs)。设计界面也和之前的Window Form程序大不一样了,感觉和Flex差不多,不支持直接拖拽到指定位置的界面设计在此感谢 cesium和 Muse为我指出问题所在),还真是有点不怎么习惯哦~

     好了,开始做个简单的倒计时器了。 先让大家看下运行效果吧,显示在屏幕正中央且置顶显示:

     

 

     由于比较简单,就三个文件便写完了,分别为界面设计的MainWin.xaml和应用程序类App.xaml 和倒计时处理类ProcessCount.cs类文件。代码分别如下:

     倒计时处理类ProcessCount.cs :

     

 1  using  System;
 2  using  System.Collections.Generic;
 3  using  System.Linq;
 4  using  System.Text;
 5 
 6  namespace  CountDown
 7  {   
 8 
 9       ///   <summary>
10       ///  实现倒计时功能的类
11       ///   </summary>
12       public   class  ProcessCount
13      {
14           private  Int32 _TotalSecond;
15           public  Int32 TotalSecond
16          {
17               get  {  return  _TotalSecond; }
18               set  { _TotalSecond  =  value; }
19          }
20         
21           ///   <summary>
22           ///  构造函数
23           ///   </summary>
24           public  ProcessCount(Int32 totalSecond)
25          {
26               this ._TotalSecond  =  totalSecond;
27          }
28 
29           ///   <summary>
30           ///  减秒
31           ///   </summary>
32           ///   <returns></returns>
33           public   bool  ProcessCountDown()
34          {
35               if  (_TotalSecond  ==   0 )
36                   return   false ;
37               else
38              {
39                  _TotalSecond -- ;
40                   return   true ;
41              }
42          }
43 
44           ///   <summary>
45           ///  获取小时显示值
46           ///   </summary>
47           ///   <returns></returns>
48           public   string  GetHour()
49          {
50               return  String.Format( " {0:D2} " , (_TotalSecond  /   3600 ));
51          }
52 
53           ///   <summary>
54           ///  获取分钟显示值
55           ///   </summary>
56           ///   <returns></returns>
57           public   string  GetMinute()
58          {
59               return  String.Format( " {0:D2} " , (_TotalSecond  %   3600 /   60 ); 
60          }
61 
62           ///   <summary>
63           ///  获取秒显示值
64           ///   </summary>
65           ///   <returns></returns>
66           public   string  GetSecond()
67          {
68               return  String.Format( " {0:D2} " , _TotalSecond  %   60 );
69          }
70        
71      }
72  }
73 

 

 

  

     窗口界面设计文件MainWin.xaml:

     

 

 1  < Window x:Class = " CountDown.MainWin "
 2      xmlns = " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
 3      xmlns:x = " http://schemas.microsoft.com/winfx/2006/xaml "  Height = " 400 "  Width = " 800 "  HorizontalAlignment = " Center "  VerticalAlignment = " Center "
 4      Title = "   "  Topmost = " True "  WindowStyle = " None "  Background = " Transparent "  AllowsTransparency = " True "  WindowStartupLocation = " CenterScreen " >
 5       < Grid >
 6          
 7           < Grid.ColumnDefinitions >
 8               < ColumnDefinition  />
 9               < ColumnDefinition Width = " 40 " />
10               < ColumnDefinition  />
11               < ColumnDefinition Width = " 40 " />
12               < ColumnDefinition  />
13           </ Grid.ColumnDefinitions >
14         
15           < TextBlock Text = " 00 "  Name = " HourArea "    VerticalAlignment = " Center "  FontSize = " 180 "  Background = " Red "  Grid.Column = " 0 " />
16           < TextBlock Text = " : "  Name = " HourSplitMinute "    VerticalAlignment = " Center "  FontSize = " 180 "  Background = " Red "  Grid.Column = " 1 " />         
17           < TextBlock Text = " 10 "  Name = " MinuteArea "  VerticalAlignment = " Center "  FontSize = " 180 "  Background = " Red "  Grid.Column = " 2 "   />
18           < TextBlock Text = " : "  Name = " MinuteSplitSecond "    VerticalAlignment = " Center "  FontSize = " 180 "  Background = " Red "  Grid.Column = " 3 " />         
19           < TextBlock Text = " 00 "   Name = " SecondArea "  VerticalAlignment = " Center "  FontSize = " 180 "  Background = " Red "  Grid.Column = " 4 " />  
20          
21       </ Grid >
22  </ Window >
23 

 

 

 

     窗口界面逻辑设计文件:MainWin.xaml.cs:

     

 

 1  using  System;
 2  using  System.Collections.Generic;
 3  using  System.Linq;
 4  using  System.Text;
 5  using  System.Windows;
 6  using  System.Windows.Controls;
 7  using  System.Windows.Data;
 8  using  System.Windows.Documents;
 9  using  System.Windows.Input;
10  using  System.Windows.Media;
11  using  System.Windows.Media.Imaging;
12  using  System.Windows.Shapes;
13  using  System.Windows.Threading;
14 
15  namespace  CountDown
16 
17       ///   <summary>
18       ///  Interaction logic for MainWin.xaml
19       ///   </summary>
20       public   partial   class  MainWin : Window
21      {
22           private  DispatcherTimer timer;
23 
24           private  ProcessCount processCount;
25 
26           public  MainWin()
27          {
28              InitializeComponent();
29 
30               this .Loaded  +=   new  RoutedEventHandler(MainWin_Loaded);
31          }
32 
33           ///   <summary>
34           ///  窗口加载事件
35           ///   </summary>
36           ///   <param name="sender"></param>
37           ///   <param name="e"></param>
38           private   void  MainWin_Loaded( object  sender, RoutedEventArgs e)
39          {
40               // 设置定时器
41              timer  =   new  DispatcherTimer();
42              timer.Interval  =   new  TimeSpan( 10000000 );    // 时间间隔为一秒
43              timer.Tick  +=   new  EventHandler(timer_Tick);
44 
45               // 转换成秒数
46              Int32 hour =  Convert.ToInt32(HourArea.Text);
47              Int32 minute  =  Convert.ToInt32(MinuteArea.Text);
48              Int32 second  =  Convert.ToInt32(SecondArea.Text);
49 
50               // 处理倒计时的类
51              processCount  =   new  ProcessCount(hour * 3600 + minute * 60 + second);
52              CountDown  +=   new  CountDownHandler(processCount.ProcessCountDown);
53 
54               // 开启定时器
55              timer.Start();
56          }
57        
58 
59           ///   <summary>
60           ///  Timer触发的事件
61           ///   </summary>
62           ///   <param name="sender"></param>
63           ///   <param name="e"></param>
64           private   void  timer_Tick( object  sender, EventArgs e)
65          {
66               if  (OnCountDown())
67              {
68                  HourArea.Text  =  processCount.GetHour();
69                  MinuteArea.Text  =  processCount.GetMinute();
70                  SecondArea.Text  =  processCount.GetSecond();
71              }
72               else
73                  timer.Stop();
74          }
75 
76           ///   <summary>
77           ///  处理事件
78           ///   </summary>
79           public   event  CountDownHandler CountDown;
80           public   bool  OnCountDown()
81          {
82               if  (CountDown  !=   null )
83                  return  CountDown();
84 
85               return   false ;
86          }
87      }
88 
89       ///   <summary>
90       ///  处理倒计时的委托
91       ///   </summary>
92       ///   <returns></returns>
93       public   delegate   bool  CountDownHandler();
94  }
95 

 

 

 

     鉴于代码中注释的比较详细,所以笔者也不再一一赘述了,希望对大家能有所帮助。完整的工程包下载:/Files/royenhome/CountDown.rar

      

     

     

   

     

 

 

 

转载于:https://www.cnblogs.com/royenhome/archive/2010/01/24/1655168.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值