一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - ASP.NET教程 - WPF制作一個(gè)簡(jiǎn)單的倒計(jì)時(shí)器實(shí)例附源碼

WPF制作一個(gè)簡(jiǎn)單的倒計(jì)時(shí)器實(shí)例附源碼

2019-10-14 11:50asp.net教程網(wǎng) ASP.NET教程

既然早上沒(méi)事干,于是想到前些日子學(xué)院的某個(gè)老師讓大家給他找個(gè)什么倒計(jì)時(shí)的小軟件;何不寫(xiě)個(gè)玩玩~既然要寫(xiě),就用以前沒(méi)怎么搗鼓過(guò)的WPF寫(xiě)一個(gè)倒計(jì)時(shí)器,需要了解的朋友可以參考下

實(shí)例一: 
早上起來(lái)后閑的無(wú)事,于是想到前些日子學(xué)院的某個(gè)老師讓大家給他找個(gè)什么倒計(jì)時(shí)的小軟件,當(dāng)時(shí)大家忙于復(fù)習(xí)所以也懶得搭理這件事,囧~。既然早上沒(méi)事干,何不寫(xiě)個(gè)玩玩~既然要寫(xiě),就用以前沒(méi)怎么搗鼓過(guò)的WPF寫(xiě)一個(gè)吧,也算是一次學(xué)習(xí)WPF的初探吧(感覺(jué)自己很落后了)! 

在Vs2008和Vs2010之間徘徊了許久之后,最終還是選擇了Vs2008做開(kāi)發(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程序大不一樣了,感覺(jué)和Flex差不多,不支持直接拖拽到指定位置的界面設(shè)計(jì)(在此感謝 cesium和 Muse為我指出問(wèn)題所在),還真是有點(diǎn)不怎么習(xí)慣哦~ 
好了,開(kāi)始做個(gè)簡(jiǎn)單的倒計(jì)時(shí)器了。 先讓大家看下運(yùn)行效果吧,顯示在屏幕正中央且置頂顯示: 
WPF制作一個(gè)簡(jiǎn)單的倒計(jì)時(shí)器實(shí)例附源碼 
由于比較簡(jiǎn)單,就三個(gè)文件便寫(xiě)完了,分別為界面設(shè)計(jì)的MainWin.xaml和應(yīng)用程序類(lèi)App.xaml 和倒計(jì)時(shí)處理類(lèi)ProcessCount.cs類(lèi)文件。代碼分別如下: 
倒計(jì)時(shí)處理類(lèi)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í)功能的類(lèi) 
/// </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í)的類(lèi) 
processCount = new ProcessCount(hour*3600+minute*60+second); 
CountDown += new CountDownHandler(processCount.ProcessCountDown); 
//開(kāi)啟定時(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ì)大家能有所幫助。

實(shí)例二
效果:
WPF制作一個(gè)簡(jiǎn)單的倒計(jì)時(shí)器實(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--; 
    } 
  } 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 性色生活片在线观看 | 久久青草费线频观看国产 | 亚洲v日韩v欧美在线观看 | 美女gif趴跪式抽搐动态图 | 欧美草逼网站 | 高清视频在线观看+免费 | 无码人妻99久久密AV | boobsmilking流奶水野战 | 亚洲gogo人体大胆西西安徽 | 亚洲 欧美 偷自乱 图片 | 99热这里有免费国产精品 | 日产精品卡一卡2卡三卡乱码工厂 | 国产一区二区视频免费 | 亚洲天堂视频在线观看 | 国产精品高清一区二区三区 | 欧美一二区视频 | 大桥未久midd—962在线 | 亚洲一区二区三区久久精品 | 色四虎 | 美女女女女女女bbbbbb毛片 | 欧美色青| 国产精品第页 | 免费jizz在在线播放国产 | 双性人bbww欧美双性 | 免费高清在线视频色yeye | 日韩精品免费看 | 国产成人综合久久精品红 | 欧美高清3dfreexxxx性 | 俄罗斯freeⅹ性欧美 | 日本人成大片在线 | 五月桃花网婷婷亚洲综合 | ysl蜜桃色成人麻豆 youwu在线影院 | 范冰冰特黄xx大片 | 男人狂躁女人下面的视频免费 | 欧美一级特黄特色大片免费 | 大胸孕妇孕交pregnantsex 大象视频污 | 久久电影院久久国产 | 91视频免费观看网站 | 视频在线观看国产 | 欧美a一级片 | 性夜a爽黄爽 |