1. 程式人生 > >(五十二)c#Winform自定義控制元件-LED數字

(五十二)c#Winform自定義控制元件-LED數字

前提

入行已經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 }
View Code

 

 

 以上就是單個字元的了

 

=======================分割線==========================

下面對數字控制元件處理

新增一個使用者控制元件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 }
View Code
 1 namespace HZH_Controls.Controls.LED
 2 {
 3     partial class UCLEDNums
 4     {
 5         /// <summary> 
 6         /// 必需的設計器變數。
 7         /// </summary>
 8         private System.ComponentModel.IContainer components = null;
 9 
10         /// <summary> 
11         /// 清理所有正在使用的資源。
12         /// </summary>
13         /// <param name="disposing">如果應釋放託管資源,為 true;否則為 false。</param>
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         /// <summary> 
26         /// 設計器支援所需的方法 - 不要
27         /// 使用程式碼編輯器修改此方法的內容。
28         /// </summary>
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 }
View Code

 

 

 =======================分割線==========================

下面是日期類控制元件了,這裡偷懶,分成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 }
View Code
  1 namespace HZH_Controls.Controls
  2 {
  3     partial class UCLEDData
  4     {
  5         /// <summary> 
  6         /// 必需的設計器變數。
  7         /// </summary>
  8         private System.ComponentModel.IContainer components = null;
  9 
 10         /// <summary> 
 11         /// 清理所有正在使用的資源。
 12         /// </summary>
 13         /// <param name="disposing">如果應釋放託管資源,為 true;否則為 false。</param>
 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         /// <summary> 
 26         /// 設計器支援所需的方法 - 不要
 27         /// 使用程式碼編輯器修改此方法的內容。
 28         /// </summary>
 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 }
View Code

 =======================分割線==========================

時間控制元件

新增一個使用者控制元件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 }
View Code
  1 namespace HZH_Controls.Controls
  2 {
  3     partial class UCLEDTime
  4     {
  5         /// <summary> 
  6         /// 必需的設計器變數。
  7         /// </summary>
  8         private System.ComponentModel.IContainer components = null;
  9 
 10         /// <summary> 
 11         /// 清理所有正在使用的資源。
 12         /// </summary>
 13         /// <param name="disposing">如果應釋放託管資源,為 true;否則為 false。</param>
 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         /// <summary> 
 26         /// 設計器支援所需的方法 - 不要
 27         /// 使用程式碼編輯器修改此方法的內容。
 28         /// </summary>
 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 }
View Code

 =======================分割線==========================

日期時間控制元件

新增一個使用者控制元件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 }
View Code
  1 namespace HZH_Controls.Controls
  2 {
  3     partial class UCLEDDataTime
  4     {
  5         /// <summary> 
  6         /// 必需的設計器變數。
  7         /// </summary>
  8         private System.ComponentModel.IContainer components = null;
  9 
 10         /// <summary> 
 11         /// 清理所有正在使用的資源。
 12         /// </summary>
 13         /// <param name="disposing">如果應釋放託管資源,為 true;否則為 false。</param>
 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         /// <summary> 
 26         /// 設計器支援所需的方法 - 不要
 27         /// 使用程式碼編輯器修改此方法的內容。
 28         /// </summary>
 29         private void InitializeComponent()
 30         {
 31             this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
 32             this.D1 = new HZH_Controls.Controls.UCLEDNum();