/// <summary> /// 用户选择了日期,修改选择的日期,并将月历隐藏 /// </summary> private void monthCalendar_DateSelected(object sender, EventArgs e) { date = monthCalendar.SelectedLunarDate; //修改选择的日期 formCalendar.Visible = false; monthCalendar.Visible = false; Refresh(); }
/// <summary> /// 更新这一页的月历信息 /// </summary> private void GetData() { var cld = new ChineseCalendar.ChineseCalendar(new DateTime(date.SolarDate.Year, date.SolarDate.Month, 1)); monthStartIndex = (int)cld.WeekDay; monthEndIndex = monthStartIndex + cld.SolarDate.AddMonths(1).AddDays(-1).Day - 1; cld.SolarDate = cld.SolarDate.AddDays(0 - monthStartIndex); firstDay = cld.SolarDate; //这一页月历的第一天所在的日期 //最小日期出现在该页 startIndex = GetIndex(minDate); //最大日期出现在该页 endIndex = GetIndex(maxDate); if (endIndex == -1) //endIndex用42表示不在该页 { endIndex = 42; } //选择的日期出现在该页,更新selectedIndex selectedIndex = GetIndex(selectedDate.SolarDate); //今天的索引 todayIndex = GetIndex(todayDate.SolarDate); //更新数据 for (int i = 0; i < 42; i++) { FormatDayInfo(ref cld, i); //格式化DayInfo cld.SolarDate = cld.SolarDate.AddDays(1); } }
/// <summary> /// 获得dt这一天的所有节日, /// </summary> private string GetHoliday(ref ChineseCalendar.ChineseCalendar date, out string sh, out string lh, out string wh, out string st) { string str = ""; sh = date.SolarDateHoliday; lh = date.LunarDateHoliday; wh = date.WeekDayHoliday; st = date.TwentyFourSolarTerms; if (lh != "") { str += lh; } if (st != "") { if (str != "") { str += " " + st; } else { str += st; } } if (sh != "") { if (str != "") { str += " " + sh; } else { str += sh; } } if (wh != "") { if (str != "") { str += " " + wh; } else { str += wh; } } for (int i = 0; i < str.Length; i++) { if (str.Substring(i, 1) == " ") { str = str.Remove(i, 1); str = str.Insert(i, "\r\n"); i++; } } return(str); }
/// <summary> /// 构造函数 /// </summary> public MyMonthCalendar() { InitializeComponent(); //初始化最小日期和最大日期 minDate = MinSolarDate; maxDate = MaxSolarDate; //选择的日期默认为今天 selectedDate = new ChineseCalendar.ChineseCalendar(DateTime.Today); graphics = CreateGraphics(); //创建Graphics对象 //设置为1号 date.SolarDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1); GetData(); //更新数据 selectedIndex = todayIndex; //初始时,将选择的日期设置今天的日期。 BackColor = Color.White; //默认背景颜色为白色 Paint += MyMonthCalendar_Paint; Resize += MyMonthCalendar_Resize; DateSelected += MyMonthCalendar_DateSelected; size = Size; //记录当前大小 penToday.Width = 2; penMouseIn.Width = 2; penSelected.Width = 2; }
/// <summary> /// 格式化日期中的信息 /// </summary> /// <remarks> /// 为DayInfo赋值 /// </remarks> private void FormatDayInfo(ref ChineseCalendar.ChineseCalendar date, int i) { string sh, lh, wh, st; dayInfo[i].day = date.SolarDate.Day.ToString(); if (date.LunarDayString == "初一") { dayInfo[i].lunarDay = date.LunarMonthString + "月"; } else { dayInfo[i].lunarDay = date.LunarDayString; } //获取所有节日 dayInfo[i].holidayInfo = GetHoliday(ref date, out sh, out lh, out wh, out st); if (sh != "" || wh != "") { dayInfo[i].hightLightS = true; } else { dayInfo[i].hightLightS = false; } if (lh != "" || st != "") { dayInfo[i].hightLightL = true; } else { dayInfo[i].hightLightL = false; } //要显示的重要节日 //优先级24节气 < 公历节日 < 农历节日 //24节气 if (st != "") { dayInfo[i].lunarDay = st; if (st == "清明") { dayInfo[i].hightLightS = true; } } //公历节日 if (sh != "") { foreach (SolarHolidayStruct shs in solarHolidayInfo) { if ((shs.Month == date.SolarDate.Month) && (shs.Day == date.SolarDate.Day)) { dayInfo[i].lunarDay = shs.HolidayName; dayInfo[i].hightLightL = true; break; } } } //农历节日 if (lh != "") { foreach (LunarHolidayStruct lhs in lunarHolidayInfo) { if ((lhs.Month == date.LunarMonth) && (lhs.Day == date.LunarDay)) { dayInfo[i].lunarDay = lhs.HolidayName; dayInfo[i].hightLightS = true; break; } } } }