前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492
麻烦博客下方点个【推荐】,谢谢
NuGet
Install-Package HZH_Controls
目录
https://www.cnblogs.com/bfyx/p/11364884.html
用处及效果
准备工作
使用GID+画的,不了解的话请自行百度
开始
添加一个类UCLEDNum ,继承UserControl
将数字拆分为单独的字,然后根据显示位置进行画出来,将显示位置定义为如下所示
/* 显示位置序号 * ****1*** * * * * 6 2 * * * * ****7*** * * * * 5 3 * * * * ****4*** */
从上面可以看出,定义了1-7的位置,然后定义一个数字对应的显示列表
1 private static Dictionary<char, int[]> m_nums = new Dictionary<char, int[]>(); 2 static UCLEDNum() 3 { 4 m_nums['0'] = new int[] { 1, 2, 3, 4, 5, 6 }; 5 m_nums['1'] = new int[] { 2, 3 }; 6 m_nums['2'] = new int[] { 1, 2, 5, 4, 7 }; 7 m_nums['3'] = new int[] { 1, 2, 7, 3, 4 }; 8 m_nums['4'] = new int[] { 2, 3, 6, 7 }; 9 m_nums['5'] = new int[] { 1, 6, 7, 3, 4 }; 10 m_nums['6'] = new int[] { 1, 6, 5, 4, 3, 7 }; 11 m_nums['7'] = new int[] { 1, 2, 3 }; 12 m_nums['8'] = new int[] { 1, 2, 3, 4, 5, 6, 7 }; 13 m_nums['9'] = new int[] { 1, 2, 3, 4, 7, 6 }; 14 m_nums['-'] = new int[] { 7 }; 15 m_nums[':'] = new int[0]; 16 m_nums['.'] = new int[0]; 17 }
你看到了还有“-”,“:”,“.”这3个符号,是为了时间和数字时候使用
然后定义一个矩形区域来用作绘画区域,并且在SizeChanged事件中赋值
Rectangle m_drawRect = Rectangle.Empty; void LEDNum_SizeChanged(object sender, EventArgs e) { m_drawRect = new Rectangle(1, 1, this.Width - 2, this.Height - 2); }
然后就是几个属性
1 private char m_value = '0'; 2 3 [Description("值"), Category("自定义")] 4 public char Value 5 { 6 get { return m_value; } 7 set 8 { 9 if (!m_nums.ContainsKey(value)) 10 { 11 return; 12 } 13 if (m_value != value) 14 { 15 m_value = value; 16 Refresh(); 17 } 18 } 19 } 20 21 private int m_lineWidth = 8; 22 23 [Description("线宽度,为了更好的显示效果,请使用偶数"), Category("自定义")] 24 public int LineWidth 25 { 26 get { return m_lineWidth; } 27 set 28 { 29 m_lineWidth = value; 30 Refresh(); 31 } 32 } 33 34 [Description("颜色"), Category("自定义")] 35 public override System.Drawing.Color ForeColor 36 { 37 get 38 { 39 return base.ForeColor; 40 } 41 set 42 { 43 base.ForeColor = value; 44 } 45 }
最重要的重绘
1 protected override void OnPaint(PaintEventArgs e) 2 { 3 base.OnPaint(e); 4 e.Graphics.SetGDIHigh(); 5 if (m_value == '.') 6 { 7 Rectangle r2 = new Rectangle(m_drawRect.Left + (m_drawRect.Width - m_lineWidth) / 2, m_drawRect.Bottom - m_lineWidth * 2, m_lineWidth, m_lineWidth); 8 e.Graphics.FillRectangle(new SolidBrush(ForeColor), r2); 9 } 10 else if (m_value == ':') 11 { 12 Rectangle r1 = new Rectangle(m_drawRect.Left + (m_drawRect.Width - m_lineWidth) / 2, m_drawRect.Top + (m_drawRect.Height / 2 - m_lineWidth) / 2, m_lineWidth, m_lineWidth); 13 e.Graphics.FillRectangle(new SolidBrush(ForeColor), r1); 14 Rectangle r2 = new Rectangle(m_drawRect.Left + (m_drawRect.Width - m_lineWidth) / 2, m_drawRect.Top + (m_drawRect.Height / 2 - m_lineWidth) / 2 + m_drawRect.Height / 2, m_lineWidth, m_lineWidth); 15 e.Graphics.FillRectangle(new SolidBrush(ForeColor), r2); 16 } 17 else 18 { 19 int[] vs = m_nums[m_value]; 20 if (vs.Contains(1)) 21 { 22 GraphicsPath path = new GraphicsPath(); 23 path.AddLines(new Point[] 24 { 25 new Point(m_drawRect.Left + 2, m_drawRect.Top), 26 new Point(m_drawRect.Right - 2, m_drawRect.Top), 27 new Point(m_drawRect.Right - m_lineWidth-2, m_drawRect.Top+m_lineWidth), 28 new Point(m_drawRect.Left + m_lineWidth+2, m_drawRect.Top+m_lineWidth), 29 new Point(m_drawRect.Left + 2, m_drawRect.Top) 30 }); 31 path.CloseAllFigures(); 32 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 33 } 34 35 if (vs.Contains(2)) 36 { 37 GraphicsPath path = new GraphicsPath(); 38 path.AddLines(new Point[] 39 { 40 new Point(m_drawRect.Right, m_drawRect.Top), 41 new Point(m_drawRect.Right, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2), 42 new Point(m_drawRect.Right-m_lineWidth/2, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2+m_lineWidth/2), 43 new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2), 44 new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Top+m_lineWidth), 45 new Point(m_drawRect.Right, m_drawRect.Top) 46 }); 47 path.CloseAllFigures(); 48 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 49 } 50 51 if (vs.Contains(3)) 52 { 53 GraphicsPath path = new GraphicsPath(); 54 path.AddLines(new Point[] 55 { 56 new Point(m_drawRect.Right, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2), 57 new Point(m_drawRect.Right, m_drawRect.Bottom), 58 new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Bottom-m_lineWidth), 59 new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2), 60 new Point(m_drawRect.Right-m_lineWidth/2, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2-m_lineWidth/2), 61 new Point(m_drawRect.Right, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2), 62 }); 63 path.CloseAllFigures(); 64 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 65 } 66 67 if (vs.Contains(4)) 68 { 69 GraphicsPath path = new GraphicsPath(); 70 path.AddLines(new Point[] 71 { 72 new Point(m_drawRect.Left + 2, m_drawRect.Bottom), 73 new Point(m_drawRect.Right - 2, m_drawRect.Bottom), 74 new Point(m_drawRect.Right - m_lineWidth-2, m_drawRect.Bottom-m_lineWidth), 75 new Point(m_drawRect.Left + m_lineWidth+2, m_drawRect.Bottom-m_lineWidth), 76 new Point(m_drawRect.Left + 2, m_drawRect.Bottom) 77 }); 78 path.CloseAllFigures(); 79 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 80 } 81 82 if (vs.Contains(5)) 83 { 84 GraphicsPath path = new GraphicsPath(); 85 path.AddLines(new Point[] 86 { 87 new Point(m_drawRect.Left, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2), 88 new Point(m_drawRect.Left, m_drawRect.Bottom), 89 new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Bottom-m_lineWidth), 90 new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2), 91 new Point(m_drawRect.Left+m_lineWidth/2, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2-m_lineWidth/2), 92 new Point(m_drawRect.Left, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2), 93 }); 94 path.CloseAllFigures(); 95 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 96 } 97 98 99 if (vs.Contains(6)) 100 { 101 GraphicsPath path = new GraphicsPath(); 102 path.AddLines(new Point[] 103 { 104 new Point(m_drawRect.Left, m_drawRect.Top), 105 new Point(m_drawRect.Left, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2), 106 new Point(m_drawRect.Left+m_lineWidth/2, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2+m_lineWidth/2), 107 new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2), 108 new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Top+m_lineWidth), 109 new Point(m_drawRect.Left, m_drawRect.Top) 110 }); 111 path.CloseAllFigures(); 112 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 113 } 114 115 if (vs.Contains(7)) 116 { 117 GraphicsPath path = new GraphicsPath(); 118 path.AddLines(new Point[] 119 { 120 new Point(m_drawRect.Left+m_lineWidth/2, m_drawRect.Height/2+1), 121 new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Height/2-m_lineWidth/2+1), 122 new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Height/2-m_lineWidth/2+1), 123 new Point(m_drawRect.Right-m_lineWidth/2, m_drawRect.Height/2+1), 124 new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Height/2+m_lineWidth/2+1), 125 new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Height/2+m_lineWidth/2+1), 126 new Point(m_drawRect.Left+m_lineWidth/2, m_drawRect.Height/2+1) 127 }); 128 path.CloseAllFigures(); 129 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 130 } 131 } 132 }
完工,看下完整代码和效果
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows.Forms; 6 using System.Drawing; 7 using System.Drawing.Drawing2D; 8 using System.ComponentModel; 9 10 namespace HZH_Controls.Controls 11 { 12 /* 显示位置序号 13 * ****1*** 14 * * * 15 * 6 2 16 * * * 17 * ****7*** 18 * * * 19 * 5 3 20 * * * 21 * ****4*** 22 */ 23 public class UCLEDNum : UserControl 24 { 25 Rectangle m_drawRect = Rectangle.Empty; 26 27 private static Dictionary<char, int[]> m_nums = new Dictionary<char, int[]>(); 28 static UCLEDNum() 29 { 30 m_nums['0'] = new int[] { 1, 2, 3, 4, 5, 6 }; 31 m_nums['1'] = new int[] { 2, 3 }; 32 m_nums['2'] = new int[] { 1, 2, 5, 4, 7 }; 33 m_nums['3'] = new int[] { 1, 2, 7, 3, 4 }; 34 m_nums['4'] = new int[] { 2, 3, 6, 7 }; 35 m_nums['5'] = new int[] { 1, 6, 7, 3, 4 }; 36 m_nums['6'] = new int[] { 1, 6, 5, 4, 3, 7 }; 37 m_nums['7'] = new int[] { 1, 2, 3 }; 38 m_nums['8'] = new int[] { 1, 2, 3, 4, 5, 6, 7 }; 39 m_nums['9'] = new int[] { 1, 2, 3, 4, 7, 6 }; 40 m_nums['-'] = new int[] { 7 }; 41 m_nums[':'] = new int[0]; 42 m_nums['.'] = new int[0]; 43 } 44 45 46 public UCLEDNum() 47 { 48 SizeChanged += LEDNum_SizeChanged; 49 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; 50 Size = new System.Drawing.Size(40, 70); 51 if (m_drawRect == Rectangle.Empty) 52 m_drawRect = new Rectangle(1, 1, this.Width - 2, this.Height - 2); 53 } 54 55 void LEDNum_SizeChanged(object sender, EventArgs e) 56 { 57 m_drawRect = new Rectangle(1, 1, this.Width - 2, this.Height - 2); 58 } 59 60 private char m_value = '0'; 61 62 [Description("值"), Category("自定义")] 63 public char Value 64 { 65 get { return m_value; } 66 set 67 { 68 if (!m_nums.ContainsKey(value)) 69 { 70 return; 71 } 72 if (m_value != value) 73 { 74 m_value = value; 75 Refresh(); 76 } 77 } 78 } 79 80 private int m_lineWidth = 8; 81 82 [Description("线宽度,为了更好的显示效果,请使用偶数"), Category("自定义")] 83 public int LineWidth 84 { 85 get { return m_lineWidth; } 86 set 87 { 88 m_lineWidth = value; 89 Refresh(); 90 } 91 } 92 93 [Description("颜色"), Category("自定义")] 94 public override System.Drawing.Color ForeColor 95 { 96 get 97 { 98 return base.ForeColor; 99 } 100 set 101 { 102 base.ForeColor = value; 103 } 104 } 105 106 protected override void OnPaint(PaintEventArgs e) 107 { 108 base.OnPaint(e); 109 e.Graphics.SetGDIHigh(); 110 if (m_value == '.') 111 { 112 Rectangle r2 = new Rectangle(m_drawRect.Left + (m_drawRect.Width - m_lineWidth) / 2, m_drawRect.Bottom - m_lineWidth * 2, m_lineWidth, m_lineWidth); 113 e.Graphics.FillRectangle(new SolidBrush(ForeColor), r2); 114 } 115 else if (m_value == ':') 116 { 117 Rectangle r1 = new Rectangle(m_drawRect.Left + (m_drawRect.Width - m_lineWidth) / 2, m_drawRect.Top + (m_drawRect.Height / 2 - m_lineWidth) / 2, m_lineWidth, m_lineWidth); 118 e.Graphics.FillRectangle(new SolidBrush(ForeColor), r1); 119 Rectangle r2 = new Rectangle(m_drawRect.Left + (m_drawRect.Width - m_lineWidth) / 2, m_drawRect.Top + (m_drawRect.Height / 2 - m_lineWidth) / 2 + m_drawRect.Height / 2, m_lineWidth, m_lineWidth); 120 e.Graphics.FillRectangle(new SolidBrush(ForeColor), r2); 121 } 122 else 123 { 124 int[] vs = m_nums[m_value]; 125 if (vs.Contains(1)) 126 { 127 GraphicsPath path = new GraphicsPath(); 128 path.AddLines(new Point[] 129 { 130 new Point(m_drawRect.Left + 2, m_drawRect.Top), 131 new Point(m_drawRect.Right - 2, m_drawRect.Top), 132 new Point(m_drawRect.Right - m_lineWidth-2, m_drawRect.Top+m_lineWidth), 133 new Point(m_drawRect.Left + m_lineWidth+2, m_drawRect.Top+m_lineWidth), 134 new Point(m_drawRect.Left + 2, m_drawRect.Top) 135 }); 136 path.CloseAllFigures(); 137 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 138 } 139 140 if (vs.Contains(2)) 141 { 142 GraphicsPath path = new GraphicsPath(); 143 path.AddLines(new Point[] 144 { 145 new Point(m_drawRect.Right, m_drawRect.Top), 146 new Point(m_drawRect.Right, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2), 147 new Point(m_drawRect.Right-m_lineWidth/2, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2+m_lineWidth/2), 148 new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2), 149 new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Top+m_lineWidth), 150 new Point(m_drawRect.Right, m_drawRect.Top) 151 }); 152 path.CloseAllFigures(); 153 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 154 } 155 156 if (vs.Contains(3)) 157 { 158 GraphicsPath path = new GraphicsPath(); 159 path.AddLines(new Point[] 160 { 161 new Point(m_drawRect.Right, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2), 162 new Point(m_drawRect.Right, m_drawRect.Bottom), 163 new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Bottom-m_lineWidth), 164 new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2), 165 new Point(m_drawRect.Right-m_lineWidth/2, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2-m_lineWidth/2), 166 new Point(m_drawRect.Right, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2), 167 }); 168 path.CloseAllFigures(); 169 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 170 } 171 172 if (vs.Contains(4)) 173 { 174 GraphicsPath path = new GraphicsPath(); 175 path.AddLines(new Point[] 176 { 177 new Point(m_drawRect.Left + 2, m_drawRect.Bottom), 178 new Point(m_drawRect.Right - 2, m_drawRect.Bottom), 179 new Point(m_drawRect.Right - m_lineWidth-2, m_drawRect.Bottom-m_lineWidth), 180 new Point(m_drawRect.Left + m_lineWidth+2, m_drawRect.Bottom-m_lineWidth), 181 new Point(m_drawRect.Left + 2, m_drawRect.Bottom) 182 }); 183 path.CloseAllFigures(); 184 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 185 } 186 187 if (vs.Contains(5)) 188 { 189 GraphicsPath path = new GraphicsPath(); 190 path.AddLines(new Point[] 191 { 192 new Point(m_drawRect.Left, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2), 193 new Point(m_drawRect.Left, m_drawRect.Bottom), 194 new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Bottom-m_lineWidth), 195 new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2), 196 new Point(m_drawRect.Left+m_lineWidth/2, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2-m_lineWidth/2), 197 new Point(m_drawRect.Left, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2), 198 }); 199 path.CloseAllFigures(); 200 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 201 } 202 203 204 if (vs.Contains(6)) 205 { 206 GraphicsPath path = new GraphicsPath(); 207 path.AddLines(new Point[] 208 { 209 new Point(m_drawRect.Left, m_drawRect.Top), 210 new Point(m_drawRect.Left, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2), 211 new Point(m_drawRect.Left+m_lineWidth/2, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2+m_lineWidth/2), 212 new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2), 213 new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Top+m_lineWidth), 214 new Point(m_drawRect.Left, m_drawRect.Top) 215 }); 216 path.CloseAllFigures(); 217 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 218 } 219 220 if (vs.Contains(7)) 221 { 222 GraphicsPath path = new GraphicsPath(); 223 path.AddLines(new Point[] 224 { 225 new Point(m_drawRect.Left+m_lineWidth/2, m_drawRect.Height/2+1), 226 new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Height/2-m_lineWidth/2+1), 227 new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Height/2-m_lineWidth/2+1), 228 new Point(m_drawRect.Right-m_lineWidth/2, m_drawRect.Height/2+1), 229 new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Height/2+m_lineWidth/2+1), 230 new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Height/2+m_lineWidth/2+1), 231 new Point(m_drawRect.Left+m_lineWidth/2, m_drawRect.Height/2+1) 232 }); 233 path.CloseAllFigures(); 234 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 235 } 236 } 237 } 238 } 239 }
以上就是单个字符的了
=======================分割线==========================
下面对数字控件处理
添加一个用户控件UCLEDNums
添加一点属性
1 private string m_value; 2 3 [Description("值"), Category("自定义")] 4 public string Value 5 { 6 get { return m_value; } 7 set 8 { 9 m_value = value; 10 ReloadValue(); 11 } 12 } 13 14 private int m_lineWidth = 8; 15 16 [Description("线宽度,为了更好的显示效果,请使用偶数"), Category("自定义")] 17 public int LineWidth 18 { 19 get { return m_lineWidth; } 20 set 21 { 22 m_lineWidth = value; 23 foreach (UCLEDNum c in this.Controls) 24 { 25 c.LineWidth = value; 26 } 27 } 28 } 29 30 [Description("颜色"), Category("自定义")] 31 public override System.Drawing.Color ForeColor 32 { 33 get 34 { 35 return base.ForeColor; 36 } 37 set 38 { 39 base.ForeColor = value; 40 foreach (UCLEDNum c in this.Controls) 41 { 42 c.ForeColor = value; 43 } 44 } 45 } 46 47 public override RightToLeft RightToLeft 48 { 49 get 50 { 51 return base.RightToLeft; 52 } 53 set 54 { 55 base.RightToLeft = value; 56 ReloadValue(); 57 } 58 }
加载控件的函数
1 private void ReloadValue() 2 { 3 try 4 { 5 ControlHelper.FreezeControl(this, true); 6 this.Controls.Clear(); 7 foreach (var item in m_value) 8 { 9 UCLEDNum uc = new UCLEDNum(); 10 if (RightToLeft == System.Windows.Forms.RightToLeft.Yes) 11 uc.Dock = DockStyle.Right; 12 else 13 uc.Dock = DockStyle.Left; 14 uc.Value = item; 15 uc.ForeColor = ForeColor; 16 uc.LineWidth = m_lineWidth; 17 this.Controls.Add(uc); 18 if (RightToLeft == System.Windows.Forms.RightToLeft.Yes) 19 uc.SendToBack(); 20 else 21 uc.BringToFront(); 22 } 23 } 24 finally 25 { 26 ControlHelper.FreezeControl(this, false); 27 } 28 }
完整代码及效果
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Drawing; 5 using System.Data; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace HZH_Controls.Controls.LED 11 { 12 public partial class UCLEDNums : UserControl 13 { 14 private string m_value; 15 16 [Description("值"), Category("自定义")] 17 public string Value 18 { 19 get { return m_value; } 20 set 21 { 22 m_value = value; 23 ReloadValue(); 24 } 25 } 26 27 private int m_lineWidth = 8; 28 29 [Description("线宽度,为了更好的显示效果,请使用偶数"), Category("自定义")] 30 public int LineWidth 31 { 32 get { return m_lineWidth; } 33 set 34 { 35 m_lineWidth = value; 36 foreach (UCLEDNum c in this.Controls) 37 { 38 c.LineWidth = value; 39 } 40 } 41 } 42 43 [Description("颜色"), Category("自定义")] 44 public override System.Drawing.Color ForeColor 45 { 46 get 47 { 48 return base.ForeColor; 49 } 50 set 51 { 52 base.ForeColor = value; 53 foreach (UCLEDNum c in this.Controls) 54 { 55 c.ForeColor = value; 56 } 57 } 58 } 59 60 public override RightToLeft RightToLeft 61 { 62 get 63 { 64 return base.RightToLeft; 65 } 66 set 67 { 68 base.RightToLeft = value; 69 ReloadValue(); 70 } 71 } 72 73 private void ReloadValue() 74 { 75 try 76 { 77 ControlHelper.FreezeControl(this, true); 78 this.Controls.Clear(); 79 foreach (var item in m_value) 80 { 81 UCLEDNum uc = new UCLEDNum(); 82 if (RightToLeft == System.Windows.Forms.RightToLeft.Yes) 83 uc.Dock = DockStyle.Right; 84 else 85 uc.Dock = DockStyle.Left; 86 uc.Value = item; 87 uc.ForeColor = ForeColor; 88 uc.LineWidth = m_lineWidth; 89 this.Controls.Add(uc); 90 if (RightToLeft == System.Windows.Forms.RightToLeft.Yes) 91 uc.SendToBack(); 92 else 93 uc.BringToFront(); 94 } 95 } 96 finally 97 { 98 ControlHelper.FreezeControl(this, false); 99 } 100 } 101 public UCLEDNums() 102 { 103 InitializeComponent(); 104 Value = "0.00"; 105 } 106 } 107 }
1 namespace HZH_Controls.Controls.LED 2 { 3 partial class UCLEDNums 4 { 5 ///6 /// 必需的设计器变量。 7 /// 8 private System.ComponentModel.IContainer components = null; 9 10 /// 11 /// 清理所有正在使用的资源。 12 /// 13 /// 如果应释放托管资源,为 true;否则为 false。 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region 组件设计器生成的代码 24 25 /// 26 /// 设计器支持所需的方法 - 不要 27 /// 使用代码编辑器修改此方法的内容。 28 /// 29 private void InitializeComponent() 30 { 31 this.SuspendLayout(); 32 // 33 // UCLEDNums 34 // 35 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 36 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 37 this.Name = "UCLEDNums"; 38 this.Size = new System.Drawing.Size(150, 58); 39 this.ResumeLayout(false); 40 41 } 42 43 #endregion 44 } 45 }
=======================分割线==========================
下面是日期类控件了,这里偷懒,分成3个控件,分别是日期控件,时间控件,日期时间控件
先说日期控件,
添加一个用户控件UCLEDData
添加属性
1 private DateTime m_value; 2 3 [Description("值"), Category("自定义")] 4 public DateTime Value 5 { 6 get { return m_value; } 7 set 8 { 9 m_value = value; 10 string str = value.ToString("yyyy-MM-dd"); 11 for (int i = 0; i < str.Length; i++) 12 { 13 ((UCLEDNum)this.tableLayoutPanel1.Controls.Find("D" + (i + 1), false)[0]).Value = str[i]; 14 } 15 } 16 } 17 18 private int m_lineWidth = 8; 19 20 [Description("线宽度,为了更好的显示效果,请使用偶数"), Category("自定义")] 21 public int LineWidth 22 { 23 get { return m_lineWidth; } 24 set 25 { 26 m_lineWidth = value; 27 foreach (UCLEDNum c in this.tableLayoutPanel1.Controls) 28 { 29 c.LineWidth = value; 30 } 31 } 32 } 33 34 [Description("颜色"), Category("自定义")] 35 public override System.Drawing.Color ForeColor 36 { 37 get 38 { 39 return base.ForeColor; 40 } 41 set 42 { 43 base.ForeColor = value; 44 foreach (UCLEDNum c in this.tableLayoutPanel1.Controls) 45 { 46 c.ForeColor = value; 47 } 48 } 49 }
完整代码
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Drawing; 5 using System.Data; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace HZH_Controls.Controls 11 { 12 public partial class UCLEDData : UserControl 13 { 14 private DateTime m_value; 15 16 [Description("值"), Category("自定义")] 17 public DateTime Value 18 { 19 get { return m_value; } 20 set 21 { 22 m_value = value; 23 string str = value.ToString("yyyy-MM-dd"); 24 for (int i = 0; i < str.Length; i++) 25 { 26 ((UCLEDNum)this.tableLayoutPanel1.Controls.Find("D" + (i + 1), false)[0]).Value = str[i]; 27 } 28 } 29 } 30 31 private int m_lineWidth = 8; 32 33 [Description("线宽度,为了更好的显示效果,请使用偶数"), Category("自定义")] 34 public int LineWidth 35 { 36 get { return m_lineWidth; } 37 set 38 { 39 m_lineWidth = value; 40 foreach (UCLEDNum c in this.tableLayoutPanel1.Controls) 41 { 42 c.LineWidth = value; 43 } 44 } 45 } 46 47 [Description("颜色"), Category("自定义")] 48 public override System.Drawing.Color ForeColor 49 { 50 get 51 { 52 return base.ForeColor; 53 } 54 set 55 { 56 base.ForeColor = value; 57 foreach (UCLEDNum c in this.tableLayoutPanel1.Controls) 58 { 59 c.ForeColor = value; 60 } 61 } 62 } 63 public UCLEDData() 64 { 65 InitializeComponent(); 66 Value = DateTime.Now; 67 } 68 } 69 }
1 namespace HZH_Controls.Controls 2 { 3 partial class UCLEDData 4 { 5 ///6 /// 必需的设计器变量。 7 /// 8 private System.ComponentModel.IContainer components = null; 9 10 /// 11 /// 清理所有正在使用的资源。 12 /// 13 /// 如果应释放托管资源,为 true;否则为 false。 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region 组件设计器生成的代码 24 25 /// 26 /// 设计器支持所需的方法 - 不要 27 /// 使用代码编辑器修改此方法的内容。 28 /// 29 private void InitializeComponent() 30 { 31 this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); 32 this.D1 = new HZH_Controls.Controls.UCLEDNum(); 33 this.D2 = new HZH_Controls.Controls.UCLEDNum(); 34 this.D3 = new HZH_Controls.Controls.UCLEDNum(); 35 this.D4 = new HZH_Controls.Controls.UCLEDNum(); 36 this.D5 = new HZH_Controls.Controls.UCLEDNum(); 37 this.D6 = new HZH_Controls.Controls.UCLEDNum(); 38 this.D7 = new HZH_Controls.Controls.UCLEDNum(); 39 this.D8 = new HZH_Controls.Controls.UCLEDNum(); 40 this.D9 = new HZH_Controls.Controls.UCLEDNum(); 41 this.D10 = new HZH_Controls.Controls.UCLEDNum(); 42 this.tableLayoutPanel1.SuspendLayout(); 43 this.SuspendLayout(); 44 // 45 // tableLayoutPanel1 46 // 47 this.tableLayoutPanel1.ColumnCount = 10; 48 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 10F)); 49 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 10F)); 50 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 10F)); 51 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 10F)); 52 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 10F)); 53 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 10F)); 54 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 10F)); 55 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 10F)); 56 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 10F)); 57 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 10F)); 58 this.tableLayoutPanel1.Controls.Add(this.D1, 0, 0); 59 this.tableLayoutPanel1.Controls.Add(this.D2, 1, 0); 60 this.tableLayoutPanel1.Controls.Add(this.D3, 2, 0); 61 this.tableLayoutPanel1.Controls.Add(this.D4, 3, 0); 62 this.tableLayoutPanel1.Controls.Add(this.D5, 4, 0); 63 this.tableLayoutPanel1.Controls.Add(this.D6, 5, 0); 64 this.tableLayoutPanel1.Controls.Add(this.D7, 6, 0); 65 this.tableLayoutPanel1.Controls.Add(this.D8, 7, 0); 66 this.tableLayoutPanel1.Controls.Add(this.D9, 8, 0); 67 this.tableLayoutPanel1.Controls.Add(this.D10, 9, 0); 68 this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill; 69 this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0); 70 this.tableLayoutPanel1.Name = "tableLayoutPanel1"; 71 this.tableLayoutPanel1.RowCount = 1; 72 this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); 73 this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F)); 74 this.tableLayoutPanel1.Size = new System.Drawing.Size(360, 58); 75 this.tableLayoutPanel1.TabIndex = 0; 76 // 77 // D1 78 // 79 this.D1.Dock = System.Windows.Forms.DockStyle.Fill; 80 this.D1.LineWidth = 8; 81 this.D1.Location = new System.Drawing.Point(3, 3); 82 this.D1.Name = "D1"; 83 this.D1.Size = new System.Drawing.Size(30, 52); 84 this.D1.TabIndex = 0; 85 this.D1.Value = '2'; 86 // 87 // D2 88 // 89 this.D2.Dock = System.Windows.Forms.DockStyle.Fill; 90 this.D2.LineWidth = 8; 91 this.D2.Location = new System.Drawing.Point(39, 3); 92 this.D2.Name = "D2"; 93 this.D2.Size = new System.Drawing.Size(30, 52); 94 this.D2.TabIndex = 1; 95 this.D2.Value = '0'; 96 // 97 // D3 98 // 99 this.D3.Dock = System.Windows.Forms.DockStyle.Fill; 100 this.D3.LineWidth = 8; 101 this.D3.Location = new System.Drawing.Point(75, 3); 102 this.D3.Name = "D3"; 103 this.D3.Size = new System.Drawing.Size(30, 52); 104 this.D3.TabIndex = 2; 105 this.D3.Value = '1'; 106 // 107 // D4 108 // 109 this.D4.Dock = System.Windows.Forms.DockStyle.Fill; 110 this.D4.LineWidth = 8; 111 this.D4.Location = new System.Drawing.Point(111, 3); 112 this.D4.Name = "D4"; 113 this.D4.Size = new System.Drawing.Size(30, 52); 114 this.D4.TabIndex = 3; 115 this.D4.Value = '9'; 116 // 117 // D5 118 // 119 this.D5.Dock = System.Windows.Forms.DockStyle.Fill; 120 this.D5.LineWidth = 8; 121 this.D5.Location = new System.Drawing.Point(147, 3); 122 this.D5.Name = "D5"; 123 this.D5.Size = new System.Drawing.Size(30, 52); 124 this.D5.TabIndex = 4; 125 this.D5.Value = '-'; 126 // 127 // D6 128 // 129 this.D6.Dock = System.Windows.Forms.DockStyle.Fill; 130 this.D6.LineWidth = 8; 131 this.D6.Location = new System.Drawing.Point(183, 3); 132 this.D6.Name = "D6"; 133 this.D6.Size = new System.Drawing.Size(30, 52); 134 this.D6.TabIndex = 5; 135 this.D6.Value = '0'; 136 // 137 // D7 138 // 139 this.D7.Dock = System.Windows.Forms.DockStyle.Fill; 140 this.D7.LineWidth = 8; 141 this.D7.Location = new System.Drawing.Point(219, 3); 142 this.D7.Name = "D7"; 143 this.D7.Size = new System.Drawing.Size(30, 52); 144 this.D7.TabIndex = 6; 145 this.D7.Value = '8'; 146 // 147 // D8 148 // 149 this.D8.Dock = System.Windows.Forms.DockStyle.Fill; 150 this.D8.LineWidth = 8; 151 this.D8.Location = new System.Drawing.Point(255, 3); 152 this.D8.Name = "D8"; 153 this.D8.Size = new System.Drawing.Size(30, 52); 154 this.D8.TabIndex = 7; 155 this.D8.Value = '-'; 156 // 157 // D9 158 // 159 this.D9.Dock = System.Windows.Forms.DockStyle.Fill; 160 this.D9.LineWidth = 8; 161 this.D9.Location = new System.Drawing.Point(291, 3); 162 this.D9.Name = "D9"; 163 this.D9.Size = new System.Drawing.Size(30, 52); 164 this.D9.TabIndex = 8; 165 this.D9.Value = '0'; 166 // 167 // D10 168 // 169 this.D10.Dock = System.Windows.Forms.DockStyle.Fill; 170 this.D10.LineWidth = 8; 171 this.D10.Location = new System.Drawing.Point(327, 3); 172 this.D10.Name = "D10"; 173 this.D10.Size = new System.Drawing.Size(30, 52); 174 this.D10.TabIndex = 9; 175 this.D10.Value = '1'; 176 // 177 // UCLEDData 178 // 179 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; 180 this.Controls.Add(this.tableLayoutPanel1); 181 this.Margin = new System.Windows.Forms.Padding(0); 182 this.Name = "UCLEDData"; 183 this.Size = new System.Drawing.Size(360, 58); 184 this.tableLayoutPanel1.ResumeLayout(false); 185 this.ResumeLayout(false); 186 187 } 188 189 #endregion 190 191 private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; 192 private UCLEDNum D1; 193 private UCLEDNum D2; 194 private UCLEDNum D3; 195 private UCLEDNum D4; 196 private UCLEDNum D5; 197 private UCLEDNum D6; 198 private UCLEDNum D7; 199 private UCLEDNum D8; 200 private UCLEDNum D9; 201 private UCLEDNum D10; 202 203 } 204 }
=======================分割线==========================
时间控件
添加一个用户控件UCLEDTime
添加属性
1 private DateTime m_value; 2 3 [Description("值"), Category("自定义")] 4 public DateTime Value 5 { 6 get { return m_value; } 7 set 8 { 9 m_value = value; 10 string str = value.ToString("HH:mm:ss"); 11 for (int i = 0; i < str.Length; i++) 12 { 13 ((UCLEDNum)this.tableLayoutPanel1.Controls.Find("D" + (i + 1), false)[0]).Value = str[i]; 14 } 15 } 16 } 17 18 private int m_lineWidth = 8; 19 20 [Description("线宽度,为了更好的显示效果,请使用偶数"), Category("自定义")] 21 public int LineWidth 22 { 23 get { return m_lineWidth; } 24 set 25 { 26 m_lineWidth = value; 27 foreach (UCLEDNum c in this.tableLayoutPanel1.Controls) 28 { 29 c.LineWidth = value; 30 } 31 } 32 } 33 34 [Description("颜色"), Category("自定义")] 35 public override System.Drawing.Color ForeColor 36 { 37 get 38 { 39 return base.ForeColor; 40 } 41 set 42 { 43 base.ForeColor = value; 44 foreach (UCLEDNum c in this.tableLayoutPanel1.Controls) 45 { 46 c.ForeColor = value; 47 } 48 } 49 }
全部代码
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Drawing; 5 using System.Data; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace HZH_Controls.Controls 11 { 12 public partial class UCLEDTime : UserControl 13 { 14 private DateTime m_value; 15 16 [Description("值"), Category("自定义")] 17 public DateTime Value 18 { 19 get { return m_value; } 20 set 21 { 22 m_value = value; 23 string str = value.ToString("HH:mm:ss"); 24 for (int i = 0; i < str.Length; i++) 25 { 26 ((UCLEDNum)this.tableLayoutPanel1.Controls.Find("D" + (i + 1), false)[0]).Value = str[i]; 27 } 28 } 29 } 30 31 private int m_lineWidth = 8; 32 33 [Description("线宽度,为了更好的显示效果,请使用偶数"), Category("自定义")] 34 public int LineWidth 35 { 36 get { return m_lineWidth; } 37 set 38 { 39 m_lineWidth = value; 40 foreach (UCLEDNum c in this.tableLayoutPanel1.Controls) 41 { 42 c.LineWidth = value; 43 } 44 } 45 } 46 47 [Description("颜色"), Category("自定义")] 48 public override System.Drawing.Color ForeColor 49 { 50 get 51 { 52 return base.ForeColor; 53 } 54 set 55 { 56 base.ForeColor = value; 57 foreach (UCLEDNum c in this.tableLayoutPanel1.Controls) 58 { 59 c.ForeColor = value; 60 } 61 } 62 } 63 public UCLEDTime() 64 { 65 InitializeComponent(); 66 Value = DateTime.Now; 67 } 68 } 69 }
1 namespace HZH_Controls.Controls 2 { 3 partial class UCLEDTime 4 { 5 ///6 /// 必需的设计器变量。 7 /// 8 private System.ComponentModel.IContainer components = null; 9 10 /// 11 /// 清理所有正在使用的资源。 12 /// 13 /// 如果应释放托管资源,为 true;否则为 false。 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region 组件设计器生成的代码 24 25 /// 26 /// 设计器支持所需的方法 - 不要 27 /// 使用代码编辑器修改此方法的内容。 28 /// 29 private void InitializeComponent() 30 { 31 this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); 32 this.D1 = new HZH_Controls.Controls.UCLEDNum(); 33 this.D2 = new HZH_Controls.Controls.UCLEDNum(); 34 this.D3 = new HZH_Controls.Controls.UCLEDNum(); 35 this.D4 = new HZH_Controls.Controls.UCLEDNum(); 36 this.D5 = new HZH_Controls.Controls.UCLEDNum(); 37 this.D6 = new HZH_Controls.Controls.UCLEDNum(); 38 this.D7 = new HZH_Controls.Controls.UCLEDNum(); 39 this.D8 = new HZH_Controls.Controls.UCLEDNum(); 40 this.tableLayoutPanel1.SuspendLayout(); 41 this.SuspendLayout(); 42 // 43 // tableLayoutPanel1 44 // 45 this.tableLayoutPanel1.ColumnCount = 8; 46 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 12.5F)); 47 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 12.5F)); 48 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 12.5F)); 49 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 12.5F)); 50 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 12.5F)); 51 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 12.5F)); 52 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 12.5F)); 53 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 12.5F)); 54 this.tableLayoutPanel1.Controls.Add(this.D1, 0, 0); 55 this.tableLayoutPanel1.Controls.Add(this.D2, 1, 0); 56 this.tableLayoutPanel1.Controls.Add(this.D3, 2, 0); 57 this.tableLayoutPanel1.Controls.Add(this.D4, 3, 0); 58 this.tableLayoutPanel1.Controls.Add(this.D5, 4, 0); 59 this.tableLayoutPanel1.Controls.Add(this.D6, 5, 0); 60 this.tableLayoutPanel1.Controls.Add(this.D7, 6, 0); 61 this.tableLayoutPanel1.Controls.Add(this.D8, 7, 0); 62 this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill; 63 this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0); 64 this.tableLayoutPanel1.Name = "tableLayoutPanel1"; 65 this.tableLayoutPanel1.RowCount = 1; 66 this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); 67 this.tableLayoutPanel1.Size = new System.Drawing.Size(290, 58); 68 this.tableLayoutPanel1.TabIndex = 0; 69 // 70 // D1 71 // 72 this.D1.Dock = System.Windows.Forms.DockStyle.Fill; 73 this.D1.LineWidth = 8; 74 this.D1.Location = new System.Drawing.Point(3, 3); 75 this.D1.Name = "D1"; 76 this.D1.Size = new System.Drawing.Size(30, 52); 77 this.D1.TabIndex = 0; 78 this.D1.Value = '2'; 79 // 80 // D2 81 // 82 this.D2.Dock = System.Windows.Forms.DockStyle.Fill; 83 this.D2.LineWidth = 8; 84 this.D2.Location = new System.Drawing.Point(39, 3); 85 this.D2.Name = "D2"; 86 this.D2.Size = new System.Drawing.Size(30, 52); 87 this.D2.TabIndex = 1; 88 this.D2.Value = '3'; 89 // 90 // D3 91 // 92 this.D3.Dock = System.Windows.Forms.DockStyle.Fill; 93 this.D3.LineWidth = 8; 94 this.D3.Location = new System.Drawing.Point(75, 3); 95 this.D3.Name = "D3"; 96 this.D3.Size = new System.Drawing.Size(30, 52); 97 this.D3.TabIndex = 2; 98 this.D3.Value = ':'; 99 // 100 // D4 101 // 102 this.D4.Dock = System.Windows.Forms.DockStyle.Fill; 103 this.D4.LineWidth = 8; 104 this.D4.Location = new System.Drawing.Point(111, 3); 105 this.D4.Name = "D4"; 106 this.D4.Size = new System.Drawing.Size(30, 52); 107 this.D4.TabIndex = 3; 108 this.D4.Value = '1'; 109 // 110 // D5 111 // 112 this.D5.Dock = System.Windows.Forms.DockStyle.Fill; 113 this.D5.LineWidth = 8; 114 this.D5.Location = new System.Drawing.Point(147, 3); 115 this.D5.Name = "D5"; 116 this.D5.Size = new System.Drawing.Size(30, 52); 117 this.D5.TabIndex = 4; 118 this.D5.Value = '0'; 119 // 120 // D6 121 // 122 this.D6.Dock = System.Windows.Forms.DockStyle.Fill; 123 this.D6.LineWidth = 8; 124 this.D6.Location = new System.Drawing.Point(183, 3); 125 this.D6.Name = "D6"; 126 this.D6.Size = new System.Drawing.Size(30, 52); 127 this.D6.TabIndex = 5; 128 this.D6.Value = ':'; 129 // 130 // D7 131 // 132 this.D7.Dock = System.Windows.Forms.DockStyle.Fill; 133 this.D7.LineWidth = 8; 134 this.D7.Location = new System.Drawing.Point(219, 3); 135 this.D7.Name = "D7"; 136 this.D7.Size = new System.Drawing.Size(30, 52); 137 this.D7.TabIndex = 6; 138 this.D7.Value = '1'; 139 // 140 // D8 141 // 142 this.D8.Dock = System.Windows.Forms.DockStyle.Fill; 143 this.D8.LineWidth = 8; 144 this.D8.Location = new System.Drawing.Point(255, 3); 145 this.D8.Name = "D8"; 146 this.D8.Size = new System.Drawing.Size(32, 52); 147 this.D8.TabIndex = 7; 148 this.D8.Value = '0'; 149 // 150 // UCLEDTime 151 // 152 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; 153 this.Controls.Add(this.tableLayoutPanel1); 154 this.Name = "UCLEDTime"; 155 this.Size = new System.Drawing.Size(290, 58); 156 this.tableLayoutPanel1.ResumeLayout(false); 157 this.ResumeLayout(false); 158 159 } 160 161 #endregion 162 163 private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; 164 private UCLEDNum D1; 165 private UCLEDNum D2; 166 private UCLEDNum D3; 167 private UCLEDNum D4; 168 private UCLEDNum D5; 169 private UCLEDNum D6; 170 private UCLEDNum D7; 171 private UCLEDNum D8; 172 } 173 }
=======================分割线==========================
日期时间控件
添加一个用户控件UCLEDDataTime
添加属性
1 private DateTime m_value; 2 3 [Description("值"), Category("自定义")] 4 public DateTime Value 5 { 6 get { return m_value; } 7 set 8 { 9 m_value = value; 10 string str = value.ToString("yyyy-MM-dd HH:mm:ss"); 11 for (int i = 0; i < str.Length; i++) 12 { 13 if (i == 10) 14 continue; 15 ((UCLEDNum)this.tableLayoutPanel1.Controls.Find("D" + (i + 1), false)[0]).Value = str[i]; 16 } 17 } 18 } 19 20 private int m_lineWidth = 8; 21 22 [Description("线宽度,为了更好的显示效果,请使用偶数"), Category("自定义")] 23 public int LineWidth 24 { 25 get { return m_lineWidth; } 26 set 27 { 28 m_lineWidth = value; 29 foreach (UCLEDNum c in this.tableLayoutPanel1.Controls) 30 { 31 c.LineWidth = value; 32 } 33 } 34 } 35 36 [Description("颜色"), Category("自定义")] 37 public override System.Drawing.Color ForeColor 38 { 39 get 40 { 41 return base.ForeColor; 42 } 43 set 44 { 45 base.ForeColor = value; 46 foreach (UCLEDNum c in this.tableLayoutPanel1.Controls) 47 { 48 c.ForeColor = value; 49 } 50 } 51 }
全部代码
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Drawing; 5 using System.Data; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace HZH_Controls.Controls 11 { 12 public partial class UCLEDDataTime : UserControl 13 { 14 private DateTime m_value; 15 16 [Description("值"), Category("自定义")] 17 public DateTime Value 18 { 19 get { return m_value; } 20 set 21 { 22 m_value = value; 23 string str = value.ToString("yyyy-MM-dd HH:mm:ss"); 24 for (int i = 0; i < str.Length; i++) 25 { 26 if (i == 10) 27 continue; 28 ((UCLEDNum)this.tableLayoutPanel1.Controls.Find("D" + (i + 1), false)[0]).Value = str[i]; 29 } 30 } 31 } 32 33 private int m_lineWidth = 8; 34 35 [Description("线宽度,为了更好的显示效果,请使用偶数"), Category("自定义")] 36 public int LineWidth 37 { 38 get { return m_lineWidth; } 39 set 40 { 41 m_lineWidth = value; 42 foreach (UCLEDNum c in this.tableLayoutPanel1.Controls) 43 { 44 c.LineWidth = value; 45 } 46 } 47 } 48 49 [Description("颜色"), Category("自定义")] 50 public override System.Drawing.Color ForeColor 51 { 52 get 53 { 54 return base.ForeColor; 55 } 56 set 57 { 58 base.ForeColor = value; 59 foreach (UCLEDNum c in this.tableLayoutPanel1.Controls) 60 { 61 c.ForeColor = value; 62 } 63 } 64 } 65 public UCLEDDataTime() 66 { 67 InitializeComponent(); 68 Value = DateTime.Now; 69 } 70 } 71 }
1 namespace HZH_Controls.Controls 2 { 3 partial class UCLEDDataTime 4 { 5 ///6 /// 必需的设计器变量。 7 /// 8 private System.ComponentModel.IContainer components = null; 9 10 /// 11 /// 清理所有正在使用的资源。 12 /// 13 /// 如果应释放托管资源,为 true;否则为 false。 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region 组件设计器生成的代码 24 25 /// 26 /// 设计器支持所需的方法 - 不要 27 /// 使用代码编辑器修改此方法的内容。 28 /// 29 private void InitializeComponent() 30 { 31 this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); 32 this.D1 = new HZH_Controls.Controls.UCLEDNum(); 33 this.D2 = new HZH_Controls.Controls.UCLEDNum(); 34 this.D3 = new HZH_Controls.Controls.UCLEDNum(); 35 this.D4 = new HZH_Controls.Controls.UCLEDNum(); 36 this.D5 = new HZH_Controls.Controls.UCLEDNum(); 37 this.D6 = new HZH_Controls.Controls.UCLEDNum(); 38 this.D7 = new HZH_Controls.Controls.UCLEDNum(); 39 this.D8 = new HZH_Controls.Controls.UCLEDNum(); 40 this.D9 = new HZH_Controls.Controls.UCLEDNum(); 41 this.D10 = new HZH_Controls.Controls.UCLEDNum(); 42 this.D12 = new HZH_Controls.Controls.UCLEDNum(); 43 this.D13 = new HZH_Controls.Controls.UCLEDNum(); 44 this.D14 = new HZH_Controls.Controls.UCLEDNum(); 45 this.D15 = new HZH_Controls.Controls.UCLEDNum(); 46 this.D16 = new HZH_Controls.Controls.UCLEDNum(); 47 this.D17 = new HZH_Controls.Controls.UCLEDNum(); 48 this.D18 = new HZH_Controls.Controls.UCLEDNum(); 49 this.D19 = new HZH_Controls.Controls.UCLEDNum(); 50 this.tableLayoutPanel1.SuspendLayout(); 51 this.SuspendLayout(); 52 // 53 // tableLayoutPanel1 54 // 55 this.tableLayoutPanel1.ColumnCount = 19; 56 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5.263158F)); 57 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5.263158F)); 58 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5.263158F)); 59 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5.263158F)); 60 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5.263158F)); 61 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5.263158F)); 62 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5.263158F)); 63 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5.263158F)); 64 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5.263158F)); 65 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5.263158F)); 66 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5.263158F)); 67 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5.263158F)); 68 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5.263158F)); 69 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5.263158F)); 70 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5.263158F)); 71 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5.263158F)); 72 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5.263158F)); 73 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5.263158F)); 74 this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5.263158F)); 75 this.tableLayoutPanel1.Controls.Add(this.D1, 0, 0); 76 this.tableLayoutPanel1.Controls.Add(this.D2, 1, 0); 77 this.tableLayoutPanel1.Controls.Add(this.D3, 2, 0); 78 this.tableLayoutPanel1.Controls.Add(this.D4, 3, 0); 79 this.tableLayoutPanel1.Controls.Add(this.D5, 4, 0); 80 this.tableLayoutPanel1.Controls.Add(this.D6, 5, 0); 81 this.tableLayoutPanel1.Controls.Add(this.D7, 6, 0); 82 this.tableLayoutPanel1.Controls.Add(this.D8, 7, 0); 83 this.tableLayoutPanel1.Controls.Add(this.D9, 8, 0); 84 this.tableLayoutPanel1.Controls.Add(this.D10, 9, 0); 85 this.tableLayoutPanel1.Controls.Add(this.D12, 11, 0); 86 this.tableLayoutPanel1.Controls.Add(this.D13, 12, 0); 87 this.tableLayoutPanel1.Controls.Add(this.D14, 13, 0); 88 this.tableLayoutPanel1.Controls.Add(this.D15, 14, 0); 89 this.tableLayoutPanel1.Controls.Add(this.D16, 15, 0); 90 this.tableLayoutPanel1.Controls.Add(this.D17, 16, 0); 91 this.tableLayoutPanel1.Controls.Add(this.D18, 17, 0); 92 this.tableLayoutPanel1.Controls.Add(this.D19, 18, 0); 93 this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill; 94 this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0); 95 this.tableLayoutPanel1.Name = "tableLayoutPanel1"; 96 this.tableLayoutPanel1.RowCount = 1; 97 this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); 98 this.tableLayoutPanel1.Size = new System.Drawing.Size(650, 58); 99 this.tableLayoutPanel1.TabIndex = 1; 100 // 101 // D1 102 // 103 this.D1.Dock = System.Windows.Forms.DockStyle.Fill; 104 this.D1.LineWidth = 8; 105 this.D1.Location = new System.Drawing.Point(3, 3); 106 this.D1.Name = "D1"; 107 this.D1.Size = new System.Drawing.Size(28, 52); 108 this.D1.TabIndex = 0; 109 this.D1.Value = '2'; 110 // 111 // D2 112 // 113 this.D2.Dock = System.Windows.Forms.DockStyle.Fill; 114 this.D2.LineWidth = 8; 115 this.D2.Location = new System.Drawing.Point(37, 3); 116 this.D2.Name = "D2"; 117 this.D2.Size = new System.Drawing.Size(28, 52); 118 this.D2.TabIndex = 1; 119 this.D2.Value = '0'; 120 // 121 // D3 122 // 123 this.D3.Dock = System.Windows.Forms.DockStyle.Fill; 124 this.D3.LineWidth = 8; 125 this.D3.Location = new System.Drawing.Point(71, 3); 126 this.D3.Name = "D3"; 127 this.D3.Size = new System.Drawing.Size(28, 52); 128 this.D3.TabIndex = 2; 129 this.D3.Value = '1'; 130 // 131 // D4 132 // 133 this.D4.Dock = System.Windows.Forms.DockStyle.Fill; 134 this.D4.LineWidth = 8; 135 this.D4.Location = new System.Drawing.Point(105, 3); 136 this.D4.Name = "D4"; 137 this.D4.Size = new System.Drawing.Size(28, 52); 138 this.D4.TabIndex = 3; 139 this.D4.Value = '9'; 140 // 141 // D5 142 // 143 this.D5.Dock = System.Windows.Forms.DockStyle.Fill; 144 this.D5.LineWidth = 8; 145 this.D5.Location = new System.Drawing.Point(139, 3); 146 this.D5.Name = "D5"; 147 this.D5.Size = new System.Drawing.Size(28, 52); 148 this.D5.TabIndex = 4; 149 this.D5.Value = '-'; 150 // 151 // D6 152 // 153 this.D6.Dock = System.Windows.Forms.DockStyle.Fill; 154 this.D6.LineWidth = 8; 155 this.D6.Location = new System.Drawing.Point(173, 3); 156 this.D6.Name = "D6"; 157 this.D6.Size = new System.Drawing.Size(28, 52); 158 this.D6.TabIndex = 5; 159 this.D6.Value = '0'; 160 // 161 // D7 162 // 163 this.D7.Dock = System.Windows.Forms.DockStyle.Fill; 164 this.D7.LineWidth = 8; 165 this.D7.Location = new System.Drawing.Point(207, 3); 166 this.D7.Name = "D7"; 167 this.D7.Size = new System.Drawing.Size(28, 52); 168 this.D7.TabIndex = 6; 169 this.D7.Value = '8'; 170 // 171 // D8 172 // 173 this.D8.Dock = System.Windows.Forms.DockStyle.Fill; 174 this.D8.LineWidth = 8; 175 this.D8.Location = new System.Drawing.Point(241, 3); 176 this.D8.Name = "D8"; 177 this.D8.Size = new System.Drawing.Size(28, 52); 178 this.D8.TabIndex = 7; 179 this.D8.Value = '-'; 180 // 181 // D9 182 // 183 this.D9.Dock = System.Windows.Forms.DockStyle.Fill; 184 this.D9.LineWidth = 8; 185 this.D9.Location = new System.Drawing.Point(275, 3); 186 this.D9.Name = "D9"; 187 this.D9.Size = new System.Drawing.Size(28, 52); 188 this.D9.TabIndex = 8; 189 this.D9.Value = '0'; 190 // 191 // D10 192 // 193 this.D10.Dock = System.Windows.Forms.DockStyle.Fill; 194 this.D10.LineWidth = 8; 195 this.D10.Location = new System.Drawing.Point(309, 3); 196 this.D10.Name = "D10"; 197 this.D10.Size = new System.Drawing.Size(28, 52); 198 this.D10.TabIndex = 9; 199 this.D10.Value = '1'; 200 // 201 // D12 202 // 203 this.D12.Dock = System.Windows.Forms.DockStyle.Fill; 204 this.D12.LineWidth = 8; 205 this.D12.Location = new System.Drawing.Point(377, 3); 206 this.D12.Name = "D12"; 207 this.D12.Size = new System.Drawing.Size(28, 52); 208 this.D12.TabIndex = 10; 209 this.D12.Value = '2'; 210 // 211 // D13 212 // 213 this.D13.Dock = System.Windows.Forms.DockStyle.Fill; 214 this.D13.LineWidth = 8; 215 this.D13.Location = new System.Drawing.Point(411, 3); 216 this.D13.Name = "D13"; 217 this.D13.Size = new System.Drawing.Size(28, 52); 218 this.D13.TabIndex = 11; 219 this.D13.Value = '3'; 220 // 221 // D14 222 // 223 this.D14.Dock = System.Windows.Forms.DockStyle.Fill; 224 this.D14.LineWidth = 8; 225 this.D14.Location = new System.Drawing.Point(445, 3); 226 this.D14.Name = "D14"; 227 this.D14.Size = new System.Drawing.Size(28, 52); 228 this.D14.TabIndex = 12; 229 this.D14.Value = ':'; 230 // 231 // D15 232 // 233 this.D15.Dock = System.Windows.Forms.DockStyle.Fill; 234 this.D15.LineWidth = 8; 235 this.D15.Location = new System.Drawing.Point(479, 3); 236 this.D15.Name = "D15"; 237 this.D15.Size = new System.Drawing.Size(28, 52); 238 this.D15.TabIndex = 13; 239 this.D15.Value = '0'; 240 // 241 // D16 242 // 243 this.D16.Dock = System.Windows.Forms.DockStyle.Fill; 244 this.D16.LineWidth = 8; 245 this.D16.Location = new System.Drawing.Point(513, 3); 246 this.D16.Name = "D16"; 247 this.D16.Size = new System.Drawing.Size(28, 52); 248 this.D16.TabIndex = 14; 249 this.D16.Value = '1'; 250 // 251 // D17 252 // 253 this.D17.Dock = System.Windows.Forms.DockStyle.Fill; 254 this.D17.LineWidth = 8; 255 this.D17.Location = new System.Drawing.Point(547, 3); 256 this.D17.Name = "D17"; 257 this.D17.Size = new System.Drawing.Size(28, 52); 258 this.D17.TabIndex = 15; 259 this.D17.Value = ':'; 260 // 261 // D18 262 // 263 this.D18.Dock = System.Windows.Forms.DockStyle.Fill; 264 this.D18.LineWidth = 8; 265 this.D18.Location = new System.Drawing.Point(581, 3); 266 this.D18.Name = "D18"; 267 this.D18.Size = new System.Drawing.Size(28, 52); 268 this.D18.TabIndex = 16; 269 this.D18.Value = '5'; 270 // 271 // D19 272 // 273 this.D19.Dock = System.Windows.Forms.DockStyle.Fill; 274 this.D19.LineWidth = 8; 275 this.D19.Location = new System.Drawing.Point(615, 3); 276 this.D19.Name = "D19"; 277 this.D19.Size = new System.Drawing.Size(32, 52); 278 this.D19.TabIndex = 17; 279 this.D19.Value = '3'; 280 // 281 // UCLEDDataTime 282 // 283 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; 284 this.Controls.Add(this.tableLayoutPanel1); 285 this.Name = "UCLEDDataTime"; 286 this.Size = new System.Drawing.Size(650, 58); 287 this.tableLayoutPanel1.ResumeLayout(false); 288 this.ResumeLayout(false); 289 290 } 291 292 #endregion 293 294 private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; 295 private UCLEDNum D1; 296 private UCLEDNum D2; 297 private UCLEDNum D3; 298 private UCLEDNum D4; 299 private UCLEDNum D5; 300 private UCLEDNum D6; 301 private UCLEDNum D7; 302 private UCLEDNum D8; 303 private UCLEDNum D9; 304 private UCLEDNum D10; 305 private UCLEDNum D12; 306 private UCLEDNum D13; 307 private UCLEDNum D14; 308 private UCLEDNum D15; 309 private UCLEDNum D16; 310 private UCLEDNum D17; 311 private UCLEDNum D18; 312 private UCLEDNum D19; 313 } 314 }
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧