当前位置:首页 > Windows程序 > 正文

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

2021-05-25 Windows程序

技术分享

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

首先是定义一个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第一弹:窗口抖动+边框阴影效果+倒计时显示文字 ,也可以今明天再来看看第三篇~

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

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/71032.html