C# 中国阴历,阳历互转
2010-03-10 08:36
288 查看
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 = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
lunarCalendar = new ChineseLunisolarCalendar(); // 中国阴阳历。
lunarPicker = new DateTimePicker();
lunarPicker.Dock = DockStyle.Top;
lunarPicker.Format = DateTimePickerFormat.Custom; // 自定义格式。
lunarPicker.ShowUpDown = true; // 使用数值调节钮控件。
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.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
}
}
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 = "鼠牛虎兔龙蛇马羊猴鸡狗猪";
lunarCalendar = new ChineseLunisolarCalendar(); // 中国阴阳历。
lunarPicker = new DateTimePicker();
lunarPicker.Dock = DockStyle.Top;
lunarPicker.Format = DateTimePickerFormat.Custom; // 自定义格式。
lunarPicker.ShowUpDown = true; // 使用数值调节钮控件。
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.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
}
}
相关文章推荐
- 一个阴历阳历互相转化的类(c#源码)
- C#实现阳历转阴历的方法
- C# 中国阴历
- C# 阳历转阴历函数 公历转农历函数
- 计算中国阳历节日、阴历节日、星座、时辰等一切时间有关
- C# 查农历 阴历 阳历 公历 节假日
- 一个阴历阳历互相转化的类(c#源码)
- C# 阴历,阳历互转
- C#实现的阴历阳历互相转化类实例
- 一个阴历阳历互相转化的类(C#源码)
- 一个阴历阳历互相转化的类(c#农历)
- C# 查农历 阴历 阳历 公历 节假日
- 一个阴历阳历互相转化的类(C#源码)
- 阳历算阴历-----C语言程序
- vb.net阳历转阴历
- CSDN中国IT技术指数(语言):Java第一,C#第六
- C#中国身份证验证
- C#实现中国身份证验证问题
- 计算一年中的第几周/c#得到阳历对应农历日期
- c#获取中国城市天气编号代码