Windows的简单WPF文本时钟小工具
这是WindowsPresentationFoundation(WPF)的简单的基于文本的时钟小工具,其中时间以一组突出显示的字符显示在静态字符网格上。
背景
你们中的许多人一定已经看到了许多基于文本的时钟的变体。因此,我认为如果以台式机小工具的形式拥有它,那就太好了。由于我只是WPF(以及C#)的初学者,因此任何形式的建议都将受到欢迎。
使用代码
负责生成基于文本的当前时间的函数如下:
string TextTime = String.Empty; // for holding the text-timestatic Dictionary<int, string> Mapping; // maps numbers with the corresponding text;// e.g. 9 -> "NINE"123复制代码类型:[csharp]
private string GetTextTime(){ TextTime = "IT IS "; Now = DateTime.Now; int sec = Now.Second; int min = Now.Minute; int hour = Now.Hour; if (min < 5) TextTime += GetStringValue(hour) + " OCLOCK"; else if (min < 10) TextTime += "FIVE MINUTES PAST " + GetStringValue(hour); else if (min < 15) TextTime += "TEN MINUTES PAST " + GetStringValue(hour); else if (min < 20) TextTime += "A QUARTER PAST " + GetStringValue(hour); else if (min < 25) TextTime += "TWENTY MINUTES PAST " + GetStringValue(hour); else if (min < 30) TextTime += "TWENTY FIVE MINUTES PAST " + GetStringValue(hour); else if (min < 35) TextTime += "A HALF PAST " + GetStringValue(hour); else if (min < 40) TextTime += "TWENTY FIVE MINUTES TO " + GetStringValue(hour + 1); else if (min < 45) TextTime += "TWENTY MINUTES TO " + GetStringValue(hour + 1); else if (min < 50) TextTime += "A QUARTER TO " + GetStringValue(hour + 1); else if (min < 55) TextTime += "TEN MINUTES TO " + GetStringValue(hour + 1); else TextTime += "FIVE MINUTES TO " + GetStringValue(hour + 1); return TextTime;}private static string GetStringValue(int n){ n = n % 12; string value = (Mapping.Where(m => m.Key == n)).ToList()[0].Value; return value;}123456789101112131415161718192021222324252627282930313233343536373839404142复制代码类型:[csharp]
然后,我有一个char序列用于生成字符网格。
static string CHAR_SEQUENCE = "ITRISRAOTENTWENTYPHALFQUARTERFIVEMINUTESXTOBPASTHONETWOTHREEELEVENFOURFIVESIXSEVENQEIGHTNINE JTWELVETENPCOCLOCK";123复制代码类型:[csharp]
下面是我用来在TextBlock其中添加包含字符的s的网格的XAML标记。我正在使用几个函数分别将CHAR_SEQUENCE字符串转换为2D字符数组,并TextBlock在XAML网格中添加每个包含一个字符作为其字符的sText财产。
<Grid HorizontalAlignment="Left" Margin="0,0,0,0" Name="grdChars" VerticalAlignment="Top"> <Grid.BitmapEffect> <DropShadowBitmapEffect></DropShadowBitmapEffect> </Grid.BitmapEffect></Grid>123456复制代码类型:[csharp]
private static void FillCharGrid() // for converting CHAR_SEQUENCE into a 2D array{ var charArr = CHAR_SEQUENCE.ToCharArray(); int j = 0; for (int i = 0; i < charArr.Length; i++, j++) { char c = charArr[i]; CharGrid[i / 11, j % 11] = c; }}private void RenderCharGrid() // Render chars in 2D as separate TextBlocks{ FillCharGrid(); int topMargin = 20; for (int i = 0; i < CharGrid.GetLength(0); i++) { int leftMargin = 0; for (int j = 0; j < CharGrid.GetLength(1); j++) { TextBlock txt = new TextBlock(); txt.Name = "lbl" + i.ToString() + j.ToString(); txt.Text = CharGrid[i, j].ToString(); txt.FontFamily = new System.Windows.Media.FontFamily("Tahoma"); txt.Height = 30; txt.HorizontalAlignment = HorizontalAlignment.Left; txt.TextAlignment = TextAlignment.Center; txt.Margin = new Thickness(leftMargin, topMargin, 0, 0); txt.VerticalAlignment = VerticalAlignment.Top; txt.Width = 35; txt.Foreground = new SolidColorBrush(Colors.DarkGray); txt.Opacity = 2; leftMargin += 20; grdChars.Children.Add(txt); } topMargin += 20; }}123456789101112131415161718192021222324252627282930313233343536373839复制代码类型:[csharp]
以下是我用来突出显示GetTextTime()方法当前生成的文本中的字符的方法。
private void HighlightTextTime(string time){ UIElementCollection elements = grdChars.Children; var timeArr = time.Split(new char[] { ' ' }); bool toBreak = false; bool isFlattenedWRTFive = IsFlattenedWRTFive(time); bool isFlattenedWRTTen = IsFlattenedWRTTen(time); int currentFive = 0; int currentTen = 0; foreach (var str in timeArr) { var arr = str.ToCharArray(); List<textblock> labels = new List<textblock>(); for (int i = 0; i < grdChars.Children.Count; i++) { if (toBreak) { toBreak = false; break; } TextBlock txt = elements[i] as TextBlock; if (txt.Opacity == 2) continue; bool flag = false; List<textblock> Reds = new List<textblock>(); if (txt.Text.ToUpper().Equals(str[0].ToString().ToUpper())) { for (int j = 0; j < str.Length; j++) { TextBlock txt_succ = elements[i + j] as TextBlock; if (!txt_succ.Text.ToUpper().Equals(str[j].ToString().ToUpper())) { flag = false; Reds.Clear(); break; } else { flag = true; Reds.Add(txt_succ); } } if (flag) { if (str.ToUpper().Equals("FIVE") && !isFlattenedWRTFive && currentFive == 0) { currentFive = 1; continue; } if (str.ToUpper().Equals("TEN") && !isFlattenedWRTTen && currentTen == 0) { currentTen = 1; continue; } for (int p = 0; p < Reds.Count; p++) { Reds[p].Foreground = new SolidColorBrush(Colors.Red); Reds[p].Opacity = 2; } toBreak = true; } } } }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566复制代码类型:[csharp]
由于有两个字符集分别对应“五个”和“十个”,因此我对诸如ITISFIVEMINUTESTOFIVE或的时间略有ITISTENMINUTESTOTWELVE了解。首先是几分钟,然后是几个小时。因此,我写了一个简单的方法来确定当前文本时间是否相对于strings是平坦的,FIVE或者TEN因为它们可以表示分钟或小时,并且如果是几个小时,我只是跳过了第一char组,string而使用了后面的一组。我知道这根本不是解决此问题的好方法,但效果很好。这是一个示例方法,用于检查文本时间是否在wrtstring“TEN”上变平:
private bool IsFlattenedWRTTen(string time){ time = time.ToUpper(); var tenCount = CountSubStrings(time, "TEN"); if ((tenCount == 2 || tenCount == 0)) return true; if (time.Contains("MINUTES")) if (time.IndexOf("TEN") < time.IndexOf("MINUTES")) return true; return false;}1234567891011复制代码类型:[csharp]
正如您可能指出的那样,现在这是退出繁重的处理,因为它每秒都要执行一次。因此,我使用了一个stringCURRENT_TEXT_TIME用于存储当前文本时间的。每秒之后,当生成新的文本时间时,首先将它与this进行比较string,如果它们不同,则会刷新突出显示的字符,并突出显示新的文本时间。
因此,这里是构造函数和timer_Tick()事件处理程序。
public MainWindow(){ InitializeComponent(); InitializeComponent(); RenderCharGrid(); // return; Mapping = new Dictionary<int,>(); FillMapping(); timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(1); timer.Tick += new EventHandler(timer_Tick); timer.Start();}private void timer_Tick(object sender, EventArgs e){ string time = GetTextTime(); if (CURRENT_TEXT_TIME.ToUpper().Equals(time.ToUpper())) return; CURRENT_TEXT_TIME = time; ResetCharGrid(); HighlightTextTime(time);}123456789101112131415161718192021222324复制代码类型:[csharp]
最后但并非最不重要的一点是,由于我们要创建桌面小工具样式的应用程序,因此我们必须设置XAML窗口的一些属性,关闭小工具的按钮,最后设置一个Window_MouseLeftButtonDown()启用拖动小工具的处理程序。以下是窗口的XAML代码,“关闭”按钮Click事件处理程序和鼠标单击处理程序。
<Window x:Class="TextClock.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="TextClock by 007" Height="248" Width="253" AllowsTransparency="True" WindowStartupLocation="CenterScreen"WindowStyle="None" Opacity="0.8" Background="Black" MouseLeftButtonDown="Window_MouseLeftButtonDown" ShowInTaskbar="False">......</window>123456789101112131415复制代码类型:[csharp]
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e){ this.DragMove();}private void Button_Click(object sender, RoutedEventArgs e){ this.Close();}