前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

github:https://github.com/kwwwvagaa/netwinformcontrol

码云:

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 

麻烦博客下方点个【推荐】,谢谢

nuget

install-package hzh_controls

目录

用处及效果

准备工作

依然使用gdi+绘画,这个比较简单,就是画圆

开始

新增一个类ucsignallamp,继承usercontrol

添加属性

  1 /// <summary>
  2         /// the is show border
  3         /// </summary>
  4         private bool isshowborder = false;
  5 
  6         /// <summary>
  7         /// gets or sets a value indicating whether this instance is show border.
  8         /// </summary>
  9         /// <value><c>true</c> if this instance is show border; otherwise, <c>false</c>.</value>
 10         [description("是否显示边框"), category("自定义")]
 11         public bool isshowborder
 12         {
 13             get { return isshowborder; }
 14             set
 15             {
 16                 isshowborder = value;
 17                 refresh();
 18             }
 19         }
 20 
 21         /// <summary>
 22         /// the lamp color
 23         /// </summary>
 24         private color[] lampcolor = new color[] { color.fromargb(255, 77, 59) };
 25 
 26         /// <summary>
 27         /// gets or sets the color of the lamp.
 28         /// </summary>
 29         /// <value>the color of the lamp.</value>
 30         [description("灯颜色,当需要闪烁时,至少需要2个及以上颜色,不需要闪烁则至少需要1个颜色"), category("自定义")]
 31         public color[] lampcolor
 32         {
 33             get { return lampcolor; }
 34             set
 35             {
 36                 if (value == null || value.length <= 0)
 37                     return;
 38                 lampcolor = value;
 39                 refresh();
 40             }
 41         }
 42 
 43         /// <summary>
 44         /// the is highlight
 45         /// </summary>
 46         private bool ishighlight = true;
 47 
 48         /// <summary>
 49         /// gets or sets a value indicating whether this instance is highlight.
 50         /// </summary>
 51         /// <value><c>true</c> if this instance is highlight; otherwise, <c>false</c>.</value>
 52         [description("是否高亮显示"), category("自定义")]
 53         public bool ishighlight
 54         {
 55             get { return ishighlight; }
 56             set
 57             {
 58                 ishighlight = value;
 59                 refresh();
 60             }
 61         }
 62 
 63         /// <summary>
 64         /// the twinkle speed
 65         /// </summary>
 66         private int twinklespeed = 0;
 67 
 68         /// <summary>
 69         /// gets or sets the twinkle speed.
 70         /// </summary>
 71         /// <value>the twinkle speed.</value>
 72         [description("闪烁间隔时间(毫秒),当为0时不闪烁"), category("自定义")]
 73         public int twinklespeed
 74         {
 75             get { return twinklespeed; }
 76             set
 77             {
 78                 if (value < 0)
 79                     return;
 80                 twinklespeed = value;
 81                 if (value == 0 || lampcolor.length <= 1)
 82                 {
 83                     timer.enabled = false;
 84                 }
 85                 else
 86                 {
 87                     intcolorindex = 0;
 88                     timer.interval = value;
 89                     timer.enabled = true;
 90                 }
 91                 refresh();
 92             }
 93         }
 94         /// <summary>
 95         /// the timer
 96         /// </summary>
 97         timer timer;
 98         /// <summary>
 99         /// the int color index
100         /// </summary>
101         int intcolorindex = 0;

重绘

 1  protected override void onpaint(painteventargs e)
 2         {
 3             base.onpaint(e);
 4             var g = e.graphics;
 5             g.setgdihigh();
 6             color c1 = lampcolor[intcolorindex];
 7             g.fillellipse(new solidbrush(c1), this.clientrectangle);
 8 
 9             if (ishighlight)
10             {
11                 graphicspath gp = new graphicspath();
12 
13                 rectangle rec = new rectangle(5, 5, this.width - 10, this.height - 10);
14                 gp.addellipse(rec);
15 
16                 color[] surroundcolor = new color[] { c1 };
17                 pathgradientbrush pb = new pathgradientbrush(gp);
18                 pb.centercolor = color.white;
19                 pb.surroundcolors = surroundcolor;
20                 g.fillpath(pb, gp);
21             }
22 
23             if (isshowborder)
24             {
25                 g.drawellipse(new pen(new solidbrush(this.backcolor), 2), new rectangle(4, 4, this.width - 8, this.height - 8));
26             }
27         }

全部代码

  1 // ***********************************************************************
2 // assembly         : hzh_controls
3 // created          : 2019-09-09
4 //
5 // ***********************************************************************
6 // <copyright file="ucsignallamp.cs">
7 //     copyright by huang zhenghui(黄正辉) all, qq group:568015492 qq:623128629 email:623128629@qq.com
8 // </copyright>
9 //
10 // blog: https://www.cnblogs.com/bfyx
11 // github:https://github.com/kwwwvagaa/netwinformcontrol
12 // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
13 //
14 // if you use this code, please keep this note.
15 // ***********************************************************************
16 using system;
17 using system.collections.generic;
18 using system.linq;
19 using system.text;
20 using system.windows.forms;
21 using system.drawing;
22 using system.drawing.drawing2d;
23 using system.componentmodel;
24 
25 namespace hzh_controls.controls.factorycontrols.lamp
26 {
27     /// <summary>
28     /// class ucsignallamp.
29     /// implements the <see cref="system.windows.forms.usercontrol" />
30     /// </summary>
31     /// <seealso cref="system.windows.forms.usercontrol" />
32     public class ucsignallamp : usercontrol
33     {
34         /// <summary>
35         /// the is show border
36         /// </summary>
37         private bool isshowborder = false;
38 
39         /// <summary>
40         /// gets or sets a value indicating whether this instance is show border.
41         /// </summary>
42         /// <value><c>true</c> if this instance is show border; otherwise, <c>false</c>.</value>
43         [description("是否显示边框"), category("自定义")]
44         public bool isshowborder
45         {
46             get { return isshowborder; }
47             set
48             {
49                 isshowborder = value;
50                 refresh();
51             }
52         }
53 
54         /// <summary>
55         /// the lamp color
56         /// </summary>
57         private color[] lampcolor = new color[] { color.fromargb(255, 77, 59) };
58 
59         /// <summary>
60         /// gets or sets the color of the lamp.
61         /// </summary>
62         /// <value>the color of the lamp.</value>
63         [description("灯颜色,当需要闪烁时,至少需要2个及以上颜色,不需要闪烁则至少需要1个颜色"), category("自定义")]
64         public color[] lampcolor
65         {
66             get { return lampcolor; }
67             set
68             {
69                 if (value == null || value.length <= 0)
70                     return;
71                 lampcolor = value;
72                 refresh();
73             }
74         }
75 
76         /// <summary>
77         /// the is highlight
78         /// </summary>
79         private bool ishighlight = true;
80 
81         /// <summary>
82         /// gets or sets a value indicating whether this instance is highlight.
83         /// </summary>
84         /// <value><c>true</c> if this instance is highlight; otherwise, <c>false</c>.</value>
85         [description("是否高亮显示"), category("自定义")]
86         public bool ishighlight
87         {
88             get { return ishighlight; }
89             set
90             {
91                 ishighlight = value;
92                 refresh();
93             }
94         }
95 
96         /// <summary>
97         /// the twinkle speed
98         /// </summary>
99         private int twinklespeed = 0;
100 
101         /// <summary>
102         /// gets or sets the twinkle speed.
103         /// </summary>
104         /// <value>the twinkle speed.</value>
105         [description("闪烁间隔时间(毫秒),当为0时不闪烁"), category("自定义")]
106         public int twinklespeed
107         {
108             get { return twinklespeed; }
109             set
110             {
111                 if (value < 0)
112                     return;
113                 twinklespeed = value;
114                 if (value == 0 || lampcolor.length <= 1)
115                 {
116                     timer.enabled = false;
117                 }
118                 else
119                 {
120                     intcolorindex = 0;
121                     timer.interval = value;
122                     timer.enabled = true;
123                 }
124                 refresh();
125             }
126         }
127         /// <summary>
128         /// the timer
129         /// </summary>
130         timer timer;
131         /// <summary>
132         /// the int color index
133         /// </summary>
134         int intcolorindex = 0;
135         /// <summary>
136         /// initializes a new instance of the <see cref="ucsignallamp"/> class.
137         /// </summary>
138         public ucsignallamp()
139         {
140             this.setstyle(controlstyles.allpaintinginwmpaint, true);
141             this.setstyle(controlstyles.doublebuffer, true);
142             this.setstyle(controlstyles.resizeredraw, true);
143             this.setstyle(controlstyles.selectable, true);
144             this.setstyle(controlstyles.supportstransparentbackcolor, true);
145             this.setstyle(controlstyles.userpaint, true);
146             this.autoscalemode = system.windows.forms.autoscalemode.none;
147             this.size = new size(50, 50);
148             this.sizechanged += ucsignallamp_sizechanged;
149             timer = new timer();
150             timer.interval = 200;
151             timer.tick += timer_tick;
152         }
153 
154         /// <summary>
155         /// handles the tick event of the timer control.
156         /// </summary>
157         /// <param name="sender">the source of the event.</param>
158         /// <param name="e">the <see cref="eventargs"/> instance containing the event data.</param>
159         void timer_tick(object sender, eventargs e)
160         {
161             intcolorindex++;
162             if (intcolorindex >= lampcolor.length)
163                 intcolorindex = 0;
164             refresh();
165         }
166         /// <summary>
167         /// handles the sizechanged event of the ucsignallamp control.
168         /// </summary>
169         /// <param name="sender">the source of the event.</param>
170         /// <param name="e">the <see cref="eventargs"/> instance containing the event data.</param>
171         void ucsignallamp_sizechanged(object sender, eventargs e)
172         {
173             var maxsize = math.min(this.width, this.height);
174             if (this.width != maxsize)
175                 this.width = maxsize;
176             if (this.height != maxsize)
177                 this.height = maxsize;
178         }
179 
180         /// <summary>
181         /// 引发 <see cref="e:system.windows.forms.control.paint" /> 事件。
182         /// </summary>
183         /// <param name="e">包含事件数据的 <see cref="t:system.windows.forms.painteventargs" />。</param>
184         protected override void onpaint(painteventargs e)
185         {
186             base.onpaint(e);
187             var g = e.graphics;
188             g.setgdihigh();
189             color c1 = lampcolor[intcolorindex];
190             g.fillellipse(new solidbrush(c1), this.clientrectangle);
191 
192             if (ishighlight)
193             {
194                 graphicspath gp = new graphicspath();
195 
196                 rectangle rec = new rectangle(5, 5, this.width - 10, this.height - 10);
197                 gp.addellipse(rec);
198 
199                 color[] surroundcolor = new color[] { c1 };
200                 pathgradientbrush pb = new pathgradientbrush(gp);
201                 pb.centercolor = color.white;
202                 pb.surroundcolors = surroundcolor;
203                 g.fillpath(pb, gp);
204             }
205 
206             if (isshowborder)
207             {
208                 g.drawellipse(new pen(new solidbrush(this.backcolor), 2), new rectangle(4, 4, this.width - 8, this.height - 8));
209             }
210         }
211     }
212 }

 

最后的话

如果你喜欢的话,请到  点个星星吧