WPF制作一個(gè)簡單的倒計(jì)時(shí)器實(shí)例附源碼
更新時(shí)間:2012年12月27日 14:55:34 作者:
既然早上沒事干,于是想到前些日子學(xué)院的某個(gè)老師讓大家給他找個(gè)什么倒計(jì)時(shí)的小軟件;何不寫個(gè)玩玩~既然要寫,就用以前沒怎么搗鼓過的WPF寫一個(gè)倒計(jì)時(shí)器,需要了解的朋友可以參考下
實(shí)例一:
早上起來后閑的無事,于是想到前些日子學(xué)院的某個(gè)老師讓大家給他找個(gè)什么倒計(jì)時(shí)的小軟件,當(dāng)時(shí)大家忙于復(fù)習(xí)所以也懶得搭理這件事,囧~。既然早上沒事干,何不寫個(gè)玩玩~既然要寫,就用以前沒怎么搗鼓過的WPF寫一個(gè)吧,也算是一次學(xué)習(xí)WPF的初探吧(感覺自己很落后了)!
在Vs2008和Vs2010之間徘徊了許久之后,最終還是選擇了Vs2008做開發(fā)IDE。在Vs2008中建了個(gè)WPF工程后,瀏覽了下默認(rèn)生成的工程文件結(jié)構(gòu),一個(gè)App.xaml(當(dāng)然還有App.xaml.cs)和一個(gè)Windows1.xaml(Windows1.xaml.cs)。設(shè)計(jì)界面也和之前的Window Form程序大不一樣了,感覺和Flex差不多,不支持直接拖拽到指定位置的界面設(shè)計(jì)(在此感謝 cesium和 Muse為我指出問題所在),還真是有點(diǎn)不怎么習(xí)慣哦~
好了,開始做個(gè)簡單的倒計(jì)時(shí)器了。 先讓大家看下運(yùn)行效果吧,顯示在屏幕正中央且置頂顯示:
由于比較簡單,就三個(gè)文件便寫完了,分別為界面設(shè)計(jì)的MainWin.xaml和應(yīng)用程序類App.xaml 和倒計(jì)時(shí)處理類ProcessCount.cs類文件。代碼分別如下:
倒計(jì)時(shí)處理類ProcessCount.cs :
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>
/// 實(shí)現(xiàn)倒計(jì)時(shí)功能的類
/// </summary>
public class ProcessCount
{
private Int32 _TotalSecond;
public Int32 TotalSecond
{
get { return _TotalSecond; }
set { _TotalSecond = value; }
}
/// <summary>
/// 構(gòu)造函數(shù)
/// </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>
/// 獲取小時(shí)顯示值
/// </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);
}
}
}
窗口界面設(shè)計(jì)文件MainWin.xaml:
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>
窗口界面邏輯設(shè)計(jì)文件:MainWin.xaml.cs:
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)
{
//設(shè)置定時(shí)器
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(10000000); //時(shí)間間隔為一秒
timer.Tick += new EventHandler(timer_Tick);
//轉(zhuǎn)換成秒數(shù)
Int32 hour= Convert.ToInt32(HourArea.Text);
Int32 minute = Convert.ToInt32(MinuteArea.Text);
Int32 second = Convert.ToInt32(SecondArea.Text);
//處理倒計(jì)時(shí)的類
processCount = new ProcessCount(hour*3600+minute*60+second);
CountDown += new CountDownHandler(processCount.ProcessCountDown);
//開啟定時(shí)器
timer.Start();
}
/// <summary>
/// Timer觸發(fā)的事件
/// </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>
/// 處理倒計(jì)時(shí)的委托
/// </summary>
/// <returns></returns>
public delegate bool CountDownHandler();
}
鑒于代碼中注釋的比較詳細(xì),所以筆者也不再一一贅述了,希望對(duì)大家能有所幫助。完整的工程包下載:http://xiazai.jb51.net/201212/yuanma/CountDown_jb51.rar。
實(shí)例二:
效果:
UI:放置一個(gè)Label ---><Label Name="lblSecond" FontSize="20" Foreground="Red" ></Label>
CS:
private int countSecond=300; //記錄秒數(shù)
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
private DispatcherTimer disTimer = new DispatcherTimer();
disTimer.Interval = new TimeSpan(0, 0, 0, 1); //參數(shù)分別為:天,小時(shí),分,秒。此方法有重載,可根據(jù)實(shí)際情況調(diào)用。
disTimer.Tick += new EventHandler(disTimer_Tick); //每一秒執(zhí)行的方法
disTimer.Start();
}
void disTimer_Tick(object sender, EventArgs e)
{
if(countSecond==0)
{
MessageBox.Show("結(jié)束");
}
else
{
//判斷l(xiāng)blSecond是否處于UI線程上
if (lblSecond.Dispatcher.CheckAccess())
{
lblSecond.Content=countSecnd.ToString();
}
else
{
lblSecond.Dispatcher.BeginInvoke(DispatcherPriority.Normal,(Action)(() =>{
lblSecond.Content=countSecond.ToString();
}));
}
countSecond--;
}
}
早上起來后閑的無事,于是想到前些日子學(xué)院的某個(gè)老師讓大家給他找個(gè)什么倒計(jì)時(shí)的小軟件,當(dāng)時(shí)大家忙于復(fù)習(xí)所以也懶得搭理這件事,囧~。既然早上沒事干,何不寫個(gè)玩玩~既然要寫,就用以前沒怎么搗鼓過的WPF寫一個(gè)吧,也算是一次學(xué)習(xí)WPF的初探吧(感覺自己很落后了)!
在Vs2008和Vs2010之間徘徊了許久之后,最終還是選擇了Vs2008做開發(fā)IDE。在Vs2008中建了個(gè)WPF工程后,瀏覽了下默認(rèn)生成的工程文件結(jié)構(gòu),一個(gè)App.xaml(當(dāng)然還有App.xaml.cs)和一個(gè)Windows1.xaml(Windows1.xaml.cs)。設(shè)計(jì)界面也和之前的Window Form程序大不一樣了,感覺和Flex差不多,不支持直接拖拽到指定位置的界面設(shè)計(jì)(在此感謝 cesium和 Muse為我指出問題所在),還真是有點(diǎn)不怎么習(xí)慣哦~
好了,開始做個(gè)簡單的倒計(jì)時(shí)器了。 先讓大家看下運(yùn)行效果吧,顯示在屏幕正中央且置頂顯示:
由于比較簡單,就三個(gè)文件便寫完了,分別為界面設(shè)計(jì)的MainWin.xaml和應(yīng)用程序類App.xaml 和倒計(jì)時(shí)處理類ProcessCount.cs類文件。代碼分別如下:
倒計(jì)時(shí)處理類ProcessCount.cs :
復(fù)制代碼 代碼如下:
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>
/// 實(shí)現(xiàn)倒計(jì)時(shí)功能的類
/// </summary>
public class ProcessCount
{
private Int32 _TotalSecond;
public Int32 TotalSecond
{
get { return _TotalSecond; }
set { _TotalSecond = value; }
}
/// <summary>
/// 構(gòu)造函數(shù)
/// </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>
/// 獲取小時(shí)顯示值
/// </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);
}
}
}
窗口界面設(shè)計(jì)文件MainWin.xaml:
復(fù)制代碼 代碼如下:
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>
窗口界面邏輯設(shè)計(jì)文件:MainWin.xaml.cs:
復(fù)制代碼 代碼如下:
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)
{
//設(shè)置定時(shí)器
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(10000000); //時(shí)間間隔為一秒
timer.Tick += new EventHandler(timer_Tick);
//轉(zhuǎn)換成秒數(shù)
Int32 hour= Convert.ToInt32(HourArea.Text);
Int32 minute = Convert.ToInt32(MinuteArea.Text);
Int32 second = Convert.ToInt32(SecondArea.Text);
//處理倒計(jì)時(shí)的類
processCount = new ProcessCount(hour*3600+minute*60+second);
CountDown += new CountDownHandler(processCount.ProcessCountDown);
//開啟定時(shí)器
timer.Start();
}
/// <summary>
/// Timer觸發(fā)的事件
/// </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>
/// 處理倒計(jì)時(shí)的委托
/// </summary>
/// <returns></returns>
public delegate bool CountDownHandler();
}
鑒于代碼中注釋的比較詳細(xì),所以筆者也不再一一贅述了,希望對(duì)大家能有所幫助。完整的工程包下載:http://xiazai.jb51.net/201212/yuanma/CountDown_jb51.rar。
實(shí)例二:
效果:
UI:放置一個(gè)Label ---><Label Name="lblSecond" FontSize="20" Foreground="Red" ></Label>
CS:
復(fù)制代碼 代碼如下:
private int countSecond=300; //記錄秒數(shù)
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
private DispatcherTimer disTimer = new DispatcherTimer();
disTimer.Interval = new TimeSpan(0, 0, 0, 1); //參數(shù)分別為:天,小時(shí),分,秒。此方法有重載,可根據(jù)實(shí)際情況調(diào)用。
disTimer.Tick += new EventHandler(disTimer_Tick); //每一秒執(zhí)行的方法
disTimer.Start();
}
void disTimer_Tick(object sender, EventArgs e)
{
if(countSecond==0)
{
MessageBox.Show("結(jié)束");
}
else
{
//判斷l(xiāng)blSecond是否處于UI線程上
if (lblSecond.Dispatcher.CheckAccess())
{
lblSecond.Content=countSecnd.ToString();
}
else
{
lblSecond.Dispatcher.BeginInvoke(DispatcherPriority.Normal,(Action)(() =>{
lblSecond.Content=countSecond.ToString();
}));
}
countSecond--;
}
}
您可能感興趣的文章:
相關(guān)文章
.Net Core中自定義認(rèn)證實(shí)現(xiàn)
本文主要介紹了.Net Core中自定義認(rèn)證實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
.NET?CPU爆高事故事故分析某供應(yīng)鏈WEB網(wǎng)站
這篇文章主要為大家介紹了.NET?CPU爆高事故事故分析某供應(yīng)鏈WEB網(wǎng)站,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
.net使用jwt進(jìn)行身份認(rèn)證的流程記錄
這篇文章主要給大家介紹了關(guān)于.net使用jwt進(jìn)行身份認(rèn)證的相關(guān)資料,JWT是Auth0提出的通過對(duì)JSON進(jìn)行加密簽名來實(shí)現(xiàn)授權(quán)驗(yàn)證的方案,本文通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09
asp.net 模擬提交有文件上傳的表單(通過http模擬上傳文件)
通過HTTP模擬GET或POST請(qǐng)求,提交數(shù)據(jù)到服務(wù)端獲取響應(yīng),比較常見些;但如上傳文件到服務(wù)端,使用html form當(dāng)然簡單了,而因環(huán)境所限有時(shí)需要使用模擬方法去提交有附件(文件上傳)的表單。2010-02-02
在ASP.NET Core5.0中訪問HttpContext的方法步驟
這篇文章主要介紹了在ASP.NET Core5.0中訪問HttpContext的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
輕量級(jí)ORM框架Dapper應(yīng)用支持操作函數(shù)和事物
這篇文章介紹了Dapper支持操作函數(shù)和事物的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
.NET使用System.Timers.Timer類實(shí)現(xiàn)程序定時(shí)執(zhí)行
這篇文章介紹了.NET使用System.Timers.Timer類實(shí)現(xiàn)程序定時(shí)執(zhí)行的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07

