From f3f48d0de3cf7501e4f60ebf662d8408531b6def Mon Sep 17 00:00:00 2001 From: Sunny Date: Tue, 8 Nov 2022 16:10:58 +0800 Subject: [PATCH] =?UTF-8?q?*=20UIDatePicker:=20=E5=A2=9E=E5=8A=A0MaxDate,M?= =?UTF-8?q?inDate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SunnyUI/Controls/DropItem/UIDateItem.cs | 81 +++++++++++++-- SunnyUI/Controls/UIDatePicker.cs | 129 ++++++++++++++++++++++++ 2 files changed, 204 insertions(+), 6 deletions(-) diff --git a/SunnyUI/Controls/DropItem/UIDateItem.cs b/SunnyUI/Controls/DropItem/UIDateItem.cs index a5a7820b..be4db899 100644 --- a/SunnyUI/Controls/DropItem/UIDateItem.cs +++ b/SunnyUI/Controls/DropItem/UIDateItem.cs @@ -336,6 +336,9 @@ namespace Sunny.UI base.SetDPIScale(); } + public DateTime max = DateTime.MaxValue; + public DateTime min = DateTime.MinValue; + public void Translate() { months.Clear(); @@ -357,6 +360,18 @@ namespace Sunny.UI { if (TabControl.SelectedIndex > 0) { + if (TabControl.SelectedIndex == 2) + { + if (new DateTime(Year, Month, 1) > max) return; + if (new DateTime(Year, Month, 1).EndOfMonth() < min) return; + } + + if (TabControl.SelectedIndex == 1) + { + if (Year < min.Year) return; + if (Year > max.Year) return; + } + TabControl.SelectedIndex--; activeDay = -1; } @@ -581,15 +596,21 @@ namespace Sunny.UI int left = width * (i % 4); int top = height * (i / 4); - SizeF sf = e.Graphics.MeasureString(months[i], Font); - if (i + 1 == Month) + Color color = ForeColor; + if (new DateTime(Year, i + 1, 1) > max || new DateTime(Year, i + 1, 1).EndOfMonth() < min) { - e.Graphics.DrawString(months[i], Font, PrimaryColor, left + (width - sf.Width) / 2, top + (height - sf.Height) / 2); + color = Color.DarkGray; } else { - e.Graphics.DrawString(months[i], Font, i == activeMonth ? PrimaryColor : ForeColor, left + (width - sf.Width) / 2, top + (height - sf.Height) / 2); + if (i + 1 == Month) color = PrimaryColor; + if (i + 1 == Month) color = PrimaryColor; + if (i == activeMonth) color = PrimaryColor; + if (i == activeMonth) color = PrimaryColor; } + + SizeF sf = e.Graphics.MeasureString(months[i], Font); + e.Graphics.DrawString(months[i], Font, color, left + (width - sf.Width) / 2, top + (height - sf.Height) / 2); } } @@ -601,6 +622,10 @@ namespace Sunny.UI int y = e.Location.Y / height; Month = x + y * 4 + 1; if (Month <= 0 || Month > 12) return; + + if (new DateTime(Year, Month, 1) > max) return; + if (new DateTime(Year, Month, 1).EndOfMonth() < min) return; + SetYearMonth(Year, Month); activeMonth = -1; @@ -625,6 +650,11 @@ namespace Sunny.UI int x = e.Location.X / width; int y = e.Location.Y / height; int im = x + y * 4; + + if (im + 1 <= 0 || im + 1 > 12) return; + if (new DateTime(Year, im + 1, 1) > max) return; + if (new DateTime(Year, im + 1, 1).EndOfMonth() < min) return; + if (activeMonth != im) { activeMonth = im; @@ -647,9 +677,21 @@ namespace Sunny.UI SizeF sf = e.Graphics.MeasureString(years[i].ToString(), Font); Color color = (i == 0 || i == 11) ? Color.DarkGray : ForeColor; + if (years[i] < min.Year || years[i] > max.Year) + { + color = Color.DarkGray; + } + else + { + if (years[i] == Year) color = PrimaryColor; + if (years[i] == Year) color = PrimaryColor; + if (i == activeYear) color = PrimaryColor; + if (i == activeYear) color = PrimaryColor; + } + if (years[i] != 10000) { - e.Graphics.DrawString(years[i].ToString(), Font, (i == activeYear || years[i] == Year) ? PrimaryColor : color, left + (width - sf.Width) / 2, top + (height - sf.Height) / 2); + e.Graphics.DrawString(years[i].ToString(), Font, color, left + (width - sf.Width) / 2, top + (height - sf.Height) / 2); } } } @@ -661,6 +703,10 @@ namespace Sunny.UI int x = e.Location.X / width; int y = e.Location.Y / height; int iy = x + y * 4; + + if (years[iy] < min.Year) return; + if (years[iy] > max.Year) return; + if (activeYear != iy) { activeYear = iy; @@ -676,6 +722,9 @@ namespace Sunny.UI int y = e.Location.Y / height; int iy = x + y * 4; if (iy < 0 || iy >= 12) return; + if (years[iy] < min.Year) return; + if (years[iy] > max.Year) return; + Year = years[iy] > 9999 ? 9999 : years[iy]; activeYear = -1; @@ -723,6 +772,8 @@ namespace Sunny.UI sf = e.Graphics.MeasureString(days[i].Day.ToString(), Font); Color color = (days[i].Month == Month) ? ForeColor : Color.DarkGray; color = (days[i].DateString() == date.DateString()) ? PrimaryColor : color; + if (days[i] < min) color = Color.DarkGray; + if (days[i] > max) color = Color.DarkGray; if (!maxDrawer) { @@ -768,6 +819,17 @@ namespace Sunny.UI int x = e.Location.X / width; int y = (e.Location.Y - 30) / height; int iy = x + y * 7; + + if (iy.InRange(0, days.Count)) + { + if (days[iy] < min) return; + if (days[iy] > max) return; + } + else + { + return; + } + bool istoday = ShowToday && e.Location.Y > p3.Top + p3.Height - height && e.Location.X > p3.Left + width * 3; if (activeDay != iy || istoday != isToday) @@ -787,12 +849,19 @@ namespace Sunny.UI int y = (e.Location.Y - 30) / height; int id = x + y * 7; if (id < 0 || id >= 42) return; - date = days[id].Date; if (ShowToday && e.Location.Y > p3.Height - height && e.Location.X > p3.Width - width * 4) { + if (DateTime.Now.Date < min) return; + if (DateTime.Now.Date > max) return; date = DateTime.Now.Date; } + else + { + if (days[id].Date < min) return; + if (days[id].Date > max) return; + date = days[id].Date; + } DoValueChanged(this, Date); Close(); diff --git a/SunnyUI/Controls/UIDatePicker.cs b/SunnyUI/Controls/UIDatePicker.cs index d19d7cd1..0c44a1f6 100644 --- a/SunnyUI/Controls/UIDatePicker.cs +++ b/SunnyUI/Controls/UIDatePicker.cs @@ -20,10 +20,12 @@ * 2020-08-07: V2.2.7 可编辑输入,日期范围控制以防止出错 * 2021-04-15: V3.0.3 增加ShowToday显示今日属性 * 2021-08-14: V3.0.6 增加可选择年、年月、年月日 + * 2022-11-08: V3.2.8 增加MaxDate,MinDate ******************************************************************************/ using System; using System.ComponentModel; +using System.Globalization; using System.Windows.Forms; namespace Sunny.UI @@ -46,6 +48,131 @@ namespace Sunny.UI CreateInstance(); } + private DateTime max = DateTime.MaxValue; + private DateTime min = DateTime.MinValue; + + static internal DateTime EffectiveMaxDate(DateTime maxDate) + { + DateTime maxSupportedDate = DateTimePicker.MaximumDateTime; + if (maxDate > maxSupportedDate) + { + return maxSupportedDate; + } + return maxDate; + } + + static internal DateTime EffectiveMinDate(DateTime minDate) + { + DateTime minSupportedDate = DateTimePicker.MinimumDateTime; + if (minDate < minSupportedDate) + { + return minSupportedDate; + } + return minDate; + } + + [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] + public static readonly DateTime MinDateTime = new DateTime(1753, 1, 1); + + [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] + public static readonly DateTime MaxDateTime = new DateTime(9998, 12, 31); + + [DefaultValue(typeof(DateTime), "9998/12/31")] + [Description("最大日期"), Category("SunnyUI")] + public DateTime MaxDate + { + get + { + return EffectiveMaxDate(max); + } + set + { + if (value != max) + { + if (value < EffectiveMinDate(min)) + { + value = EffectiveMinDate(min); + } + + // If trying to set the maximum greater than MaxDateTime, throw. + if (value > MaximumDateTime) + { + value = MaximumDateTime; + } + + max = value; + + //If Value (which was once valid) is suddenly greater than the max (since we just set it) + //then adjust this... + if (Value > max) + { + Value = max; + } + } + } + } + + [DefaultValue(typeof(DateTime), "1753/1/1")] + [Description("最小日期"), Category("SunnyUI")] + public DateTime MinDate + { + get + { + return EffectiveMinDate(min); + } + set + { + if (value != min) + { + if (value > EffectiveMaxDate(max)) + { + value = EffectiveMaxDate(max); + } + + // If trying to set the minimum less than MinimumDateTime, throw. + if (value < MinimumDateTime) + { + value = MinimumDateTime; + } + + min = value; + + //If Value (which was once valid) is suddenly less than the min (since we just set it) + //then adjust this... + if (Value < min) + { + Value = min; + } + } + } + } + + internal static DateTime MaximumDateTime + { + get + { + DateTime maxSupportedDateTime = CultureInfo.CurrentCulture.Calendar.MaxSupportedDateTime; + if (maxSupportedDateTime.Year > MaxDateTime.Year) + { + return MaxDateTime; + } + return maxSupportedDateTime; + } + } + + internal static DateTime MinimumDateTime + { + get + { + DateTime minSupportedDateTime = CultureInfo.CurrentCulture.Calendar.MinSupportedDateTime; + if (minSupportedDateTime.Year < 1753) + { + return new DateTime(1753, 1, 1); + } + return minSupportedDateTime; + } + } + [DefaultValue(false)] [Description("日期输入时,是否可空显示"), Category("SunnyUI")] public bool CanEmpty { get; set; } @@ -177,6 +304,8 @@ namespace Sunny.UI item.Translate(); item.SetDPIScale(); item.SetStyleColor(UIStyles.ActiveStyleColor); + item.max = MaxDate; + item.min = MinDate; ItemForm.Show(this); }