失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > winfrom阴阳历互相转换

winfrom阴阳历互相转换

时间:2019-10-10 23:01:29

相关推荐

winfrom阴阳历互相转换

using System;

using System.Collections.Generic;

using ponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Globalization;//引用内置

namespace WindowsFormsApplication1

{

public partial class Form3 : Form

{

#region

private readonly string animals;

private DateTimePicker lunarPicker, solarPicker;

private ChineseLunisolarCalendar lunarCalendar;

#endregion

public Form3()

{

#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(ponentModel.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;

using System.Collections.Generic;

using ponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Globalization;

namespace WindowsFormsApplication1

{

public partial class Form2 : Form

{

private static ChineseLunisolarCalendar chineseDate = new ChineseLunisolarCalendar();

public Form2()

{

InitializeComponent();

ShowYearInfo();

ShowCurrentYearInfo();

Console.ReadLine();

}

/// <summary>

/// 展示阴历年份信息

/// </summary>

public static void ShowYearInfo()

{

for (int i = chineseDate.MinSupportedDateTime.Year; i < chineseDate.MaxSupportedDateTime.Year; i++)

{

Console.WriteLine("年份:{0},月份总数:{1},总天数:{2},干支序号:{3}", i, chineseDate.GetMonthsInYear(i), chineseDate.GetDaysInYear(i)

, chineseDate.GetSexagenaryYear(new DateTime(i, 3, 1)));

}

}

/// <summary>

/// 展示当前年份信息

/// </summary>

public static void ShowCurrentYearInfo()

{

int lYear=chineseDate.GetYear(DateTime.Now);

int lMonth=chineseDate.GetMonth(DateTime.Now);

int lDay=chineseDate.GetDayOfMonth(DateTime.Now);

/** GetLeapMonth(int year)方法返回一个1到13之间的数字,

* 比如:1、该年阴历2月有闰月,则返回3

* 如果:2、该年阴历8月有闰月,则返回9

* GetMonth(DateTime dateTime)返回是当前月份,忽略是否闰月

* 比如:1、该年阴历2月有闰月,2月返回2,闰2月返回3

* 如果:2、该年阴历8月有闰月,8月返回8,闰8月返回9

*/

int leapMonth = chineseDate.GetLeapMonth(lYear);//获取第几个月是闰月,等于0表示本年无闰月

//如果今年有闰月

if (leapMonth > 0)

{

//闰月数等于当前月份

if (lMonth == leapMonth)

{

Console.WriteLine("今年的阴历日期:{0}年闰{1}月{2}日。", lYear, lMonth - 1, lDay);

}

else if (lMonth > leapMonth)//

{

Console.WriteLine("今年的阴历日期:{0}年{1}月{2}日。", lYear, lMonth - 1, lDay);

}

else

{

Console.WriteLine("今年的阴历日期:{0}年{1}月{2}日。", lYear, lMonth, lDay);

}

}

else

{

Console.WriteLine("今年的阴历日期:{0}年{1}月{2}日。", lYear, lMonth, lDay);

}

Console.WriteLine("今天的公历日期:" + DateTime.Now.ToString("yyyy-MM-dd"));

Console.WriteLine("今年阴历天数:{0},今年{1}闰年", chineseDate.GetDaysInYear(DateTime.Now.Year),(chineseDate.IsLeapYear(DateTime.Now.Year)==true)?"是":"不是");

Console.WriteLine("今年农历每月的天数:");//注意:如果有13个数字表示当年有闰月

for (int i = 1; i <= chineseDate.GetMonthsInYear(DateTime.Now.Year); i++)

{

Console.Write("{0,-5}",chineseDate.GetDaysInMonth(DateTime.Now.Year,i));

}

}

}

}

如果觉得《winfrom阴阳历互相转换》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。