(六十二)c#Winform自定义控件-警灯(工业)
程序员文章站
2022-05-03 22:21:56
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_contr ......
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
github:https://github.com/kwwwvagaa/netwinformcontrol
码云:
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492
麻烦博客下方点个【推荐】,谢谢
nuget
install-package hzh_controls
目录
用处及效果
准备工作
依然gdi+,不懂可以先百度了解下
开始
添加一个类ucalarmlamp,继承自usercontrol
添加属性
1 /// <summary> 2 /// the lamp color 3 /// </summary> 4 private color[] lampcolor = new color[] { color.fromargb(255, 77, 59) }; 5 6 /// <summary> 7 /// gets or sets the color of the lamp. 8 /// </summary> 9 /// <value>the color of the lamp.</value> 10 [description("灯颜色,当需要闪烁时,至少需要2个及以上颜色,不需要闪烁则至少需要1个颜色"), category("自定义")] 11 public color[] lampcolor 12 { 13 get { return lampcolor; } 14 set 15 { 16 if (value == null || value.length <= 0) 17 return; 18 lampcolor = value; 19 refresh(); 20 } 21 } 22 23 /// <summary> 24 /// the lampstand 25 /// </summary> 26 private color lampstand = color.fromargb(105, 105, 105); 27 28 /// <summary> 29 /// gets or sets the lampstand. 30 /// </summary> 31 /// <value>the lampstand.</value> 32 [description("灯座颜色"), category("自定义")] 33 public color lampstand 34 { 35 get { return lampstand; } 36 set { lampstand = value; } 37 } 38 39 /// <summary> 40 /// the twinkle speed 41 /// </summary> 42 private int twinklespeed = 0; 43 44 /// <summary> 45 /// gets or sets the twinkle speed. 46 /// </summary> 47 /// <value>the twinkle speed.</value> 48 [description("闪烁间隔时间(毫秒),当为0时不闪烁"), category("自定义")] 49 public int twinklespeed 50 { 51 get { return twinklespeed; } 52 set 53 { 54 if (value < 0) 55 return; 56 twinklespeed = value; 57 if (value == 0 || lampcolor.length <= 1) 58 { 59 timer.enabled = false; 60 } 61 else 62 { 63 intcolorindex = 0; 64 timer.interval = value; 65 timer.enabled = true; 66 } 67 refresh(); 68 } 69 } 70 /// <summary> 71 /// the timer 72 /// </summary> 73 timer timer; 74 /// <summary> 75 /// the int color index 76 /// </summary> 77 int intcolorindex = 0; 78 /// <summary> 79 /// the m rect working 80 /// </summary> 81 rectangle m_rectworking;
重绘
1 protected override void onpaint(painteventargs e) 2 { 3 base.onpaint(e); 4 var g = e.graphics; 5 g.setgdihigh(); 6 7 color c1 = lampcolor[intcolorindex]; 8 graphicspath path = new graphicspath(); 9 path.addline(new point(m_rectworking.left, m_rectworking.bottom), new point(m_rectworking.left, m_rectworking.top + m_rectworking.width)); 10 path.addarc(new rectangle(m_rectworking.left, m_rectworking.top, m_rectworking.width, m_rectworking.width), 180f, 180f); 11 path.addline(new point(m_rectworking.right, m_rectworking.top + m_rectworking.width), new point(m_rectworking.right, m_rectworking.bottom)); 12 path.closeallfigures(); 13 g.fillpath(new solidbrush(c1), path); 14 15 g.fillrectangle(new solidbrush(lampstand), new rectangle(5, m_rectworking.bottom - 19, this.width - 10, 10)); 16 g.fillrectangle(new solidbrush(lampstand), new rectangle(0, m_rectworking.bottom - 10, this.width, 10)); 17 }
完整代码
1 // *********************************************************************** 2 // assembly : hzh_controls 3 // created : 2019-09-10 4 // 5 // *********************************************************************** 6 // <copyright file="ucalarmlamp.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 26 { 27 /// <summary> 28 /// class ucalarmlamp. 29 /// implements the <see cref="system.windows.forms.usercontrol" /> 30 /// </summary> 31 /// <seealso cref="system.windows.forms.usercontrol" /> 32 public class ucalarmlamp : usercontrol 33 { 34 /// <summary> 35 /// the lamp color 36 /// </summary> 37 private color[] lampcolor = new color[] { color.fromargb(255, 77, 59) }; 38 39 /// <summary> 40 /// gets or sets the color of the lamp. 41 /// </summary> 42 /// <value>the color of the lamp.</value> 43 [description("灯颜色,当需要闪烁时,至少需要2个及以上颜色,不需要闪烁则至少需要1个颜色"), category("自定义")] 44 public color[] lampcolor 45 { 46 get { return lampcolor; } 47 set 48 { 49 if (value == null || value.length <= 0) 50 return; 51 lampcolor = value; 52 refresh(); 53 } 54 } 55 56 /// <summary> 57 /// the lampstand 58 /// </summary> 59 private color lampstand = color.fromargb(105, 105, 105); 60 61 /// <summary> 62 /// gets or sets the lampstand. 63 /// </summary> 64 /// <value>the lampstand.</value> 65 [description("灯座颜色"), category("自定义")] 66 public color lampstand 67 { 68 get { return lampstand; } 69 set { lampstand = value; } 70 } 71 72 /// <summary> 73 /// the twinkle speed 74 /// </summary> 75 private int twinklespeed = 0; 76 77 /// <summary> 78 /// gets or sets the twinkle speed. 79 /// </summary> 80 /// <value>the twinkle speed.</value> 81 [description("闪烁间隔时间(毫秒),当为0时不闪烁"), category("自定义")] 82 public int twinklespeed 83 { 84 get { return twinklespeed; } 85 set 86 { 87 if (value < 0) 88 return; 89 twinklespeed = value; 90 if (value == 0 || lampcolor.length <= 1) 91 { 92 timer.enabled = false; 93 } 94 else 95 { 96 intcolorindex = 0; 97 timer.interval = value; 98 timer.enabled = true; 99 } 100 refresh(); 101 } 102 } 103 /// <summary> 104 /// the timer 105 /// </summary> 106 timer timer; 107 /// <summary> 108 /// the int color index 109 /// </summary> 110 int intcolorindex = 0; 111 /// <summary> 112 /// the m rect working 113 /// </summary> 114 rectangle m_rectworking; 115 /// <summary> 116 /// initializes a new instance of the <see cref="ucalarmlamp"/> class. 117 /// </summary> 118 public ucalarmlamp() 119 { 120 this.setstyle(controlstyles.allpaintinginwmpaint, true); 121 this.setstyle(controlstyles.doublebuffer, true); 122 this.setstyle(controlstyles.resizeredraw, true); 123 this.setstyle(controlstyles.selectable, true); 124 this.setstyle(controlstyles.supportstransparentbackcolor, true); 125 this.setstyle(controlstyles.userpaint, true); 126 this.autoscalemode = system.windows.forms.autoscalemode.none; 127 this.sizechanged += ucalarmlamp_sizechanged; 128 this.size = new size(50, 50); 129 timer = new timer(); 130 timer.interval = 200; 131 timer.tick += timer_tick; 132 } 133 134 /// <summary> 135 /// handles the sizechanged event of the ucalarmlamp control. 136 /// </summary> 137 /// <param name="sender">the source of the event.</param> 138 /// <param name="e">the <see cref="eventargs"/> instance containing the event data.</param> 139 void ucalarmlamp_sizechanged(object sender, eventargs e) 140 { 141 m_rectworking = new rectangle(10, 0, this.width - 20, this.height); 142 } 143 /// <summary> 144 /// handles the tick event of the timer control. 145 /// </summary> 146 /// <param name="sender">the source of the event.</param> 147 /// <param name="e">the <see cref="eventargs"/> instance containing the event data.</param> 148 void timer_tick(object sender, eventargs e) 149 { 150 intcolorindex++; 151 if (intcolorindex >= lampcolor.length) 152 intcolorindex = 0; 153 refresh(); 154 } 155 /// <summary> 156 /// 引发 <see cref="e:system.windows.forms.control.paint" /> 事件。 157 /// </summary> 158 /// <param name="e">包含事件数据的 <see cref="t:system.windows.forms.painteventargs" />。</param> 159 protected override void onpaint(painteventargs e) 160 { 161 base.onpaint(e); 162 var g = e.graphics; 163 g.setgdihigh(); 164 165 color c1 = lampcolor[intcolorindex]; 166 graphicspath path = new graphicspath(); 167 path.addline(new point(m_rectworking.left, m_rectworking.bottom), new point(m_rectworking.left, m_rectworking.top + m_rectworking.width)); 168 path.addarc(new rectangle(m_rectworking.left, m_rectworking.top, m_rectworking.width, m_rectworking.width), 180f, 180f); 169 path.addline(new point(m_rectworking.right, m_rectworking.top + m_rectworking.width), new point(m_rectworking.right, m_rectworking.bottom)); 170 path.closeallfigures(); 171 g.fillpath(new solidbrush(c1), path); 172 173 g.fillrectangle(new solidbrush(lampstand), new rectangle(5, m_rectworking.bottom - 19, this.width - 10, 10)); 174 g.fillrectangle(new solidbrush(lampstand), new rectangle(0, m_rectworking.bottom - 10, this.width, 10)); 175 } 176 } 177 }
最后的话
如果你喜欢的话,请到 点个星星吧