WPF制作一个简单的倒计时器实例附源码
程序员文章站
2024-03-06 08:40:19
实例一: 早上起来后闲的无事,于是想到前些日子学院的某个老师让大家给他找个什么倒计时的小软件,当时大家忙于复习所以也懒得搭理这件事,囧~。既然早上没事干,何不写个玩玩~既然...
实例一:
早上起来后闲的无事,于是想到前些日子学院的某个老师让大家给他找个什么倒计时的小软件,当时大家忙于复习所以也懒得搭理这件事,囧~。既然早上没事干,何不写个玩玩~既然要写,就用以前没怎么捣鼓过的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 :
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);
}
}
}
窗口界面设计文件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>
窗口界面逻辑设计文件: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)
{
//设置定时器
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();
}
鉴于代码中注释的比较详细,所以笔者也不再一一赘述了,希望对大家能有所帮助。完整的工程包下载:http://xiazai.jb51.net/201212/yuanma/countdown_jb51.rar。
实例二:
效果:
ui:放置一个label ---><label name="lblsecond" fontsize="20" foreground="red" ></label>
cs:
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--;
}
}
早上起来后闲的无事,于是想到前些日子学院的某个老师让大家给他找个什么倒计时的小软件,当时大家忙于复习所以也懒得搭理这件事,囧~。既然早上没事干,何不写个玩玩~既然要写,就用以前没怎么捣鼓过的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 :
复制代码 代码如下:
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);
}
}
}
窗口界面设计文件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>
窗口界面逻辑设计文件: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)
{
//设置定时器
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();
}
鉴于代码中注释的比较详细,所以笔者也不再一一赘述了,希望对大家能有所帮助。完整的工程包下载:http://xiazai.jb51.net/201212/yuanma/countdown_jb51.rar。
实例二:
效果:
ui:放置一个label ---><label name="lblsecond" fontsize="20" foreground="red" ></label>
cs:
复制代码 代码如下:
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--;
}
}
上一篇: Android自定义View仿支付宝芝麻信用分仪表盘
下一篇: 自制网页选取本地路径控件(附源码)