using System;
using System.Drawing;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace LunisolarCalendar
{
public partial class FormCalendar : Form
{
#region AnimateWindow
[DllImport("user32.dll", EntryPoint = "AnimateWindow")]
private static extern bool AnimateWindow(IntPtr handle, int ms, int flags);
#endregion
#region
private EastAsianLunisolarCalendar lunarCalendar;
private readonly string animals, gan, zhi;
#endregion
public FormCalendar()
{
#region
InitializeComponent();
this.StartPosition = FormStartPosition.Manual; // 窗体的位置由 Location 属性确定。
this.DesktopLocation = new Point(Screen.GetWorkingArea(this).Size - this.Size);
this.AutoSizeMode = AutoSizeMode.GrowAndShrink; // 禁用手动调整大小。
this.SizeGripStyle = SizeGripStyle.Hide; // 隐藏调整大小手柄。
statusStripInfo.SizingGrip = false; // 隐藏状态栏大小调整手柄。
toolComboBoxCulture.DropDownStyle = ComboBoxStyle.DropDownList;
toolComboBoxCulture.ComboBox.DisplayMember = "DisplayName";
toolComboBoxCulture.ComboBox.DataSource = new CultureInfo[] { CultureInfo.GetCultureInfo("zh-CN"), CultureInfo.GetCultureInfo("ko-KR") };
monthCalendar.DataBindings.Add("SelectionEnd", datePicker, "Value");
datePicker.CustomFormat = "yyyy-MM-dd";
datePicker.Format = DateTimePickerFormat.Custom; // 自定义格式。
datePicker.ShowUpDown = true;
datePicker.MaxDate = lunarCalendar.MaxSupportedDateTime;
datePicker.MinDate = lunarCalendar.MinSupportedDateTime;
gan = "甲乙丙丁戊己庚辛壬癸";
zhi = "子丑寅卯辰巳午未申酉戌亥";
animals = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
#endregion
}
#region FormLoad
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
AnimateWindow(this.Handle, 1000, 0x20010); // 居中逐渐显示。
}
#endregion
#region FormClosing
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
AnimateWindow(this.Handle, 1000, 0x10010); // 居中逐渐隐藏。
}
#endregion
#region LunisolarCalendar
private void timerInfo_Tick(object sender, EventArgs e)
{
DateTime solar = monthCalendar.SelectionEnd;
int year = lunarCalendar.GetYear(solar);
int lm = lunarCalendar.GetMonth(solar);
int day = lunarCalendar.GetDayOfMonth(solar);
int leapMonth = lunarCalendar.GetLeapMonth(year);
if (0 < leapMonth && leapMonth <= lm)
--lm;
int sm = solar.Month;
leapMonth = lunarCalendar.GetLeapMonth(solar.Year);
if (0 < leapMonth && leapMonth <= sm)
++sm;
if (solar.Day <= lunarCalendar.GetDaysInMonth(solar.Year, sm))
solar = lunarCalendar.ToDateTime(solar.Year, sm, solar.Day, 0, 0, 0, 0);
this.Text = string.Format("农历 {0}{1}【{2}年】", gan[(year - 4) % 10], zhi[(year - 4) % 12], animals[(year - 4) % 12]);
statusLabelTime.Text = string.Format("[{0:D}] [{1} {2:00}]", solar, DateTimeFormatInfo.CurrentInfo.MonthNames[lm - 1], day);
}
#endregion
#region Application.CurrentCulture
private void toolComboBoxCulture_SelectedIndexChanged(object sender, EventArgs e)
{
Application.CurrentCulture = toolComboBoxCulture.SelectedItem as CultureInfo;
switch (Application.CurrentCulture.Name)
{
case "zh-CN":
lunarCalendar = new ChineseLunisolarCalendar();
break;
case "ko-KR":
lunarCalendar = new KoreanLunisolarCalendar();
break;
}
}
#endregion
}
}