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
}
}