码迷,mamicode.com
首页 > Windows程序 > 详细

好玩的WPF第二弹:电子表字体+显示系统时间

时间:2015-06-11 17:02:39      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:效果   显示   wpf   字体   系统   

技术分享

效果呢就是这么个效果,难度相较于上一篇也要简单许多。

首先是定义一个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="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Width="500" Height="200"
        WindowStyle="None"
        AllowsTransparency="True"
        Background="Black">

    <Window.Resources>
        <Style x:Key="QuartzMSFont">
            <Setter Property="TextElement.FontFamily" Value="Resources/#Quartz MS"/>
        </Style>
    </Window.Resources>

    <Grid>
        <TextBlock Name="tBlockTime" Style="{DynamicResource QuartzMSFont}"  
                   HorizontalAlignment="Center" 
                   VerticalAlignment="Center" FontSize="68" Foreground="Green"/>
    </Grid>

</Window>

这里我只是给大家一个启发,如果系统自带的字体已经不能满足你的艺术感,你完全可以另外找字体。甚至也可以创造字体,近来谷歌苹果都在做这个。

技术分享

我已经把字体放到项目中了,需要源码/字体的童鞋直接留邮箱……

技术分享

这一篇内容不多,也算不上精彩,但童鞋们可以看看上一篇:好玩的WPF第一弹:窗口抖动+边框阴影效果+倒计时显示文字 ,也可以今明天再来看看第三篇~



感谢您的访问,希望对您有所帮助。 欢迎大家关注、收藏以及评论。


为使本文得到斧正和提问,转载请注明出处:
http://blog.csdn.net/nomasp


好玩的WPF第二弹:电子表字体+显示系统时间

标签:效果   显示   wpf   字体   系统   

原文地址:http://blog.csdn.net/nomasp/article/details/46457923

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!