- 主题
- 355
- 战斗力
- 2987
- 美誉度
- 463
- 铜钱
- 12917
- 注册时间
- 2022-5-24
- 最后登录
- 2024-11-21
- 在线时间
- 210 小时
|
本帖最后由 tiger008 于 2023-3-10 10:21 编辑
帮我用C# NET6写一个窗体万年历。
可查时间越大越好,要有阳历,阴历,农历,天干地支,星座,宜忌。
这个窗体是要与别一个C# NET6写一个窗体整合到一起用的,用于八字测算。
好的,以下是一个简单的 C# .NET 6 窗体万年历示例,它包括阳历、阴历、农历、天干地支、星座、宜忌等信息:
csharp
Copy code
using System;
using System.Windows.Forms;
using System.Globalization;
using System.Collections.Generic;
n**espace CalendarApp
{
public partial class FormCalendar : Form
{
private DateTime currentDate;
private ChineseLunisolarCalendar chineseCalendar;
private string[] gan = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
private string[] zhi = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};
private string[] sheng**** = {"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};
private Dictionary<int, string> yi = new Dictionary<int, string>()
{
{1, "祭祀"},
{2, "祈福"},
{3, "开市"},
{4, "求财"},
{5, "订婚"},
{6, "纳采"},
{7, "嫁娶"},
{8, "入宅"},
{9, "搬家"},
{10, "安葬"},
{11, "破土"},
{12, "修造"},
{13, "立券"},
{14, "开光"},
{15, "出行"},
{16, "安床"},
{17, "交易"},
{18, "造船"},
{19, "订盟"},
{20, "裁衣"},
{21, "伐木"},
{22, "拆卸"},
{23, "安门"},
{24, "栽种"},
{25, "立刻"},
{26, "赴任"},
{27, "开仓"},
{28, "作灶"},
{29, "入学"},
{30, "捕捉"},
{31, "分居"},
};
private Dictionary<int, string> ji = new Dictionary<int, string>()
{
{1, "祭祀"},
{2, "祈福"},
{3, "安床"},
{4, "安葬"},
{5, "出行"},
{6, "开市"},
{7, "破土"},
{8, "上梁"},
{9, "修造"},
{10, "订盟"},
{11, "求嗣"},
{12, "嫁娶"},
{13, "立券"},
{14, "造车器"},
{15, "造船"},
{16, "造畜椆栖"},
{17, "造屋"},
{18, "
继续
// 定义一个八字结构体
public struct EightCharacters
{
public string yearGan;
public string yearZhi;
public string monthGan;
public string monthZhi;
public string dayGan;
public string dayZhi;
public string hourGan;
public string hourZhi;
}
csharp
Copy code
public FormCalendar()
{
InitializeComponent();
// 获取当前日期
currentDate = DateTime.Now;
// 创建中国农历日历对象
chineseCalendar = new ChineseLunisolarCalendar();
// 显示当前日期信息
ShowDateInfo();
}
// 显示日期信息
private void ShowDateInfo()
{
// 显示阳历日期
labelSolarDate.Text = currentDate.ToString("yyyy年M月d日 dddd", new CultureInfo("zh-CN"));
// 显示阴历日期
int year = chineseCalendar.GetYear(currentDate);
int month = chineseCalendar.GetMonth(currentDate);
int day = chineseCalendar.GetDayOfMonth(currentDate);
bool isLeapMonth = chineseCalendar.IsLeapMonth(year, month);
string lunarMonth = isLeapMonth ? "闰" : "";
lunarMonth += chineseCalendar.GetMonthN**e(year, month);
string lunarDay = chineseCalendar.GetDayN**e(year, month, day);
labelLunarDate.Text = $"{lunarMonth}{lunarDay}";
// 显示农历日期
int[] lunarYearInfo = GetLunarYearInfo(year);
labelChineseDate.Text = $"{gan[lunarYearInfo[0]]}{zhi[lunarYearInfo[1]]}年{lunarMonth}{lunarDay}";
// 显示天干地支和**
labelGanZhi.Text = $"{gan[chineseCalendar.GetCelestialStem(year) - 1]}{zhi[chineseCalendar.GetTerrestrialBranch(year) - 1]}";
int sheng****Index = (year - 4) % 12;
labelSheng****.Text = sheng****[sheng****Index];
// 显示当前日期的宜忌
int dayOfWeek = (int)currentDate.DayOfWeek;
if (dayOfWeek == 0)
{
dayOfWeek = 7;
}
int yiIndex = day * dayOfWeek % 31;
int jiIndex = (day + month) % 31;
string yiStr = yi.ContainsKey(yiIndex) ? yi[yiIndex] : "无";
string jiStr = ji.ContainsKey(jiIndex) ? ji[jiIndex] : "无";
labelYiJi.Text = $"宜:{yiStr}\n忌:{jiStr}";
// 显示八字信息
EightCharacters bazi = GetEightCharacters(currentDate);
labelYearGanZhi.Text = $"{bazi.yearGan}{bazi.yearZhi}";
labelMonthGanZhi.Text = $"{bazi.monthGan}{bazi.monthZhi}";
labelDayGanZhi.Text = $"{bazi.dayGan}{bazi.dayZhi}";
labelHourGanZhi.Text = $"{bazi.hourGan}{bazi.hourZhi}";
}
// 获取农历年份信息
private int[] GetLunarYearInfo(int year)
{
int[] info = new int[2];
int ganIndex = (year - 4) % 10;
int zhiIndex = (year - 4) % 12
继续
scss
Copy code
info[0] = ganIndex;
info[1] = zhiIndex;
return info;
}
// 获取八字信息
private EightCharacters GetEightCharacters(DateTime date)
{
EightCharacters bazi = new EightCharacters();
int year = chineseCalendar.GetYear(date);
int month = chineseCalendar.GetMonth(date);
int day = chineseCalendar.GetDayOfMonth(date);
int hour = date.Hour;
// 获取年柱
int[] yearInfo = GetLunarYearInfo(year);
bazi.yearGan = gan[yearInfo[0] % 5];
bazi.yearZhi = zhi[yearInfo[1]];
// 获取月柱
DateTime startOfMonth = new DateTime(year, month, 1);
int days = chineseCalendar.GetDaysInMonth(year, month);
int endDayGanIndex = chineseCalendar.GetCelestialStem(startOfMonth.AddDays(days)) - 1;
int monthGanIndex = endDayGanIndex * 2 - yearInfo[0] % 5 - (isLeapYear(year) && month == chineseCalendar.GetLeapMonth(year) ? 1 : 0);
if (monthGanIndex < 0)
{
monthGanIndex += 10;
}
bazi.monthGan = gan[monthGanIndex % 10];
bazi.monthZhi = zhi[(month + 1) % 12];
// 获取日柱
int dayGanIndex = (yearInfo[1] % 5) * 2 + chineseCalendar.GetDayOfYear(date) - 1;
bazi.dayGan = gan[dayGanIndex % 10];
bazi.dayZhi = zhi[day % 12];
// 获取时柱
int hourGanIndex = (dayGanIndex % 10) * 12 + hour / 2;
bazi.hourGan = gan[hourGanIndex % 10];
bazi.hourZhi = zhi[hour % 12];
return bazi;
}
// 判断是否为闰年
private bool isLeapYear(int year)
{
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
// 前一天
private void buttonPrevDay_Click(object sender, EventArgs e)
{
currentDate = currentDate.AddDays(-1);
ShowDateInfo();
}
// 后一天
private void buttonNextDay_Click(object sender, EventArgs e)
{
currentDate = currentDate.AddDays(1);
ShowDateInfo();
}
// 转到今天
private void buttonToday_Click(object sender, EventArgs e)
{
currentDate = DateTime.Now;
ShowDateInfo();
}
}
}
// 宜忌字典
private static readonly Dictionary<int, string> yi = new Dictionary<int, string>
{
{1, "祭祀.入殓.安葬.修坟.立碑.开光.嫁娶.捕捉.纳畜.牧养.理发.理发"},
{2, "祈福.求嗣.结婚.开市.交易.安床.问名.纳采.订盟.竖柱.上梁.会亲友"},
{3,
继续
"开工.动土.出行.移徙.入宅.修造.上梁.安门.安碓硙.筑堤.造桥"},
{4, "订盟.纳采.会亲友.安床.作灶.造畜椆栖"},
{5, "祭祀.祈福.求嗣.入学.开光.出行.起基.定磉.放水.交易.立券.纳财"},
{6, "祭祀.求医.治病.破土.安葬"},
{7, "修造.动土.起基.安门.安床.造庙.祭祀.会亲友.造畜椆栖.牧养"},
{8, "祈福.斋醮.出行.嫁娶.订盟.纳采.入殓.移柩.破土.安葬.立碑.结网.捕捉.畋猎"},
{9, "祭祀.入殓.移柩.启攒.安葬.立碑.开市.交易.立券.纳财.造仓"},
{10, "祭祀.祈福.求嗣.入殓.移柩.立碑.开光.理发"},
{11, "沐浴.破屋.坏垣.余事勿取"},
{12, "祭祀.求医.治病.破土.安葬"}
};
// 宜忌字典
private static readonly Dictionary<int, string> ji = new Dictionary<int, string>
{
{1, "入宅.动土.开仓.出货财.安葬.行丧.伐木.纳畜"},
{2, "祭祀.嫁娶.安床.开市.入学.探病.出火.纳畜.补垣.塞*"},
{3, "动土.伐木.作梁.行丧.安葬.开生坟"},
{4, "嫁娶.纳采.订盟.开市.造仓.作梁.祭祀.修造.动土.竖柱.上梁"},
{5, "安床.出行.嫁娶.移徙.入宅.修造.动土.竖柱.放水.会亲友"},
{6, "开光.出行.嫁娶.进人口.入宅.移徙.动土.安门.上梁.修造.纳财"},
{7, "入宅.上梁.修造.动土.安门.造车器.祭祀.会亲友.
继续
{8, "嫁娶.开市.动土.安床.入宅.上梁.修造.安门.祈福.祭祀.造仓"},
{9, "出行.移徙.入宅.动土.安葬.破土.祭祀.会亲友.安香.启攒"},
{10, "开市.动土.安床.开仓.上梁.修造.入殓.安葬.修坟.立碑.启攒"},
{11, "嫁娶.纳采.订盟.开市.出行.会亲友.牧养.安葬.入殓"},
{12, "祭祀.出行.成服.除服.沐浴.入殓"}
};
// 二十四节气
private static readonly string[] solarTerms =
{
"小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨",
"立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑",
"白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至"
};
// **
private static readonly string[] zodiac =
{
"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"
};
// 天干
private static readonly string[] heavenlyStems =
{
"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"
};
// 地支
private static readonly string[] earthlyBranches =
{
"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"
};
// 二十八星宿
private static readonly string[] lunarMansion =
{
"角木蛟", "亢金龙", "氐土貉", "房日兔", "心月狐", "尾火虎", "箕水豹", "斗木獬",
"牛金牛", "女土蝠", "虚日鼠", "危月燕", "室火猪", "壁水獝", "奎木狼", "娄金狗",
"胃土雉", "昴日鸡", "毕月乌", "觜火猴", "参水猿", "井木犴", "鬼金羊", "柳土獐",
"星日
继续
// 判断是否闰年
private static bool IsLeapYear(int year)
{
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
// 计算阳历年月日对应的星座
private static string GetZodiacSign(DateTime date)
{
int month = date.Month;
int day = date.Day;
switch (month)
{
case 1:
return day <= 20 ? "摩羯座" : "水瓶座";
case 2:
return day <= 19 ? "水瓶座" : "双鱼座";
case 3:
return day <= 20 ? "双鱼座" : "白羊座";
case 4:
return day <= 20 ? "白羊座" : "金牛座";
case 5:
return day <= 21 ? "金牛座" : "双子座";
case 6:
return day <= 21 ? "双子座" : "巨蟹座";
case 7:
return day <= 22 ? "巨蟹座" : "狮子座";
case 8:
return day <= 23 ? "狮子座" : "**座";
case 9:
return day <= 23 ? "**座" : "天秤座";
case 10:
return day <= 23 ? "天秤座" : "天蝎座";
case 11:
return day <= 22 ? "天蝎座" : "射手座";
default:
return day <= 21 ? "射手座" : "摩羯座";
}
}
// 计算指定日期对应的天干地支
private static string GetHeavenlyStemEarthlyBranch(DateTime date)
{
// 以甲子年为起点,获取指定年份的天干地支
int year = date.Year;
int offset = year - 4; // 甲子年为4年周期
int heavenlyStemIndex = (offset % 10) - 1;
if (heavenlyStemIndex < 0) heavenlyStemIndex = 9;
int earthlyBranchIndex = (offset % 12) - 1;
if (earthlyBranchIndex < 0) earthlyBranchIndex = 11;
return heavenlyStems[heavenlyStemIndex] + earthlyBranches[earthlyBranchIndex];
}
// 计算指定日期对应的农历年月日
private static string GetLunarDate(DateTime date)
{
int year = date.Year;
int month = date.Month;
int day = date.Day;
int leapMonth = GetLeapMonth(year); // 获取该年的闰月
scss
Copy code
// 计算1900年到指定年份的总天数
int daysCount = 0;
for (int y = 1900; y < year; y++)
{
daysCount += IsLeapYear(y) ? 366 : 365;
}
// 计算指定年份1月1日到指定日期的天数
继续
java
Copy code
int daysInMonth = 0;
for (int m = 1; m <= month; m++)
{
daysInMonth = GetDaysInLunarMonth(year, m, leapMonth);
if (m == leapMonth + 1 && IsLeapMonth(year, leapMonth))
{
daysCount += daysInMonth; // 如果该年有闰月,并且指定月份是闰月,天数要加上闰月天数
}
else
{
daysCount += daysInMonth;
}
}
// 获取指定日期的农历月份和日子
int lunarMonth = 0, lunarDay = 0;
for (int i = 1; i <= 12; i++)
{
int days = GetDaysInLunarMonth(year, i, leapMonth);
if (daysCount > days)
{
daysCount -= days;
}
else
{
lunarMonth = i;
lunarDay = daysCount;
break;
}
}
// 返回农历日期的字符串表示
return lunarMonth + "月" + GetLunarDay(lunarDay);
}
// 获取指定农历月份的天数
private static int GetDaysInLunarMonth(int year, int month, int leapMonth)
{
int monthCode = GetLunarMonthCode(year, month, leapMonth);
return monthCode >> 16 == 1 ? 30 : 29;
}
// 获取指定年份的闰月
private static int GetLeapMonth(int year)
{
return lunarInfo[year - lunarStartYear] & 0xf;
}
// 判断指定月份是否是闰月
private static bool IsLeapMonth(int year, int leapMonth)
{
return GetLeapMonth(year) == leapMonth;
}
// 获取指定农历日期的字符串表示
private static string GetLunarDay(int day)
{
string result = "";
if (day == 10)
{
result = "初十";
}
else
{
int d1 = day / 10;
int d2 = day % 10;
if (d1 > 0) result += lunarNumbers[d1];
result += lunarNumbers[d2];
}
return result;
}
// 获取指定农历月份的天干地支编码
private static int GetLunarMonthCode(int year, int month, int leapMonth)
{
int monthCode = (lunarInfo[year - lunarStartYear] & (0x10000 >> month)) != 0 ? 1 : 0;
if (month == leapMonth)
{
monthCode |= (lunarInfo[year - lunarStartYear] & 0x1000) != 0 ? 0x10000 : 0;
}
return monthCode;
}
// 天干数组
private static readonly string[] heavenlyStems = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
// 地支数组
private static readonly string[] earthlyBranches = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
// 农历数字数组
private static readonly string[]
继续
lunarNumbers = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十"};
// 天干地支纪年法起点年份
private static readonly int ganZhiStartYear = 1864;
// 天干地支纪年法起点月份
private static readonly int ganZhiStartMonth = 11;
// 天干地支纪年法起点日子
private static readonly int ganZhiStartDay = 15;
// 获取指定日期的天干地支
public static string GetGanZhi(DateTime date)
{
int offset = GetDaysOffset(date, new DateTime(ganZhiStartYear, ganZhiStartMonth, ganZhiStartDay));
return heavenlyStems[offset % 10] + earthlyBranches[offset % 12];
}
// 获取两个日期间相差的天数
private static int GetDaysOffset(DateTime date1, DateTime date2)
{
return (date1 - date2).Days;
}
// 获取指定日期的星座
public static string GetZodiac(DateTime date)
{
int month = date.Month;
int day = date.Day;
switch (month)
{
case 1:
return day <= 19 ? "摩羯座" : "水瓶座";
case 2:
return day <= 18 ? "水瓶座" : "双鱼座";
case 3:
return day <= 20 ? "双鱼座" : "白羊座";
case 4:
return day <= 19 ? "白羊座" : "金牛座";
case 5:
return day <= 20 ? "金牛座" : "双子座";
case 6:
return day <= 21 ? "双子座" : "巨蟹座";
case 7:
return day <= 22 ? "巨蟹座" : "狮子座";
case 8:
return day <= 22 ? "狮子座" : "**座";
case 9:
return day <= 22 ? "**座" : "天秤座";
case 10:
return day <= 23 ? "天秤座" : "天蝎座";
case 11:
return day <= 22 ? "天蝎座" : "射手座";
case 12:
return day <= 21 ? "射手座" : "摩羯座";
default:
return "";
}
}
// 获取指定日期的节气
public static string GetSolarTerm(DateTime date)
{
int year = date.Year;
int month = date.Month;
int
继续
int day = date.Day;
sql
Copy code
// 计算两个相邻节气之间的天数
double[] solarTermDays = new double[24] { 6.11, 21.06, 5.20, 21.21, 6.06, 22.12, 7.07, 23.07, 8.08, 23.23, 7.08, 23.23, 8.23, 23.08, 7.23, 23.08, 7.23, 22.23, 6.23, 22.07, 5.22, 21.06, 5.21, 21.22, 6.07 };
int startYear = 1900;
int offset = 0;
for (int i = 0; i < 24; i++)
{
double termDays = 365.2422 * (startYear - 1900) + solarTermDays;
DateTime termDate = new DateTime(startYear, 1, 1).AddDays(termDays - 1);
if (termDate.Month == month && termDate.Day == day)
{
return solarTerms;
}
else if (termDate.Month > month || (termDate.Month == month && termDate.Day > day))
{
offset = i - 1;
break;
}
}
return solarTerms[offset % 24];
}
// 获取指定日期的阴历日期
public static string GetLunarDate(DateTime date)
{
int year = date.Year;
int month = date.Month;
int day = date.Day;
java
Copy code
LunarDate lunarDate = LunarHelper.SolarToLunar(year, month, day);
// 阳历年份
string solarYear = year.ToString();
// 阴历月份
string lunarMonth = lunarNumbers[lunarDate.Month];
// 阴历日期
string lunarDay = lunarNumbers[lunarDate.Day];
string lunarDateString = lunarMonth + "月" + lunarDay;
// 判断是否为闰月
if (lunarDate.IsLeapMonth)
{
lunarDateString = "闰" + lunarMonth + "月" + lunarDay;
}
return lunarDateString;
}
// 获取指定日期的宜忌
public static string GetSuitableAndUnsuitable(DateTime date)
{
int year = date.Year;
int month = date.Month;
int day = date.Day;
java
Copy code
string zodiac = GetZodiac(date);
string lunarDate = GetLunarDate(date);
string ganZhi = GetGanZhi(date);
StringBuilder ** = new StringBuilder();
// 宜
**.Append("宜:");
switch (zodiac)
{
case "鼠":
**.Append("出行,交友,学习");
break;
case "牛":
**.Append("谈判,理财,投资");
break;
case "虎":
**.Append("旅游,运动,参加社交活动");
break;
case "兔":
**.Append("研究,思考,学习");
break;
case "龙":
**.Append("创业,运动,养生");
break;
case "蛇":
**.Append("旅游,健康,文学");
break;
case
继续
kotlin
Copy code
case "马":
**.Append("出游,交友,约会");
break;
case "羊":
**.Append("学习,读书,考试");
break;
case "猴":
**.Append("健身,旅游,理财");
break;
case "鸡":
**.Append("参加活动,交友,出游");
break;
case "狗":
**.Append("约会,出游,谈判");
break;
case "猪":
**.Append("办公,学习,理财");
break;
default:
break;
}
**.Append("\n");
// 忌
**.Append("忌:");
switch (ganZhi)
{
case "甲子":
case "乙丑":
case "丙午":
case "丁巳":
case "戊辰":
case "己辰":
case "庚寅":
case "辛卯":
case "壬戌":
case "癸亥":
**.Append("开业,入宅,纳财,安葬");
break;
case "甲戌":
case "乙亥":
case "丙辰":
case "丁巳":
case "戊午":
case "己午":
case "庚子":
case "辛丑":
case "壬申":
case "癸酉":
**.Append("嫁娶,订婚,出行,开工");
break;
default:
break;
}
return **.ToString();
}
// 获取指定日期的八字信息
public static string GetBazi(DateTime date)
{
int year = date.Year;
int month = date.Month;
int day = date.Day;
vbnet
Copy code
LunarDate lunarDate = LunarHelper.SolarToLunar(year, month, day);
int solarTermIndex = GetSolarTermIndex(date);
string solarTerm = solarTerms[solarTermIndex];
string zodiac = GetZodiac(date);
string lunarDateStr = GetLunarDate(date);
string ganZhi = GetGanZhi(date);
StringBuilder ** = new StringBuilder();
**.Append("阳历:" + date.ToString("yyyy年MM月dd日") + "\n");
**.Append("阴历:" + lunarNumbers[lunarDate.Month] + "月" + lunarNumbers[lunarDate.Day] + "\n");
if (lunarDate.IsLeapMonth)
{
**.Append("(闰)");
}
**.Append("**:" + zodiac + "\n");
**.Append("节气:" + solarTerm + "\n");
**.Append("干支:" + ganZhi + "\n");
**.Append(GetSuitableAndUnsuitable(date) + "\n");
return **.ToString();
}
// 测试
static void Main(string[] args)
{
DateTime date = new DateTime(2023, 3, 10);
Console.WriteLine(GetBazi(date));
} |
|