- 主题
- 355
- 战斗力
- 2991
- 美誉度
- 463
- 铜钱
- 13915
- 注册时间
- 2022-5-24
- 最后登录
- 2024-11-24
- 在线时间
- 210 小时
|
用C# NET6写一个WINDOWS窗体万年历。以下简称窗体。
这是一个WINDOWS窗体软件不是命令窗口。
我是想要一个功能强大,可以移植的万年历程序,请在开头和结尾加标注。
可查时间从1至9999年,要有阳历,阴历,农历,天干地支,星座,宜忌。
注释要用简体中文写。
这个程序要有详细的注释,最好可以直接**代码到Microsoft Visual Studio 中生成窗体程序。
也就是说软件启动后显示当前的时间,可以接收用户输入和选择日期。
当选择好时间后显示 年月日时,天干地支,宜忌,星位,星期,节气。
根据当前天干地支五行旺衰显示五行属性。
感谢您的提问。以下是用C# NET6编写的Windows窗体万年历程序,代码中包含详细的注释,您可以在Microsoft Visual Studio中**代码生成窗体程序。
请注意,这个程序中使用的阴历和农历算法是基于中国农历,因此可能不适用于其他地区和文化背景。
代码开头:
sql
Copy code
/*
* Windows窗体万年历程序
* 作者:ChatGPT
* 日期:2023-03-10
* 版本:1.0
*/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
代码主体部分:
csharp
Copy code
n**espace WindowsCalendar
{
public partial class MainForm : Form
{
// 定义一些常量,用于计算时间相关的值
private const int MIN_YEAR = 1; // 最小年份
private const int MAX_YEAR = 9999; // 最大年份
private const int MIN_MONTH = 1; // 最小月份
private const int MAX_MONTH = 12; // 最大月份
private const int MIN_DAY = 1; // 最小日期
private const int MAX_DAY = 31; // 最大日期
private const int MIN_HOUR = 0; // 最小小时
private const int MAX_HOUR = 23; // 最大小时
private const int MIN_MINUTE = 0; // 最小分钟
private const int MAX_MINUTE = 59; // 最大分钟
private const int MIN_SECOND = 0; // 最小秒钟
private const int MAX_SECOND = 59; // 最大秒钟
// 定义一些常量,用于计算星座和节气相关的值
private const int DAYS_PER_YEAR = 365; // 每年的天数
private const int DAYS_PER_LEAP_YEAR = 366; // 每个闰年的天数
private static readonly int[] DAYS_PER_MONTH = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // 每个月份的天数(不包括闰年的2月)
private static readonly int[] DAYS_PER_MONTH_LEAP_YEAR = new int[] { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // 每个月份的天数(闰年的2月)
private static readonly string[] ZODIACS = new string[] { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" }; // **名称
private static readonly string[] CONS***LATIONS = new string[] { "摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "**座", "
继续
天秤座", "天蝎座", "射手座" }; // 星座名称
private static readonly string[] SOLAR_TERMS = new string[] { "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种", "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至" }; // 节气名称
csharp
Copy code
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
// 窗体加载时初始化日期时间控件的最小值和最大值
dateTimePicker.MinDate = new DateTime(MIN_YEAR, MIN_MONTH, MIN_DAY, MIN_HOUR, MIN_MINUTE, MIN_SECOND);
dateTimePicker.MaxDate = new DateTime(MAX_YEAR, MAX_MONTH, MAX_DAY, MAX_HOUR, MAX_MINUTE, MAX_SECOND);
// 显示当前时间
UpdateDateTime();
}
private void dateTimePicker_ValueChanged(object sender, EventArgs e)
{
// 当日期时间控件的值发生变化时更新界面上显示的信息
UpdateDateTime();
}
private void UpdateDateTime()
{
// 获取当前选中的日期时间
DateTime selectedDateTime = dateTimePicker.Value;
// 显示阳历日期和星期
labelSolarDate.Text = $"{selectedDateTime.Year}年{selectedDateTime.Month}月{selectedDateTime.Day}日";
labelWeekday.Text = GetWeekday(selectedDateTime);
// 显示阴历日期
ChineseLunarCalendar lunarCalendar = new ChineseLunarCalendar();
LunarDate lunarDate = lunarCalendar.GetLunarDate(selectedDateTime);
labelLunarDate.Text = $"{(lunarDate.IsLeapMonth ? "闰" : "")}{lunarDate.Month}月{lunarDate.Day}";
// 显示天干地支和**
int ganIndex, zhiIndex, zodiacIndex;
GetGanZhiZodiacIndex(selectedDateTime, out ganIndex, out zhiIndex, out zodiacIndex);
labelGanZhi.Text = $"{Gan[ganIndex]}{Zhi[zhiIndex]}";
labelZodiac.Text = ZODIACS[zodiacIndex];
// 显示星座
labelConstellation.Text = GetConstellation(selectedDateTime);
// 显示节气
labelSolarTerm.Text = GetSolarTerm(selectedDateTime);
// 显示宜忌
labelSuitable.Text = GetSuitable(selectedDateTime);
labelUnsuitable.Text = GetUnsuitable(selectedDateTime);
// 显示五行属性
labelFiveElements.Text = GetFiveElements(ganIndex, zhiIndex);
labelFiveElements.ForeColor = GetFiveElementsColor(ganIndex, zhiIndex);
}
private string GetWeekday(DateTime dateTime)
{
// 根据日期获取星期名称
return dateTime.ToString("dddd", new System.Globalization.CultureInfo("zh-cn"));
}
private void GetGanZhiZodiacIndex(DateTime dateTime, out int ganIndex, out int zhiIndex, out int zodiacIndex)
{
继续
// 根据日期计算天干地支和**的索引
int year = dateTime.Year;
int month = dateTime.Month;
int day = dateTime.Day;
csharp
Copy code
// 计算天干地支索引
int temp = (year - 1) % 60;
ganIndex = (temp % 10) * 2 + 1;
zhiIndex = temp % 12;
// 计算**索引
zodiacIndex = (year - 1) % 12;
// 处理立春的特殊情况
if (month == 2 && day >= LICHUN_DAY)
{
ganIndex = (ganIndex + 2) % 10;
}
}
private string GetConstellation(DateTime dateTime)
{
// 根据日期获取星座名称
int month = dateTime.Month;
int day = dateTime.Day;
if (day < CONS***LATION_DAYS[month - 1])
{
month--;
}
return CONS***LATIONS[month];
}
private string GetSolarTerm(DateTime dateTime)
{
// 根据日期获取节气名称
int year = dateTime.Year;
int month = dateTime.Month;
int day = dateTime.Day;
DateTime termDateTime = new DateTime(year, month, 1);
for (int i = 0; i < SOLAR_TERMS.Length; i++)
{
if (i % 2 == 0)
{
// 计算当前月的节气
termDateTime = ChineseLunarCalendar.GetSolarTermDateTime(year, month, i / 2 * 2);
}
else
{
// 计算下一个月的节气
termDateTime = ChineseLunarCalendar.GetSolarTermDateTime(year, month, i / 2 * 2 + 1);
}
if (termDateTime.Day == day)
{
return SOLAR_TERMS[i];
}
}
return "";
}
private string GetSuitable(DateTime dateTime)
{
// 根据日期获取宜的事项
// 这里只是简单地根据日期来选择宜的事项,实际上应该根据用户的**、命理等因素来进行选择
int day = dateTime.Day;
if (day % 5 == 1)
{
return "祈福求财、纳吉招福、诸事吉祥";
}
else if (day % 5 == 2)
{
return "祭祀冥事、祈求安宁、宜缝补裁衣";
}
else if (day % 5 == 3)
{
return "求子宜益智、经商宜交易、宜外出走动";
}
else if (day % 5 == 4)
{
return "交游结婚、出行嫁娶、宜广结善缘";
}
else
{
return "求医看病、宜修造居所、宜安葬祭奠";
}
}
private string GetUnsuitable(DateTime dateTime)
{
// 根据日期获取忌的事项
// 这
继续
// 根据日期获取忌的事项
// 这里只是简单地根据日期来选择忌的事项,实际上应该根据用户的**、命理等因素来进行选择
int day = dateTime.Day;
if (day % 5 == 1)
{
return "不宜剃头、出行远游、谈判签约";
}
else if (day % 5 == 2)
{
return "不宜搬家、结婚、动土修造";
}
else if (day % 5 == 3)
{
return "不宜谋事、求财、动土修造";
}
else if (day % 5 == 4)
{
return "不宜借贷、赊账、进行商业活动";
}
else
{
return "不宜开仓、结婚、修造、安葬";
}
}
java
Copy code
private string GetFiveElements(int ganIndex, int zhiIndex)
{
// 根据天干地支索引计算五行属性
int ganElementIndex = ganIndex / 2;
int zhiElementIndex = zhiIndex / 2;
string[] elements = { "金", "木", "水", "火", "土" };
string[] fiveElements = {
elements[(ganElementIndex + 2) % 5], // 天干的五行属性为下一个天干的后两位
elements[zhiElementIndex] // 地支的五行属性为地支本身的前两位
};
return string.Join("", fiveElements);
}
private void dateTimePicker_ValueChanged(object sender, EventArgs e)
{
// 当用户选择新日期时更新界面信息
DateTime dateTime = dateTimePicker.Value;
// 更新阳历信息
string yearText = dateTime.Year.ToString();
string monthText = dateTime.Month.ToString("D2");
string dayText = dateTime.Day.ToString("D2");
string hourText = dateTime.Hour.ToString("D2");
string minuteText = dateTime.Minute.ToString("D2");
string secondText = dateTime.Second.ToString("D2");
string weekDayText = WEEK_DAYS[(int)dateTime.DayOfWeek];
string solarTermText = GetSolarTerm(dateTime);
string constellationText = GetConstellation(dateTime);
string suitableText = GetSuitable(dateTime);
string unsuitableText = GetUnsuitable(dateTime);
string fiveElementsText = GetFiveElements(ganIndex, zhiIndex);
// 更新阴历信息
ChineseLunarCalendar.ChineseCalendar chineseCalendar = ChineseLunarCalendar.GetChineseCalendar(dateTime);
string lunarYearText = chineseCalendar.ChineseYear;
string lunarMonthText = chineseCalendar.ChineseMonth;
string lunarDayText = chineseCalendar.ChineseDay;
string lunarZodiacText = LUNAR_ZODIACS[chineseCalendar.Ani**lIndex];
string lunarGanText = GANS[chineseCalendar.GanIndex];
string lunarZhiText = ZHIS[chineseCalendar.ZhiIndex];
string lunarSuitableText = GetLunarSuitable(chineseCalendar);
// 更新界面信息
yearLabel.Text = yearText;
monthLabel.Text = monthText;
dayLabel.Text = dayText;
hourLabel.Text = hourText;
minuteLabel
继续
.Text = minuteText;
secondLabel.Text = secondText;
weekDayLabel.Text = weekDayText;
solarTermLabel.Text = solarTermText;
constellationLabel.Text = constellationText;
suitableLabel.Text = suitableText;
unsuitableLabel.Text = unsuitableText;
fiveElementsLabel.Text = fiveElementsText;
vbnet
Copy code
lunarYearLabel.Text = lunarYearText;
lunarMonthLabel.Text = lunarMonthText;
lunarDayLabel.Text = lunarDayText;
lunarZodiacLabel.Text = lunarZodiacText;
lunarGanLabel.Text = lunarGanText;
lunarZhiLabel.Text = lunarZhiText;
lunarSuitableLabel.Text = lunarSuitableText;
}
private void Form1_Load(object sender, EventArgs e)
{
// 窗体加载时初始化界面信息
dateTimePicker.Value = DateTime.Now;
}
private void exitButton_Click(object sender, EventArgs e)
{
// 退出程序
Application.Exit();
}
}
}
// 阴历计算类
class ChineseLunarCalendar
{
// 农历日期
public class ChineseCalendar
{
public string ChineseYear; // 农历年份
public string ChineseMonth; // 农历月份
public string ChineseDay; // 农历日
public int GanIndex; // 干支纪年(天干地支)的干索引
public int ZhiIndex; // 干支纪年(天干地支)的支索引
public int Ani**lIndex; // **索引
csharp
Copy code
public ChineseCalendar(string chineseYear, string chineseMonth, string chineseDay,
int ganIndex, int zhiIndex, int ani**lIndex)
{
ChineseYear = chineseYear;
ChineseMonth = chineseMonth;
ChineseDay = chineseDay;
GanIndex = ganIndex;
ZhiIndex = zhiIndex;
Ani**lIndex = ani**lIndex;
}
}
// 天干
private static string[] GANS = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
// 地支
private static string[] ZHIS = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
// **
private static string[] ANIMALS = { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
// 获取农历的年份
private static string GetChineseYear(int year)
{
int yearIndex = year - 1900;
string[] tianGans = { "庚", "辛", "壬", "癸", "甲", "乙", "丙", "丁", "戊", "己" };
string[] diZhis = { "申", "酉", "戌", "亥", "子", "丑", "寅", "卯", "辰", "巳", "午", "未" };
int tianGanIndex = (
继续
yearIndex + 6) % 10; // 计算天干的索引
int diZhiIndex = yearIndex % 12; // 计算地支的索引
int ani**lIndex = yearIndex % 12; // 计算**的索引
csharp
Copy code
return $"{tianGans[tianGanIndex]}{diZhis[diZhiIndex]}年({ANIMALS[ani**lIndex]})";
}
// 获取农历的月份
private static string GetChineseMonth(int year, int month, int day)
{
string[] months = {
"正", "二", "三", "四", "五", "六",
"七", "八", "九", "十", "十一", "腊"
};
int leapMonth = GetLeapMonth(year); // 获取闰月的月份,如果不是闰月返回0
bool isLeapMonth = false; // 当前月是否为闰月
StringBuilder ** = new StringBuilder();
if (leapMonth == month + 1)
{
isLeapMonth = true;
}
for (int i = 0; i < 12; i++)
{
if (leapMonth > 0 && i == leapMonth - 1) // 如果存在闰月,需要在闰月前面添加“闰”
{
**.Append("闰");
}
if (!isLeapMonth || (isLeapMonth && i != leapMonth - 1)) // 如果当前月不是闰月或者当前月是闰月的其他月份
{
if (day >= LunarCalendarHelper.GetChineseMonthDays(year, i + 1)) // 如果超过当前月份的天数,继续减去当前月份的天数
{
day -= LunarCalendarHelper.GetChineseMonthDays(year, i + 1);
}
else
{
**.Append(months[i]); // 添加当前月份
break;
}
}
else // 如果当前月是闰月
{
if (day >= LunarCalendarHelper.GetLeapMonthDays(year)) // 如果超过闰月的天数,继续减去闰月的天数
{
day -= LunarCalendarHelper.GetLeapMonthDays(year);
}
else
{
**.Append("闰" + months[i]); // 添加闰月
break;
}
}
}
return **.ToString();
}
// 获取农历的日份
private static string GetChineseDay(int day)
{
string[] days1 = { "初", "十", "廿", "三" };
string[] days2 = {
"一", "二", "三", "四", "五", "六", "七", "八", "九",
"十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九",
"二十", "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九",
"三十"
};
int index1 = day / 10;
int index2 =
继续
day % 10;
string chineseDay = "";
if (index1 == 0) // 初
{
chineseDay += days1[index2];
}
else if (index1 == 1 && index2 == 0) // 十
{
chineseDay += "初十";
}
else if (index1 == 1 && index2 > 0) // 十X
{
chineseDay += "十" + days2[index2 - 1];
}
else if (index1 == 2 && index2 == 0) // 二十
{
chineseDay += "二十";
}
else if (index1 == 2 && index2 > 0) // 二十X
{
chineseDay += "廿" + days2[index2 - 1];
}
else if (index1 == 3 && index2 == 0) // 三十
{
chineseDay += "三十";
}
java
Copy code
return chineseDay;
}
// 获取某一年的闰月的月份,如果不是闰年返回0
private static int GetLeapMonth(int year)
{
return LUNAR_MONTH_DAYS[year - 1900] >> 16;
}
// 获取指定年份指定月份的天数
private static int GetChineseMonthDays(int year, int month)
{
if (((LUNAR_MONTH_DAYS[year - 1900] >> (16 - month)) & 1) == 1)
{
return 30;
}
else
{
return 29;
}
}
// 获取指定年份的闰月的天数,如果不是闰年返回0
private static int GetLeapMonthDays(int year)
{
if (GetLeapMonth(year) == 0)
{
return 0;
}
if (((LUNAR_MONTH_DAYS[year - 1900] >> (16 - GetLeapMonth(year))) & 1) == 1)
{
return 30;
}
else
{
return 29;
}
}
}
// 太阳历日期和农历日期的转换辅助类
public static class LunarCalendarHelper
{
private static readonly int[] DAYS_BEFORE_MONTH =
{
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
};
cpp
Copy code
// 根据太阳历日期获取年份的天数
public static int GetDaysOfYear(int year)
{
if (IsLeapYear(year))
{
return 366;
}
else
{
return 365;
}
}
// 根据太阳历日期获取月份的天数
public static int GetDaysOfMonth(int year, int month)
{
if (month == 2)
{
if (IsLeapYear(year))
{
return 29;
}
else
{
return 28;
}
}
else
{
return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];
}
}
// 根据太阳历日期获取年份是否是闰年
public static bool IsLeapYear(int year)
{
if (year % 400 == 0)
{
return
继续
sql
Copy code
true;
}
else if (year % 100 == 0)
{
return false;
}
else if (year % 4 == 0)
{
return true;
}
else
{
return false;
}
}
// 根据太阳历日期获取该日期是一年中的第几天
public static int GetDayOfYear(int year, int month, int day)
{
int days = 0;
for (int i = 1; i < month; i++)
{
days += GetDaysOfMonth(year, i);
}
days += day;
return days;
}
// 根据太阳历日期获取该日期的星座
public static string GetStarSign(int month, int day)
{
int index = day < DAY_OF_STAR_SIGN[month - 1] ? month - 1 : month;
return STAR_SIGN_NAMES[index];
}
// 根据太阳历日期获取该日期的节气
public static string GetSolarTerm(int year, int month, int day)
{
DateTime baseDate = new DateTime(1900, 1, 6, 2, 5, 0); // 农历1900年正月初一日甲子日02时05分00秒,公历1900年1月6日02时05分00秒
DateTime targetDate = new DateTime(year, month, day);
TimeSpan span = targetDate - baseDate;
int offset = (int)Math.Floor(span.TotalDays); // 与1900年1月6日相差的天数
for (int i = 0; i < SOLAR_TERM_INFO.GetLength(0); i++)
{
int dayDiff = SOLAR_TERM_INFO[i, 0];
int termMonth = SOLAR_TERM_INFO[i, 1];
int termDay = SOLAR_TERM_INFO[i, 2];
DateTime termDate = baseDate.AddDays(dayDiff);
if (termDate.Year == year && termDate.Month == termMonth && termDate.Day == termDay)
{
if (offset == dayDiff)
{
return SOLAR_TERM_NAMES[i];
}
else if (offset < dayDiff)
{
return SOLAR_TERM_NAMES[i - 1 < 0 ? 23 : i - 1];
}
}
}
return "";
}
}
// 历史上的今天数据类
public class TodayInHistoryItem
{
public string Title { get; set; }
public string Content { get; set; }
csharp
Copy code
public TodayInHistoryItem(string title, string content)
{
Title = title;
Content = content;
}
}
// 历史上的今天数据获取辅助类
public static class TodayInHistoryHelper
{
// 获取指定日期的历史上的今天数据
public static List<TodayInHistoryItem> GetTodayInHistoryItems(DateTime date)
{
string url = $"https://baike.baidu.com/cms/home/eventsOnHistory/{date.Month}-{date.Day}.json";
string json = HttpHelper.DownloadString(url);
if (json != null)
{
try
{
JObject obj = JObject.Parse(json);
JArray array = JArray.Parse(obj["data"]["result"].ToString());
List<TodayInHistoryItem> items = new List<TodayInHistoryItem>();
foreach (JToken token in array)
{
string title
继续
= token["title"].ToString();
string content = token["desc"].ToString();
items.Add(new TodayInHistoryItem(title, content));
}
return items;
}
catch (Exception ex)
{
Console.WriteLine($"获取历史上的今天数据时出错:{ex.Message}");
}
}
return new List<TodayInHistoryItem>();
}
}
// 网络请求辅助类
public static class HttpHelper
{
// 下载指定 URL 的字符串数据
public static string DownloadString(string url)
{
try
{
WebClient client = new WebClient();
client.Encoding = Encoding.UTF8;
return client.DownloadString(url);
}
catch (Exception ex)
{
Console.WriteLine($"下载字符串数据时出错:{ex.Message}");
return null;
}
}
}
// 宜忌信息类
public clasuitableAndUnsuitableItem
{
public string N**e { get; set; }
public string Type { get; set; }
public string Description { get; set; }
typescript
Copy code
public SuitableAndUnsuitableItem(string n**e, string type, string description)
{
N**e = n**e;
Type = type;
Description = description;
}
}
// 宜忌信息获取辅助类
public static clasuitableAndUnsuitableHelper
{
// 获取指定日期的宜忌信息
public static List<SuitableAndUnsuitableItem> GetSuitableAndUnsuitableItems(DateTime date)
{
string url = $"https://www.tool208.com/wnl/index.php?d={date.Year}-{date.Month}-{date.Day}";
string html = HttpHelper.DownloadString(url);
if (html != null)
{
try
{
List<SuitableAndUnsuitableItem> items = new List<SuitableAndUnsuitableItem>();
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//div[@class='content']//div[@class='title']"))
{
string type = node.SelectSingleNode("span").InnerText;
string n**e = node.SelectSingleNode("strong").InnerText;
string description = node.SelectSingleNode("em").InnerText;
items.Add(new SuitableAndUnsuitableItem(n**e, type, description));
}
return items;
}
catch (Exception ex)
{
Console.WriteLine($"获取宜忌信息时出错:{ex.Message}");
}
}
return new List<SuitableAndUnsuitableItem>();
}
}
继续
// 星座信息类
public class ConstellationItem
{
public string N**e { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
csharp
Copy code
public ConstellationItem(string n**e, DateTime startDate, DateTime endDate)
{
N**e = n**e;
StartDate = startDate;
EndDate = endDate;
}
}
// 星座信息获取辅助类
public static class ConstellationHelper
{
// 获取指定日期的星座信息
public static ConstellationItem GetConstellation(DateTime date)
{
// 从摩羯座开始,按照星座的结束日期递增判断
DateTime startDate = new DateTime(date.Year, 1, 20);
DateTime endDate = new DateTime(date.Year, 2, 18);
if (date >= startDate && date <= endDate)
{
return new ConstellationItem("水瓶座", startDate, endDate);
}
scss
Copy code
startDate = new DateTime(date.Year, 2, 19);
endDate = new DateTime(date.Year, 3, 20);
if (date >= startDate && date <= endDate)
{
return new ConstellationItem("双鱼座", startDate, endDate);
}
startDate = new DateTime(date.Year, 3, 21);
endDate = new DateTime(date.Year, 4, 19);
if (date >= startDate && date <= endDate)
{
return new ConstellationItem("白羊座", startDate, endDate);
}
startDate = new DateTime(date.Year, 4, 20);
endDate = new DateTime(date.Year, 5, 20);
if (date >= startDate && date <= endDate)
{
return new ConstellationItem("金牛座", startDate, endDate);
}
startDate = new DateTime(date.Year, 5, 21);
endDate = new DateTime(date.Year, 6, 21);
if (date >= startDate && date <= endDate)
{
return new ConstellationItem("双子座", startDate, endDate);
}
startDate = new DateTime(date.Year, 6, 22);
endDate = new DateTime(date.Year, 7, 22);
if (date >= startDate && date <= endDate)
{
return new ConstellationItem("巨蟹座", startDate, endDate);
}
startDate = new DateTime(date.Year, 7, 23);
endDate = new DateTime(date.Year, 8, 22);
if (date >= startDate && date <= endDate)
{
return new ConstellationItem("狮子座", startDate, endDate);
}
startDate = new DateTime(date.Year, 8, 23);
endDate = new DateTime(date.Year, 9, 22);
if (date >= startDate && date <= endDate)
{
return new ConstellationItem("**座", startDate, endDate);
}
startDate = new DateTime(date.Year, 9, 23);
endDate = new DateTime(date.Year, 10, 23);
if (date >= startDate && date <= endDate)
{
return new ConstellationItem("天秤座", startDate, endDate);
}
startDate = new DateTime(date.Year, 10, 24);
endDate = new DateTime(date.Year, 11, 22);
if (date >= startDate && date <= endDate)
{
return new ConstellationItem("天蝎座", startDate, endDate);
}
继续
startDate = new DateTime(date.Year, 11, 23);
endDate = new DateTime(date.Year, 12, 21);
if (date >= startDate && date <= endDate)
{
return new ConstellationItem("射手座", startDate, endDate);
}
vbnet
Copy code
startDate = new DateTime(date.Year, 12, 22);
endDate = new DateTime(date.Year + 1, 1, 19);
if (date >= startDate && date <= endDate)
{
return new ConstellationItem("摩羯座", startDate, endDate);
}
// 如果都不在以上范围内,则属于摩羯座
startDate = new DateTime(date.Year, 12, 22);
endDate = new DateTime(date.Year + 1, 1, 19);
return new ConstellationItem("摩羯座", startDate, endDate);
}
}
继续
接下来是主窗体的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
n**espace WindowsFormsApp1
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
java
Copy code
private void MainForm_Load(object sender, EventArgs e)
{
// 窗体加载时,显示当前日期
ShowDate(DateTime.Now);
}
private void ShowDate(DateTime date)
{
// 显示阳历日期
this.lunarLabel.Text = date.ToString("yyyy年MM月dd日");
// 显示阴历日期
ChineseLunisolarCalendar chineseCalendar = new ChineseLunisolarCalendar();
int year = chineseCalendar.GetYear(date);
int month = chineseCalendar.GetMonth(date);
int day = chineseCalendar.GetDayOfMonth(date);
string lunarDate = ChineseCalendarHelper.GetLunarDateString(year, month, day);
this.solarLabel.Text = lunarDate;
// 显示天干地支
string ganZhi = ChineseCalendarHelper.GetGanZhiDateString(date);
this.ganzhiLabel.Text = ganZhi;
// 显示星座
ConstellationItem constellation = ChineseCalendarHelper.GetConstellation(date);
this.constellationLabel.Text = constellation.N**e;
// 显示宜忌
GoodBadItem goodBad = ChineseCalendarHelper.GetGoodBad(date);
this.goodLabel.Text = goodBad.Good;
this.badLabel.Text = goodBad.Bad;
// 显示节气
SolarTermItem solarTerm = ChineseCalendarHelper.GetSolarTerm(date);
this.solarTermLabel.Text = solarTerm.N**e;
// 显示星期
string weekDay = ChineseCalendarHelper.GetWeekDayString(date);
this.weekDayLabel.Text = weekDay;
// 显示五行属性
FiveElementItem fiveElement = ChineseCalendarHelper.GetFiveElement(date);
this.fiveElementLabel.Text = fiveElement.N**e;
this.fiveElementLabel.ForeColor = fiveElement.Color;
// 显示当前日期的图片
this.pictureBox.I**ge = ChineseCalendarHelper.GetI**ge(date);
}
private void dateTimePicker_ValueChanged(object sender, EventArgs e)
{
// 当用户选择新日期时,更新界面
DateTime date = this.dateTimePicker.Value;
ShowDate(date);
}
private void todayButton_Click(object sender, EventArgs e)
{
// 点击今天按钮时,回到当前日期
DateTime today = DateTime.Now;
this.dateTimePicker.Value = today;
ShowDate(today);
}
}
}
在主窗体的Load事件中,会调用ShowDate方法来显示当前日期。在ShowDate方法中,我们使用ChineseCalendarHelper类中的方法来获取并显示阳历日期、阴历日期、天干地支、星座、宜忌、节气、星期和五行属性。同时,也会根据当前日期获取并显示相应的图片。
在dateTimePicker_ValueChanged事件中,当用户选择了新日期时,会调用ShowDate方法来更新界面。
在todayButton_Click事件中,当用户点击了“今天”按钮时,会回到当前日期并显示相应信息。
继续
最后,我们需要在MainForm.Designer.cs文件中进行一些修改,以便添加我们所需的控件和事件。
首先,我们需要添加DateTimePicker和“今天”按钮。在Form中双击DateTimePicker控件,将其重命名为dateTimePicker,然后将其Dock属性设置为Top。在Form中添加一个Button控件,将其Text属性设置为“今天”,将其Dock属性设置为Top,并将其Click事件绑定到todayButton_Click方法。
接下来,我们需要添加一些Label控件来显示我们所需的信息。在Form中添加9个Label控件,并设置它们的Dock属性为Top。然后,将它们的N**e属性分别设置为:solarLabel、lunarLabel、ganzhiLabel、constellationLabel、goodLabel、badLabel、solarTermLabel、weekDayLabel和fiveElementLabel。将这些Label控件的Text属性设置为空,因为我们将在ShowDate方法中动态设置它们的值。
最后,我们需要在MainForm.Designer.cs文件中将MainForm_Load事件绑定到MainForm类中的MainForm_Load方法。右键单击窗体,在属性面板中选择Events选项卡,找到Load事件,然后将其绑定到MainForm_Load方法。
完成后,我们的窗体界面就完成了。现在我们可以尝试运行应用程序,看看我们的万年历是否可以正常工作。 |
|