using System;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
namespace WinFormLunisolar
{
public partial class FormLunisolar : Form
{
#region
private readonly string animals;
private DateTimePicker lunarPicker, solarPicker;
private ChineseLunisolarCalendar lunarCalendar;
#endregion
public FormLunisolar()
{
#region
InitializeComponent();
animals = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
Application.CurrentCulture = CultureInfo.GetCultureInfo("zh-CN"); // 中国区域。
lunarCalendar = new ChineseLunisolarCalendar(); // 中国阴阳历。
lunarPicker = new DateTimePicker();
lunarPicker.Dock = DockStyle.Top;
lunarPicker.Format = DateTimePickerFormat.Custom; // 自定义格式。
lunarPicker.ShowUpDown = true; // 使用数值调节钮控件。
lunarPicker.MaxDate = lunarCalendar.MaxSupportedDateTime;
lunarPicker.MinDate = lunarCalendar.MinSupportedDateTime;
lunarPicker.ValueChanged += new EventHandler(lunarPicker_ValueChanged);
this.Controls.Add(lunarPicker);
solarPicker = new DateTimePicker();
solarPicker.Dock = DockStyle.Bottom;
solarPicker.Format = DateTimePickerFormat.Custom; // 自定义格式。
solarPicker.CustomFormat = "dddd yyyy'年'M'月'd'日'";
solarPicker.ShowUpDown = true; // 使用数值调节钮控件。
solarPicker.MaxDate = lunarCalendar.MaxSupportedDateTime;
solarPicker.MinDate = lunarCalendar.MinSupportedDateTime;
solarPicker.ValueChanged += new EventHandler(solarPicker_ValueChanged);
solarPicker.Value = DateTime.Today;
this.Controls.Add(solarPicker);
this.TopMost = true; // 前端显示。
this.ShowInTaskbar = false; // 在 Windows 任务栏中隐藏窗体。
this.HelpButton = true; // 显示“帮助”按钮。
this.MaximizeBox = false; // 隐藏“最大化”按钮。
this.MinimizeBox = false; // 隐藏“最小化”按钮。
this.AutoScaleMode = AutoScaleMode.Font; // 根据字体的维度控制缩放。
this.Font = new Font(Font.Name, 12F);
this.ClientSize = new Size(192, 52);
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 = solar.Year;
int month = solar.Month;
int day = Math.Min(solar.Day, lunarCalendar.GetDaysInMonth(year, month));
int leapMonth = lunarCalendar.GetLeapMonth(year);
if (0 < leapMonth && leapMonth <= month)
++month;
solarPicker.Value = lunarCalendar.ToDateTime(year, month, day, 0, 0, 0, 0);
}
#endregion
#region 计算阴历
private void solarPicker_ValueChanged(object sender, EventArgs e)
{
DateTime solar = solarPicker.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;
if (month == 2 && day > 28)
lunarPicker.CustomFormat = string.Format("{0}年 {1}年 {2}月{3}日", animals[(year - 4) % 12], year, month, day);
else
{
lunarPicker.CustomFormat = string.Format("{0}'年' yyyy'年'M'月'd'日'", animals[(year - 4) % 12]);
lunarPicker.Value = new DateTime(year, month, day);
}
}
#endregion
#region DesktopBounds
protected override void OnHelpButtonClicked(System.ComponentModel.CancelEventArgs e)
{
base.OnHelpButtonClicked(e);
e.Cancel = true;
using (FontDialog fontDialog = new FontDialog())
{
fontDialog.Font = this.Font;
fontDialog.ShowEffects = false;
if (fontDialog.ShowDialog(this) == DialogResult.OK)
{
this.Font = fontDialog.Font;
int sw = TextRenderer.MeasureText("星期日 0000年00月00日", this.Font).Width;
this.ClientSize = new Size(sw + 16, lunarPicker.Height + solarPicker.Height);
Size ws = Screen.GetWorkingArea(this).Size - this.Size;
this.DesktopLocation = new Point(ws.Width / 2, ws.Height / 2);
}
}
}
#endregion
}
}