winform --自定义日期控件
新建一个用户控件,然后直接上代码:
public partial class UCCalendarControl : UserControl { public event MouseEventHandler ChildItemMouseClick = null;//子项鼠标事件 public int bb { get; set; } private Panel panel_Container { get; set; } private Panel panel_Top { get; set; } #region 日期最上层top private PictureBox YearPicLeft { get; set; } private PictureBox MonthPicLeft { get; set; } private PictureBox MonthPicRight { get; set; } private PictureBox YearPicRight { get; set; } private Label label_Year { get; set; } private Label label_Month { get; set; } private Label YM_Label { get; set; } #endregion #region 时分秒选择 private Label labelHour { get; set; } private Label labelMin { get; set; } private Label labelSec { get; set; } private Control HourControl { get; set; } private Control MinControl { get; set; } private Control SecControl { get; set; } private FlowLayoutPanel flowHour { get; set; } private FlowLayoutPanel flowMin { get; set; } private FlowLayoutPanel flowSec { get; set; } #endregion private Panel panel_Bottom { get; set; } private Panel panel_Bottom_Top { get; set; } private Panel panel_Bottom_Bottom { get; set; } private Panel panel_Bottom_Top_Top { get; set; } private Panel panel_Bottom_Top_Bottom { get; set; } private string timeFormat { get; set; }="yyyy-MM-dd"; [Description("时间格式"), Category("自定义")] public string TimeFormat { get { return timeFormat; } set { if (value != "yyyy-MM-dd" || value != "yyyy-MM-dd HH:mm:ss") timeFormat = value; else { MessageBox.Show("日期格式为:yyyy-MM-dd 或者 yyyy-MM-dd HH:mm:ss"); this.timeFormat = "yyyy-MM-dd"; value = "yyyy-MM-dd"; } } } private Label label_Date { get; set; } public DateTime CurrentTime { get; set; } = DateTime.Now; public string TimeValue { get { if (Input_Time != null) return Input_Time.Text.ToString(); else return ""; } } private Control CurrentControl { get; set; } private int TimeType { get; set; } = (int)EnumDate.日; private Popup popup { get; set; } public UCCalendarControl() { InitializeComponent(); this.Input_Time.Paint += Input_Time_Panit; this.Input_Time.Click += Input_Time_Click; ///this.Input_Time.MouseEnter += MouseEnterClick; // this.Input_Time.MouseLeave += MouseLeaveClick; this.Input_Time.Text = CurrentTime.ToString("yyyy-MM-dd HH:mm:ss"); Init(); } public void SetTime(DateTime time) { this.Input_Time.Text = time.ToString("yyyy-MM-dd HH:mm:ss"); } private void Input_Time_Click(object? sender, EventArgs e) { TimeType = (int)EnumDate.日; this.panel_Bottom_Top.Controls.Clear(); InitData(); popup = new Popup(panel_Container); popup.Show(this); } private void Input_Time_Panit(object? sender, PaintEventArgs e) { ControlPaint.DrawBorder(e.Graphics,//获取进行绘制的图形 new Rectangle(0, 0, this.Input_Time.Width, this.Input_Time.Height),//绘制控件所在工作区域 Color.FromArgb(138, 238, 238), 1, ButtonBorderStyle.Solid,//边框类型,选择实线边框,也有虚线等种类;下面这四个重复的传参是设置工作区上下左右边框的功能 Color.FromArgb(138, 238, 238), 1, ButtonBorderStyle.Solid, Color.FromArgb(138, 238, 238), 1, ButtonBorderStyle.Solid, Color.FromArgb(138, 238, 238), 1, ButtonBorderStyle.Solid); } #region 初始化数据 private void Init() { #region 日期容器,最外层 this.panel_Container = new Panel(); this.panel_Container.Padding = new Padding(10, 10, 10, 10); this.panel_Container.Height = 490; this.panel_Container.Width = 370; this.panel_Container.BackColor = Color.White; #region 最上方日期 Top this.panel_Top = new Panel(); this.panel_Top.Parent = this.panel_Container; this.panel_Top.Dock = DockStyle.Top; this.panel_Top.Height = 60; //this.panel_Top.BackColor = Color.Blue; #endregion #region 最下放 Bottom this.panel_Bottom = new Panel(); this.panel_Bottom.Dock = DockStyle.Bottom; this.panel_Bottom.Parent = this.panel_Container; this.panel_Bottom.Height = 410; // this.panel_Bottom.BackColor = Color.Red; #endregion #region 日期+天 this.panel_Bottom_Top = new Panel(); this.panel_Bottom_Top.Parent = this.panel_Bottom; this.panel_Bottom_Top.Dock = DockStyle.Top; this.panel_Bottom_Top.Height = 350; // this.panel_Bottom_Top.BackColor = Color.Green; #endregion #region 最下方的选择时间按钮和显示当前选中的日期 this.panel_Bottom_Bottom = new Panel(); this.panel_Bottom_Bottom.Parent = this.panel_Bottom; this.panel_Bottom_Bottom.Dock = DockStyle.Bottom; this.panel_Bottom_Bottom.Height = 60; //this.panel_Bottom_Bottom.Padding=new Padding(50,20,10,10); // this.panel_Bottom_Bottom.BackColor = Color.Gray; #endregion #endregion #region 最下方的日期显示,小时选择按钮,确定按钮 label_Date = new Label(); label_Date.Parent = this.panel_Bottom_Bottom; label_Date.Size = new Size(200, 60); label_Date.Text = CurrentTime.ToString("yyyy-MM-dd HH:mm:ss"); label_Date.TextAlign = ContentAlignment.MiddleCenter; label_Date.AutoSize = false; label_Date.Location = new Point(0, 0);//位置 Label label_SeletTime = new Label(); label_SeletTime.Parent = this.panel_Bottom_Bottom; label_SeletTime.Size = new Size(80, 30); label_SeletTime.Text = "选择时间"; label_SeletTime.TextAlign = ContentAlignment.MiddleCenter; label_SeletTime.AutoSize = false; label_SeletTime.Location = new Point(200, 15);//位置 label_SeletTime.Click += label_SeletTime_Click; label_SeletTime.MouseEnter += MouseEnterClick; label_SeletTime.MouseLeave += MouseLeaveClick; label_SeletTime.Paint += label_SeletTime_Painit; Label label_Sure = new Label(); label_Sure.Parent = this.panel_Bottom_Bottom; label_Sure.Size = new Size(60, 30); label_Sure.Text = "确定"; label_Sure.TextAlign = ContentAlignment.MiddleCenter; label_Sure.AutoSize = false; label_Sure.Location = new Point(280, 15);//位置 label_Sure.Click += label_Sure_Click; label_Sure.MouseEnter += MouseEnterClick; label_Sure.MouseLeave += MouseLeaveClick; label_Sure.MouseClick += ChildItemMouseClick; label_Sure.Paint += label_Sure_Painit; #endregion } #endregion #region 选择年月 private void Month_InitDataTop() { this.panel_Top.Controls.Remove(MonthPicRight); this.panel_Top.Controls.Remove(MonthPicLeft); this.panel_Top.Controls.Remove(label_Year); this.panel_Top.Controls.Remove(label_Month); YM_Label = new Label(); YM_Label.Size = new Size(150, 50); YM_Label.Tag = CurrentTime.Year; YM_Label.Text = TimeType == (int)EnumDate.年 ? CurrentTime.Year - 7 + " 年" + "~" + (CurrentTime.Year + 7) + " 年" : CurrentTime.Year + " 年"; YM_Label.TextAlign = ContentAlignment.MiddleCenter; YM_Label.Parent = this.panel_Top; YM_Label.AutoSize = false; YM_Label.Location = new Point(100, 0);//位置 SetYM(); } private void SetYM() { switch (TimeType) { case (int)EnumDate.月: this.panel_Bottom_Top.Controls.Clear(); int Mcount = 0; int Mrowcount = 15; for (int i = 1; i <= 12; i++) { Mcount++; Label label = new Label(); label.Size = new Size(80, 60); label.Text = i + "月"; label.Tag = i; if (i == CurrentTime.Month) { label.ForeColor = Color.White; label.BackColor = Color.FromArgb(0, 159, 149); } label.Click += Month_Click; label.MouseEnter += Month_MouseEnterClick; label.MouseLeave += Month_MouseLeaveClick; label.TextAlign = ContentAlignment.MiddleCenter; label.Parent = this.panel_Bottom_Top; label.AutoSize = false; label.Location = new Point((Mcount - 1) * 80 + Mcount * 10, Mrowcount);//位置 if (Mcount % 4 == 0) { Mcount = 0; Mrowcount += 115; } } break; case (int)EnumDate.年: this.panel_Bottom_Top.Controls.Clear(); int Ycount = 0; int Yrowcount = 1; for (int i = CurrentTime.Year - 7; i <= CurrentTime.Year + 7; i++) { Ycount++; Label label = new Label(); label.Size = new Size(116, 70); label.Text = i + "年"; label.Tag = i; if (i == CurrentTime.Year) { label.ForeColor = Color.White; label.BackColor = Color.FromArgb(0, 159, 149); } label.Click += Year_Click; label.MouseEnter += Year_MouseEnterClick; label.MouseLeave += Year_MouseLeaveClick; label.TextAlign = ContentAlignment.MiddleCenter; label.Parent = this.panel_Bottom_Top; label.AutoSize = false; label.Location = new Point((Ycount - 1) * 116, Yrowcount);//位置 if (Ycount % 3 == 0) { Ycount = 0; Yrowcount += 70; } } break; } } private void Year_MouseLeaveClick(object? sender, EventArgs e) { Label label = sender as Label; if ((int)YM_Label.Tag == CurrentTime.Year) { if ((int)label.Tag != CurrentTime.Year) label.BackColor = Color.White; } else { if ((int)label.Tag != (int)YM_Label.Tag) label.BackColor = Color.White; } } private void Year_MouseEnterClick(object? sender, EventArgs e) { Label label = sender as Label; label.Cursor = Cursors.Hand; if ((int)YM_Label.Tag == CurrentTime.Year) { if ((int)label.Tag != CurrentTime.Year) label.BackColor = Color.FromArgb(224, 224, 224); } else { if ((int)label.Tag != (int)YM_Label.Tag) label.BackColor = Color.FromArgb(224, 224, 224); } } private void Year_Click(object? sender, EventArgs e) { Label label = sender as Label; int year = (int)label.Tag - CurrentTime.Year; //获取去年这个月多少天 var dayscount = DateTime.DaysInMonth(CurrentTime.AddYears(year).Year, CurrentTime.AddYears(year).Month); CurrentTime = CurrentTime.Day <= dayscount ? new DateTime(CurrentTime.AddYears(year).Year, CurrentTime.AddYears(year).Month, CurrentTime.Day, CurrentTime.Hour, CurrentTime.Minute, CurrentTime.Second) : new DateTime(CurrentTime.AddYears(year).Year, CurrentTime.AddYears(year).Month, dayscount, CurrentTime.Hour, CurrentTime.Minute, CurrentTime.Second); TimeType = (int)EnumDate.日; InitDayTop(); this.panel_Bottom_Top.Controls.Clear(); InitData(); label_Year.Text = CurrentTime.Year + " 年"; label_Month.Text = CurrentTime.Month + " 月"; this.label_Date.Text = CurrentTime.ToString("yyyy-MM-dd HH:mm:ss"); } private void Month_MouseLeaveClick(object? sender, EventArgs e) { Label label = sender as Label; if ((int)label.Tag != CurrentTime.Month) label.BackColor = Color.White; } private void Month_MouseEnterClick(object? sender, EventArgs e) { Label label = sender as Label; label.Cursor = Cursors.Hand; if ((int)label.Tag != CurrentTime.Month) label.BackColor = Color.FromArgb(224, 224, 224); } private void Month_Click(object? sender, EventArgs e) { Label label = sender as Label; //获取上个月多少天 var dayscount = DateTime.DaysInMonth((int)YM_Label.Tag, (int)label.Tag); CurrentTime = CurrentTime.Day <= dayscount ? new DateTime((int)YM_Label.Tag, (int)label.Tag, CurrentTime.Day, CurrentTime.Hour, CurrentTime.Minute, CurrentTime.Second) : new DateTime((int)YM_Label.Tag, (int)label.Tag, dayscount, CurrentTime.Hour, CurrentTime.Minute, CurrentTime.Second); TimeType = (int)EnumDate.日; InitDayTop(); this.panel_Bottom_Top.Controls.Clear(); InitData(); label_Year.Text = CurrentTime.Year + " 年"; label_Month.Text = CurrentTime.Month + " 月"; this.label_Date.Text = CurrentTime.ToString("yyyy-MM-dd HH:mm:ss"); } #endregion #region 选择天 private void InitData() { #region 日期: 一 二 三 四 this.panel_Bottom_Top_Top = new Panel(); this.panel_Bottom_Top_Top.Parent = this.panel_Bottom_Top; this.panel_Bottom_Top_Top.Dock = DockStyle.Top; this.panel_Bottom_Top_Top.Height = 50; this.panel_Bottom_Top_Top.BorderStyle = BorderStyle.FixedSingle; // this.panel_Bottom_Top_Top.BackColor = Color.Pink; #endregion #region 中间内容---最上方=Top this.panel_Bottom_Top_Bottom = new Panel(); this.panel_Bottom_Top_Bottom.Parent = this.panel_Bottom_Top; this.panel_Bottom_Top_Bottom.Dock = DockStyle.Bottom; this.panel_Bottom_Top_Bottom.Height = 300; // this.panel_Bottom_Top_Bottom.BackColor = Color.Black; #endregion InitDayTop(); Day_InitWeek(); SetDay(CurrentTime); } private void Day_InitWeek() { string[] strWeek = new string[] { "一", "二", "三", "四", "五", "六", "日" }; for (int i = 0; i < strWeek.Length; i++) { Label label = new Label(); label.Size = new Size(50, 50); label.Text = strWeek[i]; label.TextAlign = ContentAlignment.MiddleCenter; label.Parent = this.panel_Bottom_Top_Top; label.AutoSize = false; label.Location = new Point((i) * 50, 0);//位置 } } private void InitDayTop() { this.panel_Top.Controls.Clear(); this.panel_Top.Padding = new Padding(0, 5, 0, 5); YearPicLeft = new PictureBox(); YearPicLeft.Size = new Size(50, 50); YearPicLeft.BackgroundImage = Properties.Resources.lefts; YearPicLeft.Parent = this.panel_Top; YearPicLeft.Location = new Point(0, 0); YearPicLeft.SizeMode = PictureBoxSizeMode.AutoSize; YearPicLeft.BackgroundImageLayout = ImageLayout.Center; YearPicLeft.Click += YearPicLeft_Click; YearPicLeft.MouseEnter += MouseEnterClick; YearPicLeft.MouseLeave += MouseLeaveClick; YearPicRight = new PictureBox(); YearPicRight.Size = new Size(50, 50); YearPicRight.BackgroundImage = Properties.Resources.rights; YearPicRight.Parent = this.panel_Top; YearPicRight.Location = new Point(300, 0); YearPicRight.SizeMode = PictureBoxSizeMode.AutoSize; YearPicRight.BackgroundImageLayout = ImageLayout.Center; YearPicRight.Click += YearPicRight_Click; YearPicRight.MouseEnter += MouseEnterClick; YearPicRight.MouseLeave += MouseLeaveClick; MonthPicLeft = new PictureBox(); MonthPicLeft.Size = new Size(50, 50); MonthPicLeft.BackgroundImage = Properties.Resources.left; MonthPicLeft.Parent = this.panel_Top; MonthPicLeft.Location = new Point(50, 0); MonthPicLeft.SizeMode = PictureBoxSizeMode.AutoSize; MonthPicLeft.BackgroundImageLayout = ImageLayout.Center; MonthPicLeft.Click += MonthPicLeft_Click; MonthPicLeft.MouseEnter += MouseEnterClick; MonthPicLeft.MouseLeave += MouseLeaveClick; label_Year = new Label(); label_Year.Size = new Size(75, 50); // label_Year.Font = new System.Drawing.Font("幼圆", 8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); label_Year.Text = CurrentTime.Year + " 年"; label_Year.TextAlign = ContentAlignment.MiddleCenter; label_Year.Parent = this.panel_Top; label_Year.AutoSize = false; label_Year.Location = new Point(100, 0);//位置 label_Year.Click += label_Year_Click; label_Year.MouseEnter += MouseEnterClick; label_Year.MouseLeave += MouseLeaveClick; label_Month = new Label(); label_Month.Size = new Size(75, 50); label_Month.Text = CurrentTime.Month + " 月"; label_Month.TextAlign = ContentAlignment.MiddleCenter; label_Month.Parent = this.panel_Top; label_Month.AutoSize = false; label_Month.Location = new Point(175, 0);//位置 label_Month.Click += label_Month_Click; label_Month.MouseEnter += MouseEnterClick; label_Month.MouseLeave += MouseLeaveClick; MonthPicRight = new PictureBox(); MonthPicRight.Size = new Size(50, 50); MonthPicRight.BackgroundImage = Properties.Resources.right; MonthPicRight.Parent = this.panel_Top; MonthPicRight.Location = new Point(250, 0); MonthPicRight.SizeMode = PictureBoxSizeMode.AutoSize; MonthPicRight.BackgroundImageLayout = ImageLayout.Center; MonthPicRight.Click += MonthPicRight_Click; MonthPicRight.MouseEnter += MouseEnterClick; MonthPicRight.MouseLeave += MouseLeaveClick; } private void MonthPicRight_Click(object? sender, EventArgs e) { switch (TimeType) { case (int)EnumDate.日: //获取上个月多少天 int dayscount = DateTime.DaysInMonth(CurrentTime.AddMonths(1).Year, CurrentTime.AddMonths(1).Month); CurrentTime = CurrentTime.Day <= dayscount ? new DateTime(CurrentTime.AddMonths(1).Year, CurrentTime.AddMonths(1).Month, CurrentTime.Day, CurrentTime.Hour, CurrentTime.Minute, CurrentTime.Second) : new DateTime(CurrentTime.AddMonths(1).Year, CurrentTime.AddMonths(1).Month, dayscount, CurrentTime.Hour, CurrentTime.Minute, CurrentTime.Second); label_Year.Text = CurrentTime.Year + " 年"; label_Month.Text = CurrentTime.Month + " 月"; this.label_Date.Text = CurrentTime.ToString("yyyy-MM-dd HH:mm:ss"); SetDay(CurrentTime); break; case (int)EnumDate.月: YM_Label.Text = (int)YM_Label.Tag + 1 + " 年"; YM_Label.Tag = (int)YM_Label.Tag + 1; break; case (int)EnumDate.年: int year = (int)YM_Label.Tag + 77; for (int i = 0; i < panel_Bottom_Top.Controls.Count; i++) { panel_Bottom_Top.Controls[i].Text = year + "年"; panel_Bottom_Top.Controls[i].Tag = year; year++; } YM_Label.Tag = (int)YM_Label.Tag + 7; break; default: break; } } private void label_Month_Click(object? sender, EventArgs e) { TimeType = (int)EnumDate.月; Month_InitDataTop(); } private void label_Year_Click(object? sender, EventArgs e) { TimeType = (int)EnumDate.年; Month_InitDataTop(); } private void MonthPicLeft_Click(object? sender, EventArgs e) { //获取上个月多少天 var dayscount = DateTime.DaysInMonth(CurrentTime.AddMonths(-1).Year, CurrentTime.AddMonths(-1).Month); CurrentTime = CurrentTime.Day <= dayscount ? new DateTime(CurrentTime.AddMonths(-1).Year, CurrentTime.AddMonths(-1).Month, CurrentTime.Day, CurrentTime.Hour, CurrentTime.Minute, CurrentTime.Second) : new DateTime(CurrentTime.AddMonths(-1).Year, CurrentTime.AddMonths(-1).Month, dayscount, CurrentTime.Hour, CurrentTime.Minute, CurrentTime.Second); label_Year.Text = CurrentTime.Year + " 年"; label_Month.Text = CurrentTime.Month + " 月"; this.label_Date.Text = CurrentTime.ToString("yyyy-MM-dd HH:mm:ss"); SetDay(CurrentTime); } private void YearPicRight_Click(object? sender, EventArgs e) { switch (TimeType) { case (int)EnumDate.日: //获取去年这个月多少天 int dayscount = DateTime.DaysInMonth(CurrentTime.AddYears(1).Year, CurrentTime.AddYears(1).Month); CurrentTime = CurrentTime.Day <= dayscount ? new DateTime(CurrentTime.AddYears(1).Year, CurrentTime.AddYears(1).Month, CurrentTime.Day, CurrentTime.Hour, CurrentTime.Minute, CurrentTime.Second) : new DateTime(CurrentTime.AddYears(1).Year, CurrentTime.AddYears(1).Month, dayscount, CurrentTime.Hour, CurrentTime.Minute, CurrentTime.Second); label_Year.Text = CurrentTime.Year + " 年"; label_Month.Text = CurrentTime.Month + " 月"; this.label_Date.Text = CurrentTime.ToString("yyyy-MM-dd HH:mm:ss"); SetDay(CurrentTime); break; case (int)EnumDate.月: YM_Label.Text = (int)YM_Label.Tag + 1 + " 年"; YM_Label.Tag = (int)YM_Label.Tag + 1; break; case (int)EnumDate.年: int year = (int)YM_Label.Tag + 8; for (int i = 0; i < panel_Bottom_Top.Controls.Count; i++) { panel_Bottom_Top.Controls[i].Text = year + "年"; panel_Bottom_Top.Controls[i].Tag = year; year++; } YM_Label.Text = ((int)YM_Label.Tag + 8) + " 年~" + ((int)YM_Label.Tag + 22) + " 年"; YM_Label.Tag = (int)YM_Label.Tag + 15; break; default: break; } } private void MouseLeaveClick(object? sender, EventArgs e) { var control = sender as Control; control.BackColor = Color.White; } private void MouseEnterClick(object? sender, EventArgs e) { var control = sender as Control; control.Cursor = Cursors.Hand; control.BackColor = Color.FromArgb(224, 224, 224); } private void YearPicLeft_Click(object? sender, EventArgs e) { switch (TimeType) { case (int)EnumDate.日: //获取去年这个月多少天 int dayscount = DateTime.DaysInMonth(CurrentTime.AddYears(-1).Year, CurrentTime.AddYears(-1).Month); CurrentTime = CurrentTime.Day <= dayscount ? new DateTime(CurrentTime.AddYears(-1).Year, CurrentTime.AddYears(-1).Month, CurrentTime.Day, CurrentTime.Hour, CurrentTime.Minute, CurrentTime.Second) : new DateTime(CurrentTime.AddYears(-1).Year, CurrentTime.AddYears(-1).Month, dayscount, CurrentTime.Hour, CurrentTime.Minute, CurrentTime.Second); label_Year.Text = CurrentTime.Year + " 年"; label_Month.Text = CurrentTime.Month + " 月"; this.label_Date.Text = CurrentTime.ToString("yyyy-MM-dd HH:mm:ss"); SetDay(CurrentTime); break; case (int)EnumDate.月: YM_Label.Text = (int)YM_Label.Tag - 1 + " 年"; YM_Label.Tag = (int)YM_Label.Tag - 1; break; case (int)EnumDate.年: int year = (int)YM_Label.Tag - 22; for (int i = 0; i < panel_Bottom_Top.Controls.Count; i++) { panel_Bottom_Top.Controls[i].Text = year + "年"; panel_Bottom_Top.Controls[i].Tag = year; year++; } YM_Label.Text = ((int)YM_Label.Tag - 22) + " 年~" + ((int)YM_Label.Tag - 8) + " 年"; YM_Label.Tag = (int)YM_Label.Tag - 15; break; default: break; } } public void SetDay(DateTime time) { this.panel_Bottom_Top_Bottom.Controls.Clear(); var list = GetDays(time); int count = 0; int rowcount = 1; foreach (var item in list) { count++; Label label = new Label(); label.Size = new Size(50, 50); label.Text = item.Item2.ToString(); if (item.Item1 == 0 || item.Item1 == -1) label.ForeColor = Color.Gray; if (item.Item1 == 1 && item.Item2 == CurrentTime.Day) { label.ForeColor = Color.White; label.BackColor = Color.FromArgb(0, 159, 149); CurrentControl = label; } label.TextAlign = ContentAlignment.MiddleCenter; label.Parent = this.panel_Bottom_Top_Bottom; label.Tag = item; label.Click += Day_Click; label.MouseEnter += Day_MouseEnterClick; label.MouseLeave += Day_MouseLeaveClick; label.AutoSize = false; label.Location = new Point((count - 1) * 50, rowcount);//位置 if (count % 7 == 0) { count = 0; rowcount += 50; } } } private void Day_MouseLeaveClick(object? sender, EventArgs e) { var label = sender as Label; var tuple = label.Tag as Tuple<int, int>; if (CurrentControl != null && CurrentControl != label) { label.BackColor = Color.White; if (tuple != null && (tuple.Item1 == 0 || tuple.Item1 == -1)) label.ForeColor = Color.Gray; } } private void Day_MouseEnterClick(object? sender, EventArgs e) { var label = sender as Label; label.Cursor = Cursors.Hand; if (CurrentControl != null && CurrentControl != label) { label.BackColor = Color.FromArgb(224, 224, 224); label.ForeColor = Color.Black; } } private void Day_Click(object? sender, EventArgs e) { var label = sender as Label; var tuple = label.Tag as Tuple<int, int>; if (tuple != null) { switch (tuple.Item1) { case 0: CurrentTime = new DateTime(CurrentTime.AddMonths(-1).Year, CurrentTime.AddMonths(-1).Month, tuple.Item2, CurrentTime.AddMonths(-1).Hour, CurrentTime.AddMonths(-1).Minute, CurrentTime.AddMonths(-1).Second); break; case 1: CurrentTime = new DateTime(CurrentTime.Year, CurrentTime.Month, tuple.Item2, CurrentTime.Hour, CurrentTime.Minute, CurrentTime.Second); break; case -1: CurrentTime = new DateTime(CurrentTime.AddMonths(1).Year, CurrentTime.AddMonths(1).Month, tuple.Item2, CurrentTime.AddMonths(1).Hour, CurrentTime.AddMonths(1).Minute, CurrentTime.AddMonths(1).Second); break; } label_Year.Text = CurrentTime.Year + " 年"; label_Month.Text = CurrentTime.Month + " 月"; this.label_Date.Text = CurrentTime.ToString("yyyy-MM-dd HH:mm:ss"); if (tuple.Item1 != 1) SetDay(CurrentTime); if (CurrentControl != label && tuple.Item1 == 1) { CurrentControl.BackColor = Color.White; CurrentControl.ForeColor = Color.Black; label.ForeColor = Color.White; label.BackColor = Color.FromArgb(0, 159, 149); CurrentControl = label; } } } public List<Tuple<int, int>> GetDays(DateTimeOffset date) { // 获取当月第一天的日期 var CurrentFirstDayOfMonth = new DateTimeOffset(date.Year, date.Month, 1, 0, 0, 0, date.Offset); // 获取当月最后一天的日期 var CurrentDayOfMonth = CurrentFirstDayOfMonth.AddMonths(1).AddDays(-1); // 获取当月第一天前面的空白格子 var CurrentFristDayOfMonth = ((int)CurrentFirstDayOfMonth.DayOfWeek + 7 - (int)DayOfWeek.Monday) % 7; // 获取当月最后一天后面的空白格子 var trailingSpaces = (int)DayOfWeek.Saturday + 1 - (int)CurrentDayOfMonth.DayOfWeek; //获取上个月的天数 var LastTime = date.AddMonths(-1); int LastDayOfMonth = DateTime.DaysInMonth(LastTime.Year, LastTime.Month); var daylist = new List<Tuple<int, int>>(); if (CurrentFristDayOfMonth == 7) for (int i = LastDayOfMonth - 7 + 1; i <= LastDayOfMonth; i++) daylist.Add(Tuple.Create(0, i)); else for (int i = LastDayOfMonth - CurrentFristDayOfMonth + 1; i <= LastDayOfMonth; i++) daylist.Add(Tuple.Create(0, i)); for (int i = 1; i <= CurrentDayOfMonth.Day; i++) daylist.Add(Tuple.Create(1, i)); var daycount = daylist.Count; ; for (int i = 1; i <= 42 - daycount; i++) daylist.Add(Tuple.Create(-1, i)); return daylist; } #endregion #region 下方的时间选择 private void InitTimeData() { this.panel_Top.Controls.Clear(); TimeType = (int)EnumDate.时; Label labelTime = new Label(); labelTime.Text = "选择时间"; labelTime.TextAlign = ContentAlignment.MiddleCenter; labelTime.Parent = this.panel_Top; labelTime.AutoSize = false; labelTime.Dock = DockStyle.Fill; this.panel_Bottom_Top_Top.Controls.Clear(); labelHour = new Label(); labelHour.Size = new Size(110, 50); labelHour.Text = "时"; labelHour.Tag = CurrentTime.Hour; labelHour.TextAlign = ContentAlignment.MiddleCenter; labelHour.Parent = this.panel_Bottom_Top_Top; labelHour.AutoSize = false; labelHour.Location = new Point(0, 0);//位置 labelMin = new Label(); labelMin.Size = new Size(110, 50); labelMin.Text = "分"; labelMin.Tag = CurrentTime.Minute; labelMin.TextAlign = ContentAlignment.MiddleCenter; labelMin.Parent = this.panel_Bottom_Top_Top; labelMin.AutoSize = false; labelMin.Location = new Point(110, 0);//位置 labelSec = new Label(); labelSec.Size = new Size(110, 50); labelSec.Text = "秒"; labelSec.Tag = CurrentTime.Second; labelSec.TextAlign = ContentAlignment.MiddleCenter; labelSec.Parent = this.panel_Bottom_Top_Top; labelSec.AutoSize = false; labelSec.Location = new Point(220, 0);//位置 this.panel_Bottom_Top_Bottom.Controls.Clear(); flowHour = new FlowLayoutPanel(); flowHour.Size = new Size(110, 300); flowHour.Parent = this.panel_Bottom_Top_Bottom; flowHour.Location = new Point(10, 0);//位置 flowHour.AutoScroll = true; ; for (int i = 0; i < 24; i++) { Label lHour = new Label(); lHour.Size = new Size(80, 50); if (i < 10) lHour.Text = "0" + i; else lHour.Text = i.ToString(); lHour.Tag = i; if (i == (int)labelHour.Tag) { lHour.ForeColor = Color.White; lHour.BackColor = Color.FromArgb(0, 159, 149); HourControl = lHour; } lHour.Click += lHour_Click; lHour.MouseEnter += lHour_MouseEnterClick; lHour.MouseLeave += lHour_MouseLeaveClick; lHour.TextAlign = ContentAlignment.MiddleCenter; lHour.AutoSize = false; lHour.Location = new Point(0, i * 50);//位置 // label.BackColor = Color.Gray; flowHour.Controls.Add(lHour); } flowMin = new FlowLayoutPanel(); flowMin.Size = new Size(110, 300); flowMin.Parent = this.panel_Bottom_Top_Bottom; flowMin.Location = new Point(120, 0);//位置 flowMin.AutoScroll = true; ; for (int i = 0; i < 60; i++) { Label lMin = new Label(); lMin.Size = new Size(80, 50); if (i < 10) lMin.Text = "0" + i; else lMin.Text = i.ToString(); lMin.Tag = i; if (i == (int)labelMin.Tag) { lMin.ForeColor = Color.White; lMin.BackColor = Color.FromArgb(0, 159, 149); MinControl = lMin; } lMin.Click += lMin_Click; lMin.MouseEnter += lMin_MouseEnterClick; lMin.MouseLeave += lMin_MouseLeaveClick; lMin.TextAlign = ContentAlignment.MiddleCenter; lMin.AutoSize = false; lMin.Location = new Point(0, i * 50);//位置 flowMin.Controls.Add(lMin); } flowSec = new FlowLayoutPanel(); flowSec.Size = new Size(110, 300); flowSec.Parent = this.panel_Bottom_Top_Bottom; flowSec.Location = new Point(230, 0);//位置 flowSec.AutoScroll = true; ; for (int i = 0; i < 60; i++) { Label lSec = new Label(); lSec.Size = new Size(80, 50); if (i < 10) lSec.Text = "0" + i; else lSec.Text = i.ToString(); lSec.Tag = i; if (i == (int)labelSec.Tag) { lSec.ForeColor = Color.White; lSec.BackColor = Color.FromArgb(0, 159, 149); SecControl = lSec; } lSec.Click += lSec_Click; lSec.MouseEnter += lSec_MouseEnterClick; lSec.MouseLeave += lSec_MouseLeaveClick; lSec.TextAlign = ContentAlignment.MiddleCenter; lSec.AutoSize = false; lSec.Location = new Point(0, i * 50);//位置 flowSec.Controls.Add(lSec); } SetSrollBar(); } private void lHour_MouseLeaveClick(object? sender, EventArgs e) { var label = sender as Label; if ((int)label.Tag != (int)labelHour.Tag) label.BackColor = Color.White; } private void lHour_MouseEnterClick(object? sender, EventArgs e) { var label = sender as Label; label.Cursor = Cursors.Hand; if ((int)label.Tag != (int)labelHour.Tag) label.BackColor = Color.FromArgb(224, 224, 224); } private void lHour_Click(object? sender, EventArgs e) { var label = sender as Label; if (label != HourControl) { HourControl.BackColor = Color.White; HourControl.ForeColor = Color.Black; label.BackColor = Color.FromArgb(0, 159, 149); label.ForeColor = Color.White; HourControl = label; } labelHour.Tag = (int)label.Tag; SetSrollBar(); } private void lMin_MouseLeaveClick(object? sender, EventArgs e) { var label = sender as Label; if ((int)label.Tag != (int)labelMin.Tag) label.BackColor = Color.White; } private void lMin_MouseEnterClick(object? sender, EventArgs e) { var label = sender as Label; label.Cursor = Cursors.Hand; if ((int)label.Tag != (int)labelMin.Tag) label.BackColor = Color.FromArgb(224, 224, 224); } private void lMin_Click(object? sender, EventArgs e) { var label = sender as Label; if (label != MinControl) { MinControl.BackColor = Color.White; MinControl.ForeColor = Color.Black; label.BackColor = Color.FromArgb(0, 159, 149); label.ForeColor = Color.White; MinControl = label; } labelMin.Tag = (int)label.Tag; SetSrollBar(); } private void SetSrollBar() { if (((int)labelHour.Tag - 2) * 50 > flowHour.VerticalScroll.Maximum) flowHour.VerticalScroll.Value = flowHour.VerticalScroll.Maximum; else if (((int)labelHour.Tag - 2) * 50 < flowHour.VerticalScroll.Minimum) flowHour.VerticalScroll.Value = flowHour.VerticalScroll.Minimum; else flowHour.VerticalScroll.Value = ((int)labelHour.Tag - 2) * 50; if (((int)labelMin.Tag - 2) * 50 > flowMin.VerticalScroll.Maximum) flowMin.VerticalScroll.Value = flowMin.VerticalScroll.Maximum; else if (((int)labelMin.Tag - 2) * 50 < flowMin.VerticalScroll.Minimum) flowMin.VerticalScroll.Value = flowMin.VerticalScroll.Minimum; else flowMin.VerticalScroll.Value = ((int)labelMin.Tag - 2) * 50; if (((int)labelSec.Tag - 2) * 50 > flowSec.VerticalScroll.Maximum) flowSec.VerticalScroll.Value = flowSec.VerticalScroll.Maximum; if (((int)labelSec.Tag - 2) * 50 < flowSec.VerticalScroll.Minimum) flowSec.VerticalScroll.Value = flowSec.VerticalScroll.Minimum; else flowSec.VerticalScroll.Value = ((int)labelSec.Tag - 2) * 50; } private void lSec_MouseLeaveClick(object? sender, EventArgs e) { var label = sender as Label; if ((int)label.Tag != (int)labelSec.Tag) label.BackColor = Color.White; } private void lSec_MouseEnterClick(object? sender, EventArgs e) { var label = sender as Label; label.Cursor = Cursors.Hand; if ((int)label.Tag != (int)labelSec.Tag) label.BackColor = Color.FromArgb(224, 224, 224); } private void lSec_Click(object? sender, EventArgs e) { var label = sender as Label; if (label != SecControl) { SecControl.BackColor = Color.White; SecControl.ForeColor = Color.Black; label.BackColor = Color.FromArgb(0, 159, 149); label.ForeColor = Color.White; SecControl = label; } labelSec.Tag = (int)label.Tag; SetSrollBar(); } private void label_Sure_Painit(object? sender, PaintEventArgs e) { Label label = sender as Label; ControlPaint.DrawBorder(e.Graphics,//获取进行绘制的图形 new Rectangle(0, 0, label.Width, label.Height),//绘制控件所在工作区域 Color.FromArgb(138, 238, 238), 1, ButtonBorderStyle.Solid,//边框类型,选择实线边框,也有虚线等种类;下面这四个重复的传参是设置工作区上下左右边框的功能 Color.FromArgb(138, 238, 238), 1, ButtonBorderStyle.Solid, Color.FromArgb(138, 238, 238), 1, ButtonBorderStyle.Solid, Color.FromArgb(138, 238, 238), 1, ButtonBorderStyle.Solid); } private void label_Sure_Click(object? sender, EventArgs e) { if (TimeType == (int)EnumDate.时) { CurrentTime = new DateTime(CurrentTime.Year, CurrentTime.Month, CurrentTime.Day, (int)labelHour.Tag, (int)labelMin.Tag, (int)labelSec.Tag); label_Date.Text = CurrentTime.ToString("yyyy-MM-dd HH:mm:ss"); TimeType = (int)EnumDate.日; } this.Input_Time.Text = label_Date.Text; popup.Close(); } private void label_SeletTime_Painit(object? sender, PaintEventArgs e) { Label label = sender as Label; ControlPaint.DrawBorder(e.Graphics,//获取进行绘制的图形 new Rectangle(0, 0, label.Width, label.Height),//绘制控件所在工作区域 Color.FromArgb(138, 238, 238), 1, ButtonBorderStyle.Solid,//边框类型,选择实线边框,也有虚线等种类;下面这四个重复的传参是设置工作区上下左右边框的功能 Color.FromArgb(138, 238, 238), 1, ButtonBorderStyle.Solid, Color.FromArgb(138, 238, 238), 1, ButtonBorderStyle.Solid, Color.FromArgb(138, 238, 238), 1, ButtonBorderStyle.Solid); } private void label_SeletTime_Click(object? sender, EventArgs e) { InitTimeData(); } #endregion }
然后运行看一下效果:
.Net Core
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在外漂泊的这几年总结和感悟,展望未来
· 博客园 & 1Panel 联合终身会员上线
· 支付宝事故这事儿,凭什么又是程序员背锅?有没有可能是这样的...
· https证书一键自动续期,帮你解放90天限制
· 在 ASP.NET Core WebAPI如何实现版本控制?