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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Globalization; using System.Windows.Forms; namespace KUN.CONTROL.LIB.UC { public partial class KFillTime : UserControl { #region 属性定义 [Browsable( true )] [Description( "字体" ), Category( "自定义属性" )] private Font timeFont = new Font( "微软雅黑" , 10, FontStyle.Bold); public Font TimeFont { get { return this .timeFont; } set { this .timeFont = value; } } [Browsable( true )] [Description( "时间字体颜色" ), Category( "自定义属性" )] /// <summary> /// 外部申明画笔和画刷工具 /// </summary> private Color fontColor = Color.LightCoral; public Color FontColor { get { return this .fontColor; } set { this .fontColor = value; } } [Browsable( true )] [Description( "占用时间填充颜色" ), Category( "自定义属性" )] private Color fillColor = Color.OldLace; public Color FillColor { get { return this .fillColor; } set { this .fillColor = value; } } [Browsable( true )] [Description( "开始小时数" ), Category( "自定义属性" )] private int beginHour = 8; public int BeginHour { get { return beginHour; } set { beginHour = value; } } [Browsable( true )] [Description( "显示几个小时" ), Category( "自定义属性" )] private int hourCount = 10; public int HourCount { get { return hourCount; } set { hourCount = value; } } /// <summary> /// 时间段字典表 /// </summary> public Dictionary<DateTime, DateTime> TimeParts = new Dictionary<DateTime, DateTime>(); /// <summary> /// 时间格式 /// </summary> DateTimeFormatInfo DateFormat = new DateTimeFormatInfo(); /// <summary> /// 边距 /// </summary> private int PaddingLeft = 50; //左边距 private int PaddingRight = 10; //右边距 private int PaddingButtom = 25; // 下边距 #endregion public KFillTime() { InitializeComponent(); DateFormat.ShortTimePattern = "HH:mm:ss" ; } private void KFillTime_Paint( object sender, PaintEventArgs e) { InitXYData(beginHour + "" , hourCount); } private void InitXYData( string beginTime, int hourCount) { Graphics gra = this .CreateGraphics(); Pen p = new Pen(Color.SeaGreen, 1); Pen p1 = new Pen(Color.LightSeaGreen, 1); float onecolwidth = ( this .Width - PaddingRight - PaddingLeft) / 60; //每分钟宽度 float hourwidth = ( this .Height - 2 * PaddingButtom) / hourCount; Brush fontBursh = new SolidBrush(fontColor); //纵坐标 for ( int i = 0; i <= 60; i++) { if (i % 5 == 0) //分钟 { gra.DrawString(i + "" , timeFont, fontBursh, onecolwidth * i + PaddingLeft, this .Height - PaddingButtom + 5); gra.DrawLine(p, PaddingLeft + i * onecolwidth, this .Height - PaddingButtom - 5, PaddingLeft + i * onecolwidth, this .Height - PaddingButtom + 5); gra.DrawLine(p, PaddingLeft + i * onecolwidth, PaddingButtom, PaddingLeft + i * onecolwidth, this .Height - PaddingButtom); } else gra.DrawLine(p1, PaddingLeft + i * onecolwidth, PaddingButtom, PaddingLeft + i * onecolwidth, this .Height - PaddingButtom); } gra.DrawString( " 分钟" , timeFont, fontBursh, onecolwidth * 60 + PaddingLeft + 12, this .Height - PaddingButtom + 5); gra.DrawLine(p, PaddingLeft, this .Height - PaddingButtom, this .Width - PaddingLeft, this .Height - PaddingButtom); // 画横坐标标尺 for ( int j = 0; j < hourCount + 1; j++) { gra.DrawLine(p1, PaddingLeft, this .Height - PaddingButtom - j * hourwidth, onecolwidth * 60 + PaddingLeft, this .Height - PaddingButtom - j * hourwidth); if (j < hourCount) gra.DrawString(Convert.ToInt32(beginTime) + j + ":00" , timeFont, fontBursh, 0, this .Height - PaddingButtom - j * hourwidth - hourwidth / 2); } gra.DrawString( "小时" , timeFont, fontBursh, 0, this .Height - PaddingButtom - hourCount * hourwidth); //画横坐标线 gra.DrawLine(p, PaddingLeft, PaddingButtom, PaddingLeft, this .Height - PaddingButtom); } public void FillUsedTimes() { this .Refresh(); if (TimeParts == null || TimeParts.Count == 0) return ; foreach (KeyValuePair<DateTime, DateTime> temppair in TimeParts) { FillTimeColor(temppair.Key, temppair.Value); } } public void FillUsedTimes(Dictionary<DateTime, DateTime> usedTimeDict) { this .Refresh(); foreach (KeyValuePair<DateTime, DateTime> temppair in usedTimeDict) { FillTimeColor(temppair.Key, temppair.Value); } } private void FillTimeColor(DateTime beginTime, DateTime endTime) { try { float oneminlwidth = ( this .Width - PaddingRight - PaddingLeft) / 60; //每分钟宽度 float onehourwidth = ( this .Height - 2 * PaddingButtom) / this .hourCount; //没小时宽度 Pen pen = new Pen(fillColor, 1); // 已占用时间段画笔 Brush brush = new SolidBrush(fillColor); Graphics gra = this .CreateGraphics(); //在同一小时内 if (beginTime.Hour == endTime.Hour) { float startX = beginTime.Minute * oneminlwidth + PaddingLeft; float startY = this .Height - PaddingButtom - (beginTime.Hour - Convert.ToInt32(beginHour) + 1) * onehourwidth; RectangleF rect = new RectangleF(startX, startY, oneminlwidth * (endTime.Minute - beginTime.Minute), onehourwidth); gra.DrawRectangle(pen, startX, startY, oneminlwidth * (endTime.Minute - beginTime.Minute), onehourwidth); gra.FillRectangle(brush, rect); } else { float startX1 = beginTime.Minute * oneminlwidth + PaddingLeft; float startY1 = this .Height - PaddingButtom - (beginTime.Hour - Convert.ToInt32(beginHour) + 1) * onehourwidth; RectangleF rect1 = new RectangleF(startX1, startY1, oneminlwidth * (60 - beginTime.Minute), onehourwidth); gra.DrawRectangle(pen, startX1, startY1, oneminlwidth * (60 - beginTime.Minute), onehourwidth); gra.FillRectangle(brush, rect1); float startY2 = this .Height - PaddingButtom - (endTime.Hour - Convert.ToInt32(beginHour) + 1) * onehourwidth; RectangleF rect2 = new RectangleF(PaddingLeft, startY2, oneminlwidth * endTime.Minute, onehourwidth); gra.DrawRectangle(pen, PaddingLeft, startY2, oneminlwidth * endTime.Minute, onehourwidth); gra.FillRectangle(brush, rect2); } } catch (Exception) { } } } } |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | using System; using System.Collections.Generic; using System.Globalization; using System.Windows.Forms; namespace KUN.CONTROL { public partial class KFillTimeForm : Form { public KFillTimeForm() { InitializeComponent(); } private void button1_Click( object sender, EventArgs e) { NewMethod(); } private void NewMethod() { try { Dictionary<DateTime, DateTime> dic = new Dictionary<DateTime, DateTime>(); dic.Add(Convert.ToDateTime( "8:00" ), Convert.ToDateTime( "8:10" )); dic.Add(Convert.ToDateTime( "8:50" ), Convert.ToDateTime( "9:40" )); dic.Add(Convert.ToDateTime( "13:12" ), Convert.ToDateTime( "13:34" )); this .kFillTime1.TimeParts = dic; this .kFillTime1.FillUsedTimes(); } catch { return ; } } private void button2_Click( object sender, EventArgs e) { try { Dictionary<DateTime, DateTime> dic = new Dictionary<DateTime, DateTime>(); dic.Add(Convert.ToDateTime( "9:00" ), Convert.ToDateTime( "9:10" )); dic.Add(Convert.ToDateTime( "10:15" ), Convert.ToDateTime( "10:40" )); this .kFillTime1.TimeParts = dic; this .kFillTime1.FillUsedTimes(); } catch { return ; } } private void KFillTimeForm_Activated( object sender, EventArgs e) { // this.button1.PerformClick(); } } } |
运行效果:
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本