(六十一)c#Winform自定义控件-信号灯(工业)

(六十一)c#Winform自定义控件-信号灯(工业),第1张

概述前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git 如果觉得写的还行,请点个 star 支持一下吧 欢迎前来交流探讨: 企鹅群568015 前提

入行已经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

用处及效果

准备工作

依然使用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 index100         /// </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:[email protected]  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,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 TwinkleSpeed107         {108             get { return twinkleSpeed; }109             set110             {111                 if (value < 0)112                     return;113                 twinkleSpeed = value;114                 if (value == 0 || lampcolor.Length <= 1)115                 {116                     timer.Enabled = false;117                 }118                 else119                 {120                     intcolorIndex = 0;121                     timer.Interval = value;122                     timer.Enabled = true;123                 }124                 Refresh();125             }126         }127         /// <summary>128         /// The timer129         /// </summary>130         Timer timer;131         /// <summary>132         /// The int color index133         /// </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,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),this.Height - 8));209             }210         }211     }212 }
VIEw Code

 

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧

总结

以上是内存溢出为你收集整理的(六十一)c#Winform自定义控件-信号灯工业)全部内容,希望文章能够帮你解决(六十一)c#Winform自定义控件-信号灯(工业)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1214638.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存