C#实现动态数字时钟和日历

本文实例为大家分享了C#实现动态数字时钟和日历的具体代码,供大家参考,具体内容如下

实现如下图所示的简易时钟和日历,要求显示公历日期、时间、星期、农历日期。

首先新建一个ChineseCanlendar类用于实现和农历相关的操作:

【ChineseCanlendar.cs】

/*
 * 作者:JeronZhou
 * 时间:2021-11-01
 * 功能:动态数字时钟和日历
 */

using System;
using System.Linq;
using System.Globalization;

namespace Test2_1
{
    static public class ChineseCanlendar
    {
        private static ChineseLunisolarCalendar ChineseCalendar = new ChineseLunisolarCalendar();
        private static string[] tg = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
        private static string[] dz = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
        private static string[] sx = { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
        public static string GetLunisolarYear(int year)
        {
            if (year > 3)
            {
                int tgIndex = (year - 4) % 10;
                int dzIndex = (year - 4) % 12;
                return string.Concat(tg[tgIndex], dz[dzIndex], "[", sx[dzIndex], "]");
            }
            throw new ArgumentOutOfRangeException("年份无效!");
        }
        private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)" };
        private static string[] days1 = { "初", "十", "廿", "三" };
        private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
        public static string GetLunisolarMonth(int month)
        {
            if (month < 13 && month > 0)
            {
                return months[month - 1];
            }
            throw new ArgumentOutOfRangeException("月份无效!");
        }
        public static string GetLunisolarDay(int day)
        {
            if (day > 0 && day < 32)
            {
                if (day != 20 && day != 30)
                {
                    return string.Concat(days1[(day - 1) / 10], days[(day - 1) % 10]);
                }
                else
                {
                    return string.Concat(days[(day - 1) / 10], days1[1]);
                }
            }
            throw new ArgumentOutOfRangeException("无效日期!");
        }
        public static string GetChineseDateTime(DateTime datetime)
        {
            int year = ChineseCalendar.GetYear(datetime);
            int month = ChineseCalendar.GetMonth(datetime);
            int day = ChineseCalendar.GetDayOfMonth(datetime);
            int leapMonth = ChineseCalendar.GetLeapMonth(year);
            bool isleap = false;
            if (leapMonth > 0)
            {
                if (leapMonth == month)
                {    
                    isleap = true;
                    month--;
                }
                else if (month > leapMonth)
                {
                    month--;
                }
            }
            return string.Concat(GetLunisolarYear(year), "年", isleap ? "闰" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day));
        }
    }
}

【窗体设计】

从上到下设置三个标签,并添加背景颜色,为了使效果更佳,最好将窗口全部填满:

【MainForm.cs】

/*
 * 作者:JeronZhou
 * 时间:2021-11-01
 * 功能:动态数字时钟和日历
 */

using System;
using System.Linq;
using System.Windows.Forms;

namespace Test2_1
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            timer1.Start();
        }
        void Timer1Tick(object sender, EventArgs e)
        {
            label1.Text = DateTime.Now.ToString();
            switch(DateTime.Now.DayOfWeek.ToString())
            {
                case "Monday":
                    label2.Text = "星期一"; break;
                case "Tuesday":
                    label2.Text = "星期二"; break;
                case "Wednesday":
                    label2.Text = "星期三"; break;
                case "Thursday":
                    label2.Text = "星期四"; break;
                case "Friday":
                    label2.Text = "星期五"; break;
                case "Saturday":
                    label2.Text = "星期六"; break;
                case "Sunday":
                    label2.Text = "星期日"; break;
            }
            string GCDT = ChineseCanlendar.GetChineseDateTime(DateTime.Now);
            label3.Text = GCDT;
        }
    }
}

【Program.cs】

/*
 * 作者:JeronZhou
 * 时间:2021-11-01
 * 功能:动态数字时钟和日历
 */

using System;
using System.Windows.Forms;

namespace Test2_1
{
    internal sealed class Program
    {
        [STAThread]
        private static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }
}

【运行结果】

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • C#实现中文日历Calendar

    一.层次结构 Object<-----Calendar<-----EastAsianLunisolarCalendar<-----ChineseLunisolarCalendar(农历) 二.用法 1.支持的最大,最小日期 System.Globalization.ChineseLunisolarCalendar cc =new System.Globalization.ChineseLunisolarCalendar(); s1=cc.MinSupportedDateTime.ToSt

  • C#实现日历效果

    本文实例为大家分享了C#实现日历效果的具体代码,供大家参考,具体内容如下 展示: 主要代码: public partial class calendar : Form     {         public calendar()         {             InitializeComponent();         }         int year, month;         private void textBoxMonth_TextChanged(object s

  • C#实现功能强大的中国农历日历操作类

    本文实例讲述了C#实现功能强大的中国农历日历操作类.分享给大家供大家参考.具体如下: 这个C#类定义了中国农历日历,除了可以输入正常的日历外还可以获得指定年份的生肖.返回年份的干支以及星座.二十四节气.二十八星宿.常用节日等. 部分代码片段如下 /// <summary> /// 传回农历y年闰月的天数 /// </summary> private static int leapDays(int y) { if (leapMonth(y) != 0) { if ((lunarInf

  • C#日历样式的下拉式计算器实例讲解

    本文介绍了如何在Visual Studio中创建用户控件来显示下拉式计算器,弹出效果类似于日历控件. 介绍 如果我们正在做一个类似于库存控制和计费系统的项目,有些部分可能必须手动计算数值.因此,用户就不得不使用计算器得到结果,再填入到输入字段中,或者在工作窗口上单独打开一个计算器窗口.总之,各种不便和麻烦. 这篇文章主要描述的是如何添加下拉式计算器到DataGridView单元格中,如下图: 使用代码 第一步,我们必须先创建一个函数计算器,并且能够使用控件.因此,不妨先创建一个Visual St

  • C#实现漂亮的数字时钟效果

    本文实例讲述了用C#做了一个漂亮的数字时钟.分享给大家供大家参考. 程序运行后界面如下: 实现技术:主要是通过Graphics类的DrawImage方法来绘制数字时钟中所有的数字,这些数字是从网上找的一些图片文件.时钟使用DateTime中Now属性来获得不同的,时,分,秒,最后通过定时器来实现时钟的运行状态. 主要代码如下: 复制代码 代码如下: //将0~9数字图片保存在Image数组中  private Image[] image = new Bitmap[10];  public For

  • c# winform时钟的实现代码

    代码如下: 复制代码 代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Simpclock {     public partial class Form1 : Form     {   

  • C#实现农历日历的方法

    本文实例讲述了C#实现农历日历的方法.分享给大家供大家参考. 具体实现方法如下: 复制代码 代码如下: //天干  private  static string []TianGan =   {"甲","乙","丙","丁","戊","己","庚","辛","壬","癸"};     //地支  private

  • C# 日历类功能的实例代码

    C# 日历类的实现代码,具体如下所示: using System; namespace DotNet.Utilities { /// <summary> /// 农历属性 /// </summary> public class CNDate { /// <summary> /// 农历年(整型) /// </summary> public int cnIntYear = 0; /// <summary> /// 农历月份(整型) /// <

  • C#简单输出日历的方法

    本文实例讲述了C#简单输出日历的方法.分享给大家供大家参考.具体如下: 用C#输出日历,此功能可用于Ajax方式列出计划日程相关的内容,由于是C#控制输出,可以方便加上自己需要的业务处理逻辑. 1.控制台输出: using System; namespace 控制台日历 { class Program { public static void Main(string[] args) { string s = " "; Console.WriteLine("输入年份:"

  • c# 日历控件的实现

    public partial class MonthCalendarForm : Form { public MonthCalendarForm() { InitializeComponent(); } //窗体加载事件 private void MonthCalendarForm_Load(object sender, EventArgs e) { //隐藏日历控件 monthCalendar1.Hide(); } //"选择"按钮的单击事件 private void button1

随机推荐