我们先来看看Quartz MS字体动态显示系统时间的效果,难度相较于上一篇也要简单许多。
首先是定义一个TextBlock如下。
<Grid>
<TextBlock Name="tBlockTime" HorizontalAlignment="Center"
VerticalAlignment="Center" FontSize="68" Foreground="Green"/>
</Grid>
后台代码如下:
private DispatcherTimer dispatcherTimer;
public MainWindow()
{
InitializeComponent();
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
// 当间隔时间过去时发生的事件
dispatcherTimer.Tick += new EventHandler(ShowCurrentTime);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1);
dispatcherTimer.Start();
}
public void ShowCurrentTime(object sender, EventArgs e)
{
//获得星期
//this.tBlockTime.Text = DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo("zh-cn"));
//this.tBlockTime.Text += "\n";
//获得年月日
//this.tBlockTime.Text = DateTime.Now.ToString("yyyy:MM:dd"); //yyyy年MM月dd日
//this.tBlockTime.Text += "\n";
//获得时分秒
this.tBlockTime.Text = DateTime.Now.ToString("HH:mm:ss");
}
注意在这个时间的设置时,第一步显示的时间是”=”,随后都是”+=”。比如说要先显示星期,再显示时分秒,就是这样的:
//获得星期
this.tBlockTime.Text = DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo("zh-cn"));
this.tBlockTime.Text += "\n";
//获得时分秒
this.tBlockTime.Text += DateTime.Now.ToString("HH:mm:ss");
然后还需要字体,然而字体并不可能是写出来的……我们都需要需要引用资源。
<Window x:Class