/// <summary> /// 时间 /// </summary> [DefaultProperty("TimeValue")] [DefaultEvent("TimeValueChanged")] [Description("时间")] public partial class TimeExt : Control { public delegate void EventHandler(object sender, TimeEventArgs e); #region private event EventHandler timeValueChanged; /// <summary> /// 时间值更改事件 /// </summary> [Description("时间值更改事件")] public event EventHandler TimeValueChanged { add { this.timeValueChanged += value; } remove { this.timeValueChanged -= value; } } private int lineWidth = 6; /// <summary> /// 线宽度 /// </summary> [DefaultValue(6)] [Description("线宽度")] public int LineWidth { get { return this.lineWidth; } set { if (this.lineWidth == value) return; this.lineWidth = value; this.Invalidate(); } } private Color lineHighlightColor = Color.FromArgb(56, 58, 61); /// <summary> /// 线高亮颜色 /// </summary> [DefaultValue(typeof(Color), "56, 58, 61")] [Description("线高亮颜色")] public Color LineHighlightColor { get { return this.lineHighlightColor; } set { if (this.lineHighlightColor == value) return; this.lineHighlightColor = value; this.Invalidate(); } } private bool shadowShow = true; /// <summary> /// 是否显示线阴影 /// </summary> [DefaultValue(true)] [Description("是否显示线阴影")] public bool ShadowShow { get { return this.shadowShow; } set { if (this.shadowShow == value) return; this.shadowShow = value; this.Invalidate(); } } private Color lineShadowColor = Color.LightGray; /// <summary> /// 线阴影颜色 /// </summary> [DefaultValue(typeof(Color), "LightGray")] [Description("线阴影颜色")] public Color LineShadowColor { get { return this.lineShadowColor; } set { if (this.lineShadowColor == value) return; this.lineShadowColor = value; this.Invalidate(); } } private DateTime timeValue; /// <summary> /// 时间 /// </summary> [Description("时间")] public DateTime TimeValue { get { return this.timeValue; } set { if (this.timeValue == value) return; this.timeValue = value; if (this.timeValueChanged != null) { this.timeValueChanged(this, new TimeEventArgs() { Value = value }); } this.Invalidate(); } } private TimeType timeTypeFormat = TimeType.HourMinuteSecond; /// <summary> /// 时间显示格式 /// </summary> [DefaultValue(TimeType.HourMinuteSecond)] [Description("时间显示格式")] public TimeType TimeTypeFormat { get { return this.timeTypeFormat; } set { if (this.timeTypeFormat == value) return; this.timeTypeFormat = value; this.Invalidate(); } } protected override Size DefaultSize { get { return new Size(280, 67); } } #endregion public TimeExt() { SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); SetStyle(ControlStyles.ResizeRedraw, true); SetStyle(ControlStyles.SupportsTransparentBackColor, true); InitializeComponent(); } #region // // --- 1 // | | 11 12 // --- 2 // | | 21 22 // --- 3 // private void draw_vertical_11(Graphics g, Pen pen, RectangleF bounds_rect, float w, float h, float v) { g.DrawLine(pen, bounds_rect.X + v, bounds_rect.Y + this.lineWidth, bounds_rect.X + v, bounds_rect.Y + h); } private void draw_vertical_21(Graphics g, Pen pen, RectangleF bounds_rect, float w, float h, float v) { g.DrawLine(pen, bounds_rect.X + v, bounds_rect.Y + this.lineWidth + h, bounds_rect.X + v, bounds_rect.Y + h + h); } private void draw_horizontal_1(Graphics g, Pen pen, RectangleF bounds_rect, float w, float h, float v) { g.DrawLine(pen, 1 + bounds_rect.X + this.lineWidth, v, 1 + bounds_rect.X + w, v); } private void draw_horizontal_2(Graphics g, Pen pen, RectangleF bounds_rect, float w, float h, float v) { g.DrawLine(pen, 1 + bounds_rect.X + this.lineWidth, v + h, 1 + bounds_rect.X + w, v + h); } private void draw_horizontal_3(Graphics g, Pen pen, RectangleF bounds_rect, float w, float h, float v) { g.DrawLine(pen, 1 + bounds_rect.X + this.lineWidth, v + h + h, 1 + bounds_rect.X + w, v + h + h); } private void draw_vertical_12(Graphics g, Pen pen, RectangleF bounds_rect, float w, float h, float v) { g.DrawLine(pen, bounds_rect.X + 1 + v + h, bounds_rect.Y + this.lineWidth, bounds_rect.X + 1 + v + h, bounds_rect.Y + h); } private void draw_vertical_22(Graphics g, Pen pen, RectangleF bounds_rect, float w, float h, float v) { g.DrawLine(pen, bounds_rect.X + 1 + v + h, bounds_rect.Y + this.lineWidth + h, bounds_rect.X + 1 + v + h, bounds_rect.Y + h + h); } #endregion #region private void draw_0(Graphics g, Pen highlight_pen, RectangleF bounds_rect, float w, float h, float c, bool shadow, Pen shadow_pen) { this.draw_vertical_11(g, highlight_pen, bounds_rect, w, h, c); this.draw_vertical_21(g, highlight_pen, bounds_rect, w, h, c); this.draw_horizontal_1(g, highlight_pen, bounds_rect, w, h, c); if (shadow) this.draw_horizontal_2(g, shadow_pen, bounds_rect, w, h, c); this.draw_horizontal_3(g, highlight_pen, bounds_rect, w, h, c); this.draw_vertical_12(g, highlight_pen, bounds_rect, w, h, c); this.draw_vertical_22(g, highlight_pen, bounds_rect, w, h, c); } private void draw_1(Graphics g, Pen highlight_pen, RectangleF bounds_rect, float w, float h, float c, bool shadow, Pen shadow_pen) { if (shadow) this.draw_vertical_11(g, shadow_pen, bounds_rect, w, h, c); if (shadow) this.draw_vertical_21(g, shadow_pen, bounds_rect, w, h, c); if (shadow) this.draw_horizontal_1(g, shadow_pen, bounds_rect, w, h, c); if (shadow) this.draw_horizontal_2(g, shadow_pen, bounds_rect, w, h, c); if (shadow) this.draw_horizontal_3(g, shadow_pen, bounds_rect, w, h, c); this.draw_vertical_12(g, highlight_pen, bounds_rect, w, h, c); this.draw_vertical_22(g, highlight_pen, bounds_rect, w, h, c); } private void draw_2(Graphics g, Pen highlight_pen, RectangleF bounds_rect, float w, float h, float c, bool shadow, Pen shadow_pen) { if (shadow) this.draw_vertical_11(g, shadow_pen, bounds_rect, w, h, c); this.draw_vertical_21(g, highlight_pen, bounds_rect, w, h, c); this.draw_horizontal_1(g, highlight_pen, bounds_rect, w, h, c); this.draw_horizontal_2(g, highlight_pen, bounds_rect, w, h, c); this.draw_horizontal_3(g, highlight_pen, bounds_rect, w, h, c); this.draw_vertical_12(g, highlight_pen, bounds_rect, w, h, c); if (shadow) this.draw_vertical_22(g, shadow_pen, bounds_rect, w, h, c); } private void draw_3(Graphics g, Pen highlight_pen, RectangleF bounds_rect, float w, float h, float c, bool shadow, Pen shadow_pen) { if (shadow) this.draw_vertical_11(g, shadow_pen, bounds_rect, w, h, c); if (shadow) this.draw_vertical_21(g, shadow_pen, bounds_rect, w, h, c); this.draw_horizontal_1(g, highlight_pen, bounds_rect, w, h, c); this.draw_horizontal_2(g, highlight_pen, bounds_rect, w, h, c); this.draw_horizontal_3(g, highlight_pen, bounds_rect, w, h, c); this.draw_vertical_12(g, highlight_pen, bounds_rect, w, h, c); this.draw_vertical_22(g, highlight_pen, bounds_rect, w, h, c); } private void draw_4(Graphics g, Pen highlight_pen, RectangleF bounds_rect, float w, float h, float c, bool shadow, Pen shadow_pen) { this.draw_vertical_11(g, highlight_pen, bounds_rect, w, h, c); if (shadow) this.draw_vertical_21(g, shadow_pen, bounds_rect, w, h, c); if (shadow) this.draw_horizontal_1(g, shadow_pen, bounds_rect, w, h, c); this.draw_horizontal_2(g, highlight_pen, bounds_rect, w, h, c); if (shadow) this.draw_horizontal_3(g, shadow_pen, bounds_rect, w, h, c); this.draw_vertical_12(g, highlight_pen, bounds_rect, w, h, c); this.draw_vertical_22(g, highlight_pen, bounds_rect, w, h, c); } private void draw_5(Graphics g, Pen highlight_pen, RectangleF bounds_rect, float w, float h, float c, bool shadow, Pen shadow_pen) { this.draw_vertical_11(g, highlight_pen, bounds_rect, w, h, c); if (shadow) this.draw_vertical_21(g, shadow_pen, bounds_rect, w, h, c); this.draw_horizontal_1(g, highlight_pen, bounds_rect, w, h, c); this.draw_horizontal_2(g, highlight_pen, bounds_rect, w, h, c); this.draw_horizontal_3(g, highlight_pen, bounds_rect, w, h, c); if (shadow) this.draw_vertical_12(g, shadow_pen, bounds_rect, w, h, c); this.draw_vertical_22(g, highlight_pen, bounds_rect, w, h, c); } private void draw_6(Graphics g, Pen highlight_pen, RectangleF bounds_rect, float w, float h, float c, bool shadow, Pen shadow_pen) { this.draw_vertical_11(g, highlight_pen, bounds_rect, w, h, c); this.draw_vertical_21(g, highlight_pen, bounds_rect, w, h, c); this.draw_horizontal_1(g, highlight_pen, bounds_rect, w, h, c); this.draw_horizontal_2(g, highlight_pen, bounds_rect, w, h, c); this.draw_horizontal_3(g, highlight_pen, bounds_rect, w, h, c); if (shadow) this.draw_vertical_12(g, shadow_pen, bounds_rect, w, h, c); this.draw_vertical_22(g, highlight_pen, bounds_rect, w, h, c); } private void draw_7(Graphics g, Pen highlight_pen, RectangleF bounds_rect, float w, float h, float c, bool shadow, Pen shadow_pen) { if (shadow) this.draw_vertical_11(g, shadow_pen, bounds_rect, w, h, c); if (shadow) this.draw_vertical_21(g, shadow_pen, bounds_rect, w, h, c); this.draw_horizontal_1(g, highlight_pen, bounds_rect, w, h, c); if (shadow) this.draw_horizontal_2(g, shadow_pen, bounds_rect, w, h, c); if (shadow) this.draw_horizontal_3(g, shadow_pen, bounds_rect, w, h, c); this.draw_vertical_12(g, highlight_pen, bounds_rect, w, h, c); this.draw_vertical_22(g, highlight_pen, bounds_rect, w, h, c); } private void draw_8(Graphics g, Pen highlight_pen, RectangleF bounds_rect, float w, float h, float c, bool shadow, Pen shadow_pen) { this.draw_vertical_11(g, highlight_pen, bounds_rect, w, h, c); this.draw_vertical_21(g, highlight_pen, bounds_rect, w, h, c); this.draw_horizontal_1(g, highlight_pen, bounds_rect, w, h, c); this.draw_horizontal_2(g, highlight_pen, bounds_rect, w, h, c); this.draw_horizontal_3(g, highlight_pen, bounds_rect, w, h, c); this.draw_vertical_12(g, highlight_pen, bounds_rect, w, h, c); this.draw_vertical_22(g, highlight_pen, bounds_rect, w, h, c); } private void draw_9(Graphics g, Pen highlight_pen, RectangleF bounds_rect, float w, float h, float c, bool shadow, Pen shadow_pen) { this.draw_vertical_11(g, highlight_pen, bounds_rect, w, h, c); if (shadow) this.draw_vertical_21(g, shadow_pen, bounds_rect, w, h, c); this.draw_horizontal_1(g, highlight_pen, bounds_rect, w, h, c); this.draw_horizontal_2(g, highlight_pen, bounds_rect, w, h, c); this.draw_horizontal_3(g, highlight_pen, bounds_rect, w, h, c); this.draw_vertical_12(g, highlight_pen, bounds_rect, w, h, c); this.draw_vertical_22(g, highlight_pen, bounds_rect, w, h, c); } private void draw_split(Graphics g, SolidBrush highlight_sb, RectangleF bounds_rect, float v) { float diameter = v * 2; RectangleF top_rect = new RectangleF(bounds_rect.X + (bounds_rect.Width - diameter) / 2, bounds_rect.Y + (bounds_rect.Height / 2 - diameter) / 2, diameter, diameter); RectangleF bottom_rect = new RectangleF(bounds_rect.X + (bounds_rect.Width - diameter) / 2, bounds_rect.Y + (bounds_rect.Height / 2 - diameter) / 2 + bounds_rect.Height / 2, diameter, diameter); g.FillEllipse(highlight_sb, top_rect); g.FillEllipse(highlight_sb, bottom_rect); } /// <summary> /// 绘制数字 /// </summary> /// <param name="g"></param> /// <param name="highlight_pen">数字高亮颜色</param> /// <param name="bounds_rect">数字的rect</param> /// <param name="w">数字笔画横向宽度</param> /// <param name="h">数字笔画纵向高度</param> /// <param name="c">笔帽的一半</param> /// <param name="shadow">是否显示数字阴影</param> /// <param name="shadow_pen">数字阴影颜色</param> /// <param name="num">数字</param> private void draw_num(Graphics g, Pen highlight_pen, RectangleF bounds_rect, float w, float h, float c, bool shadow, Pen shadow_pen, int num) { switch (num) { case 0: { this.draw_0(g, highlight_pen, bounds_rect, w, h, c, shadow, shadow_pen); break; } case 1: { this.draw_1(g, highlight_pen, bounds_rect, w, h, c, shadow, shadow_pen); break; } case 2: { this.draw_2(g, highlight_pen, bounds_rect, w, h, c, shadow, shadow_pen); break; } case 3: { this.draw_3(g, highlight_pen, bounds_rect, w, h, c, shadow, shadow_pen); break; } case 4: { this.draw_4(g, highlight_pen, bounds_rect, w, h, c, shadow, shadow_pen); break; } case 5: { this.draw_5(g, highlight_pen, bounds_rect, w, h, c, shadow, shadow_pen); break; } case 6: { this.draw_6(g, highlight_pen, bounds_rect, w, h, c, shadow, shadow_pen); break; } case 7: { this.draw_7(g, highlight_pen, bounds_rect, w, h, c, shadow, shadow_pen); break; } case 8: { this.draw_8(g, highlight_pen, bounds_rect, w, h, c, shadow, shadow_pen); break; } case 9: { this.draw_9(g, highlight_pen, bounds_rect, w, h, c, shadow, shadow_pen); break; } } } #endregion protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (this.timeValue == null) return; Graphics g = e.Graphics; SmoothingMode sm = g.SmoothingMode; g.SmoothingMode = SmoothingMode.AntiAlias; RectangleF bounds_rect = g.ClipBounds; float c = (float)this.lineWidth / 2; float w = (float)this.lineWidth * 2.5f + this.lineWidth; float h = w + 1; RectangleF hour1_rect = new RectangleF(0, 0, w + this.lineWidth, h * 2 + this.lineWidth); RectangleF hour2_rect = new RectangleF(hour1_rect.Right + this.lineWidth, 0, w + this.lineWidth, h * 2 + this.lineWidth); RectangleF split1_rect = new RectangleF(hour2_rect.Right + this.lineWidth, 0, this.lineWidth * 2, h * 2 + this.lineWidth); RectangleF minute1_rect = new RectangleF(split1_rect.Right + this.lineWidth, 0, w + this.lineWidth, h * 2 + this.lineWidth); RectangleF minute2_rect = new RectangleF(minute1_rect.Right + this.lineWidth, 0, w + this.lineWidth, h * 2 + this.lineWidth); RectangleF split2_rect = new RectangleF(minute2_rect.Right + this.lineWidth, 0, this.lineWidth * 2, h * 2 + this.lineWidth); RectangleF second1_rect = new RectangleF(split2_rect.Right + this.lineWidth, 0, w + this.lineWidth, h * 2 + this.lineWidth); RectangleF second2_rect = new RectangleF(second1_rect.Right + this.lineWidth, 0, w + this.lineWidth, h * 2 + this.lineWidth); RectangleF split3_rect = new RectangleF(second2_rect.Right + this.lineWidth, 0, this.lineWidth * 2, h * 2 + this.lineWidth); RectangleF millisecond1_rect = new RectangleF(split3_rect.Right + this.lineWidth, 0, w + this.lineWidth, h * 2 + this.lineWidth); RectangleF millisecond2_rect = new RectangleF(millisecond1_rect.Right + this.lineWidth, 0, w + this.lineWidth, h * 2 + this.lineWidth); RectangleF millisecond3_rect = new RectangleF(millisecond2_rect.Right + this.lineWidth, 0, w + this.lineWidth, h * 2 + this.lineWidth); Pen highlight_pen = new Pen(this.lineHighlightColor, this.lineWidth); Pen shadow_pen = new Pen(this.lineShadowColor, this.lineWidth); SolidBrush split_sb = new SolidBrush(this.lineHighlightColor); highlight_pen.StartCap = LineCap.Triangle; highlight_pen.EndCap = LineCap.Triangle; shadow_pen.StartCap = LineCap.Triangle; shadow_pen.EndCap = LineCap.Triangle; if (this.timeTypeFormat == TimeType.Hour) { this.draw_num(g, highlight_pen, hour1_rect, w, h, c, this.shadowShow, shadow_pen, int.Parse(this.timeValue.ToString("HH").Substring(0, 1))); this.draw_num(g, highlight_pen, hour2_rect, w, h, c, this.shadowShow, shadow_pen, int.Parse(this.timeValue.ToString("HH").Substring(1, 1))); } else if (this.timeTypeFormat == TimeType.HourMinute) { this.draw_num(g, highlight_pen, hour1_rect, w, h, c, this.shadowShow, shadow_pen, int.Parse(this.timeValue.ToString("HH").Substring(0, 1))); this.draw_num(g, highlight_pen, hour2_rect, w, h, c, this.shadowShow, shadow_pen, int.Parse(this.timeValue.ToString("HH").Substring(1, 1))); this.draw_split(g, split_sb, split1_rect, c); this.draw_num(g, highlight_pen, minute1_rect, w, h, c, this.shadowShow, shadow_pen, int.Parse(this.timeValue.ToString("mm").Substring(0, 1))); this.draw_num(g, highlight_pen, minute2_rect, w, h, c, this.shadowShow, shadow_pen, int.Parse(this.timeValue.ToString("mm").Substring(1, 1))); } else if (this.timeTypeFormat == TimeType.HourMinuteSecond) { this.draw_num(g, highlight_pen, hour1_rect, w, h, c, this.shadowShow, shadow_pen, int.Parse(this.timeValue.ToString("HH").Substring(0, 1))); this.draw_num(g, highlight_pen, hour2_rect, w, h, c, this.shadowShow, shadow_pen, int.Parse(this.timeValue.ToString("HH").Substring(1, 1))); this.draw_split(g, split_sb, split1_rect, c); this.draw_num(g, highlight_pen, minute1_rect, w, h, c, this.shadowShow, shadow_pen, int.Parse(this.timeValue.ToString("mm").Substring(0, 1))); this.draw_num(g, highlight_pen, minute2_rect, w, h, c, this.shadowShow, shadow_pen, int.Parse(this.timeValue.ToString("mm").Substring(1, 1))); this.draw_split(g, split_sb, split2_rect, c); this.draw_num(g, highlight_pen, second1_rect, w, h, c, this.shadowShow, shadow_pen, int.Parse(this.timeValue.ToString("ss").Substring(0, 1))); this.draw_num(g, highlight_pen, second2_rect, w, h, c, this.shadowShow, shadow_pen, int.Parse(this.timeValue.ToString("ss").Substring(1, 1))); } else if (this.timeTypeFormat == TimeType.HourMinuteSecondMillisecond) { this.draw_num(g, highlight_pen, hour1_rect, w, h, c, this.shadowShow, shadow_pen, int.Parse(this.timeValue.ToString("HH").Substring(0, 1))); this.draw_num(g, highlight_pen, hour2_rect, w, h, c, this.shadowShow, shadow_pen, int.Parse(this.timeValue.ToString("HH").Substring(1, 1))); this.draw_split(g, split_sb, split1_rect, c); this.draw_num(g, highlight_pen, minute1_rect, w, h, c, this.shadowShow, shadow_pen, int.Parse(this.timeValue.ToString("mm").Substring(0, 1))); this.draw_num(g, highlight_pen, minute2_rect, w, h, c, this.shadowShow, shadow_pen, int.Parse(this.timeValue.ToString("mm").Substring(1, 1))); this.draw_split(g, split_sb, split2_rect, c); this.draw_num(g, highlight_pen, second1_rect, w, h, c, this.shadowShow, shadow_pen, int.Parse(this.timeValue.ToString("ss").Substring(0, 1))); this.draw_num(g, highlight_pen, second2_rect, w, h, c, this.shadowShow, shadow_pen, int.Parse(this.timeValue.ToString("ss").Substring(1, 1))); this.draw_split(g, split_sb, split3_rect, c); this.draw_num(g, highlight_pen, millisecond1_rect, w, h, c, this.shadowShow, shadow_pen, int.Parse(this.timeValue.ToString("fff").Substring(0, 1))); this.draw_num(g, highlight_pen, millisecond2_rect, w, h, c, this.shadowShow, shadow_pen, int.Parse(this.timeValue.ToString("fff").Substring(1, 1))); this.draw_num(g, highlight_pen, millisecond3_rect, w, h, c, this.shadowShow, shadow_pen, int.Parse(this.timeValue.ToString("fff").Substring(2, 1))); } split_sb.Dispose(); shadow_pen.Dispose(); highlight_pen.Dispose(); g.SmoothingMode = sm; } protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } /// <summary> /// 时间显示格式 /// </summary> [Description("时间显示格式")] public enum TimeType { /// <summary> /// 时 /// </summary> Hour, /// <summary> /// 时分 /// </summary> HourMinute, /// <summary> /// 时分秒 /// </summary> HourMinuteSecond, /// <summary> /// 时分秒毫秒 /// </summary> HourMinuteSecondMillisecond, } /// <summary> /// 时间计事件参数 /// </summary> public class TimeEventArgs : EventArgs { /// <summary> /// 时间值 /// </summary> [Description("时间值")] public DateTime Value { get; set; } } }
原文:https://www.cnblogs.com/tlmbem/p/11299008.html