好玩的WPF第二弹:电子表字体显示时间+多彩呼吸灯特效按钮

这里写图片描述

这里写图片描述

我们先来看看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
  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 38
    评论
评论 38
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值