C# 中国阴历

using System;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;

namespace WinFormLunar
{
    public partial class FormLunar : Form
    {
        #region
        private readonly string animals, gan, zhi;
        private TextBox txtLunar;
        private DateTimePicker lunarPicker;
        private ChineseLunisolarCalendar lunarCalendar;
        #endregion

        public FormLunar()
        {
            #region
            InitializeComponent();
            gan = "甲乙丙丁戊己庚辛壬癸";
            zhi = "子丑寅卯辰巳午未申酉戌亥";
            animals = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
            Application.CurrentCulture = CultureInfo.GetCultureInfo("zh-CN"); // 中国区域。
            lunarCalendar = new ChineseLunisolarCalendar(); // 中国阴阳历。
            txtLunar = new TextBox();
            txtLunar.Dock = DockStyle.Bottom;
            txtLunar.ReadOnly = true; // 只读。
            this.Controls.Add(txtLunar);
            lunarPicker = new DateTimePicker();
            lunarPicker.Dock = DockStyle.Top;
            lunarPicker.Format = DateTimePickerFormat.Custom; // 自定义格式。
            lunarPicker.CustomFormat = "dddd yyyy'年'M'月'd'日'";
            lunarPicker.ShowUpDown = true; // 使用数值调节钮控件。
            lunarPicker.MaxDate = lunarCalendar.MaxSupportedDateTime;
            lunarPicker.MinDate = lunarCalendar.MinSupportedDateTime;
            lunarPicker.ValueChanged += new EventHandler(lunarPicker_ValueChanged);
            lunarPicker.Value = DateTime.Today;
            this.Controls.Add(lunarPicker);
            this.AutoScaleMode = AutoScaleMode.Font; // 根据字体的维度控制缩放。
            this.Font = new Font(Font.Name, 24F);
            this.ClientSize = new Size(500, 88);
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink; // 禁用手动调整大小。
            this.SizeGripStyle = SizeGripStyle.Hide; // 隐藏调整大小手柄。
            this.StartPosition = FormStartPosition.CenterScreen; // 在桌面居中显示。
            #endregion
        }

        #region 计算阴历
        private void lunarPicker_ValueChanged(object sender, EventArgs e)
        {
            DateTime solar = lunarPicker.Value;
            int year = lunarCalendar.GetYear(solar);
            int month = lunarCalendar.GetMonth(solar);
            int day = lunarCalendar.GetDayOfMonth(solar);
            int leapMonth = lunarCalendar.GetLeapMonth(year);
            if (0 < leapMonth && leapMonth <= month)
                --month;
            txtLunar.Text = string.Format("农历 {0}{1}年 {2}年 {3}年{4}月{5}日", gan[(year - 4) % 10], zhi[(year - 4) % 12], animals[(year - 4) % 12], year, month, day);
        }
        #endregion
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值