WPF makes a simple countdown instance with source code _ Practical Tips

Source: Internet
Author: User
Tags time interval
instance One
Wake up in the morning after nothing, so think of some days before the college teacher let everyone give him to find what the countdown of small software, then we are busy review so also don't bother to take this matter, embarrassed ~. Since there is nothing to do in the morning, why not write a play ~ since you want to write, you can write a WPF in a way that has not been done in the past, and it is a preliminary study of WPF (feeling very backward)!

After wandering between Vs2008 and Vs2010 for a long time, finally chose the Vs2008 to do the development of the IDE. After building a WPF project in Vs2008, we browsed through the default generated engineering file structure, a App.xaml (App.xaml.cs) and a Windows1.xaml (Windows1.xaml.cs). Design interface and the previous window form program is very different, feeling and flex similar, do not support the direct drag and drop to the specified location of the interface design (thank cesium and muse for me to point out the problem), but also a little bit not accustomed to OH ~
All right, start with a simple countdown timer. Let us first look at the running effect bar, displayed in the center of the screen and the top display:

Because it is relatively simple, three files will be written, respectively, for the interface design Mainwin.xaml and application class App.xaml and Countdown processing class ProcessCount.cs class files. The code is as follows:
countdown Processing Class ProcessCount.cs:
Copy Code code as follows:

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>
The class that implements the Countdown function
</summary>
public class Processcount
{
Private Int32 _totalsecond;
Public Int32 Totalsecond
{
get {return _totalsecond;}
set {_totalsecond = value;}
}
<summary>
Constructors
</summary>
Public Processcount (Int32 totalsecond)
{
This._totalsecond = Totalsecond;
}
<summary>
Minus seconds
</summary>
<returns></returns>
public bool Processcountdown ()
{
if (_totalsecond = 0)
return false;
Else
{
_totalsecond--;
return true;
}
}
<summary>
Get Hour display value
</summary>
<returns></returns>
public string Gethour ()
{
Return String.Format ("{0:d2}", (_totalsecond/3600));
}
<summary>
Get minutes Display Value
</summary>
<returns></returns>
public string Getminute ()
{
Return String.Format ("{0:d2}", (_totalsecond% 3600)/60);
}
<summary>
Get seconds Display Value
</summary>
<returns></returns>
public string Getsecond ()
{
Return String.Format ("{0:d2}", _totalsecond% 60);
}
}
}

window interface design file Mainwin.xaml
Copy Code code as follows:

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= "width=" horizontalalignment= "Center" Verticalalignment= "Center"
Title= "" topmost= "true" windowstyle= "None" background= "Transparent" allowstransparency= "True" windowstartuplocation = "Centerscreen" >
<Grid>
<Grid.ColumnDefinitions>
<columndefinition/>
<columndefinition width= "/>"
<columndefinition/>
<columndefinition width= "/>"
<columndefinition/>
</Grid.ColumnDefinitions>
<textblock text= "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= "Ten" 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= "name=" Secondarea verticalalignment= "Center" fontsize= "180" background= "Red" grid.column= "4" />
</Grid>
</Window>

window Interface Logic design file: MainWin.xaml.cs:
Copy Code code as follows:

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>
Window Load Event
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Mainwin_loaded (object sender, RoutedEventArgs e)
{
Set timer
Timer = new DispatcherTimer ();
Timer. Interval = new TimeSpan (10000000); Time interval is one second
Timer. Tick + = new EventHandler (Timer_tick);
Convert to number of seconds
Int32 hour= Convert.ToInt32 (hourarea.text);
Int32 minute = Convert.ToInt32 (Minutearea.text);
Int32 second = Convert.ToInt32 (Secondarea.text);
class that handles the countdown
Processcount = new Processcount (Hour*3600+minute*60+second);
Countdown + = new Countdownhandler (Processcount.processcountdown);
Open Timer
Timer. Start ();
}
<summary>
Events triggered by a 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>
Handling Events
</summary>
public event Countdownhandler Countdown;
public bool Oncountdown ()
{
if (Countdown!= null)
return countdown ();
return false;
}
}
<summary>
Delegate that handles the countdown
</summary>
<returns></returns>
Public delegate bool Countdownhandler ();
}

In view of the detailed code annotation, so the author is no longer one by one to repeat, I hope to help you. Complete Engineering Package Download: Http://xiazai.jb51.net/201212/yuanma/CountDown_jb51.rar.

Instance two
Effect:

UI: Place a Label---><label name= "lblsecond" fontsize= "foreground=" "Red" ></Label>
Cs:
Copy Code code as follows:

private int countsecond=300; Number of seconds to record
private void Usercontrol_loaded (object sender, RoutedEventArgs e)
{
Private DispatcherTimer Distimer = new DispatcherTimer ();
Distimer.interval = new TimeSpan (0, 0, 0, 1); The parameters are: days, hours, minutes, seconds. This method has overloads that can be invoked according to actual circumstances.
Distimer.tick + = new EventHandler (Distimer_tick); The method executed every second
Distimer.start ();
}
void Distimer_tick (object sender, EventArgs e)
{
if (countsecond==0)
{
MessageBox.Show ("End");
}
Else
{
To determine whether Lblsecond is on the UI thread
if (lblSecond.Dispatcher.CheckAccess ())
{
Lblsecond.content=countsecnd.tostring ();
}
Else
{
LblSecond.Dispatcher.BeginInvoke (Dispatcherpriority.normal, (Action) () =>{
Lblsecond.content=countsecond.tostring ();
}));
}
countsecond--;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.