欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

桌面时钟

程序员文章站 2022-07-12 22:19:19
...

一个桌面时钟的小demo分享给大家,前几天重装系统,后来想美化一下写的,简约大气,美化win10必备,你值得拥有,代码贴出来,十几二十行代码搞定,如果权限不够请生成后右击以管理员身份运行

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace 桌面时钟
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //定义一个数组以显示星期
        private string[] weeks = { "", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
        private void Form1_Load(object sender, EventArgs e)
        {
            #region 开机自启
            string path = Application.ExecutablePath;//取到当前应用程序的路径
            RegistryKey rk = Registry.LocalMachine;//得到注册表
            RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
            rk2.SetValue("DesktopClock", path);//为注册表设置新值 
            //Console.WriteLine(rk2.GetValue("DesktopClock").ToString());
            rk2.Close();
            rk.Close(); 
            #endregion
            //窗口透明化
            this.TransparencyKey = Color.White;
            this.BackColor = TransparencyKey;
            //获取当前屏幕的长和宽
            int screenX = Screen.PrimaryScreen.Bounds.Width;
            int screenY = Screen.PrimaryScreen.Bounds.Height;
            //让窗体居中显示
            int startPositionX = screenX / 2 - 325;
            int startPositionY = screenY / 2 - 240;
            this.Location = new System.Drawing.Point(startPositionX, startPositionY);
        }
        //为timer委托事件
        private void timer1_Tick(object sender, EventArgs e)
        {
            string time = DateTime.Now.ToString("t");//取到当前时分
            string date = DateTime.Now.ToString("yy/MM/dd");//取到当前年月日
            string week = weeks[(int)DateTime.Now.DayOfWeek];//得到星期字符串
            label1.Text = time + "\r\n" + date + "\r\n" + week;//连接字符串用label显示
        }
    } 
}

效果如下
桌面时钟
关于开机自启
桌面时钟
网盘下载:链接:http://pan.baidu.com/s/1gfJ2H6j 密码:6yj8