改自网页版的美女时钟
MainWindow.xaml:
<Window x:Class="Clock.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="430" Width="640" WindowStartupLocation="CenterScreen" ToolTip="双击鼠标左键退出,右键置顶!"
ShowInTaskbar="False" MouseDoubleClick="Window_MouseDoubleClick" WindowStyle="None"
MouseRightButtonUp="Window_MouseRightButtonUp">
<Window.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF0B15E0" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Window.Background>
<Grid x:Name="grid1" />
</Window>
MainWindow.xaml.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Threading; namespace Clock { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { DispatcherTimer timer = new DispatcherTimer(); int hour, minute; //小时,分钟 string h, m; //小时,分钟 int old = -1; public MainWindow() { InitializeComponent(); timer.Tick += timer_Tick; //timer.Interval = TimeSpan.FromMinutes(1); //按1分钟换图 timer.Interval = TimeSpan.FromSeconds(10); //按1秒钟换图 GetTime(); //获取时间 timer.Start(); //开始计时器 } void timer_Tick(object sender, EventArgs e) { GetTime(); //获取时间 } private void Window_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e) { this.Close(); //关闭 } private void Window_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) { // 在此处添加事件处理程序实现。 if (this.Topmost) //窗口置顶 this.Topmost = false; else this.Topmost = true; } private void GetTime() { minute = DateTime.Now.Minute; //获取系统时间,分钟数 if (old != minute) { old = minute; //minute = DateTime.Now.Second; //获取系统时间,秒数 hour = DateTime.Now.Hour; //小时 if (minute < 10) { m = "0" + minute.ToString(); //设置分钟 } else { m = minute.ToString(); } if (hour < 10) { h = "0" + hour.ToString(); //设置小时 } else { h = hour.ToString(); } ImageBrush img1 = new ImageBrush(); //图片画刷 img1.ImageSource = new BitmapImage(new Uri("image/" + h + "_" + m + ".jpg", UriKind.RelativeOrAbsolute)); //画刷源图片 this.grid1.Background = img1; //设置画刷到grid1 } } } }