程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> WPF初探 - 制作一個簡單的倒計時器

WPF初探 - 制作一個簡單的倒計時器

編輯:關於.NET

早上起來後閒的無事,於是想到前些日子學院的某個老師讓大家給他找個什麼倒計時的小軟件,當時大家忙於復習所以也懶得搭理這件事,囧~。既然早上沒事干,何不寫個玩玩~既然要寫,就用以前沒怎麼搗鼓過的WPF寫一個吧,也算是一次學習WPF的初探吧(感覺自己很落後了)!

在Vs2008和Vs2010之間徘徊了許久之後,最終還是選擇了Vs2008做開發IDE。在Vs2008中建了個WPF工程後,浏覽了下默認生成的工程文件結構,一個App.xaml(當然還有App.xaml.cs)和一個Windows1.xaml(Windows1.xaml.cs)。設計界面也和之前的Window Form程序大不一樣了,感覺和Flex差不多,還真是有點不怎麼習慣哦~

好了,開始做個簡單的倒計時器了。 先讓大家看下運行效果吧,顯示在屏幕正中央且置頂顯示:

由於比較簡單,就三個文件便寫完了,分別為界面設計的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 

鑒於代碼中注釋的比較詳細,所以筆者也不再一一贅述了,希望對大家能有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved