(五十二)c#Winform自定义控件-LED数字

2021-02-05 00:16

阅读:482

标签:一点   完整   size   入行   ==   一个用户   code   graph   内容   

前提

入行已经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 Dictionarychar, int[]> m_nums = new Dictionarychar, 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 Dictionarychar, int[]> m_nums = new Dictionarychar, 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                 ReloadVal


评论


亲,登录后才可以留言!