123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace monitor_main_windows
- {
- public partial class LED : UserControl
- {
- protected Color m_color = Color.DimGray;
- public LED()
- {
- InitializeComponent();
- #region 【1】设置双缓冲等属性
- this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
- this.SetStyle(ControlStyles.DoubleBuffer, true);
- this.SetStyle(ControlStyles.ResizeRedraw, true);
- this.SetStyle(ControlStyles.Selectable, true);
- this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
- this.SetStyle(ControlStyles.UserPaint, true);
- timer.Enabled = true;
- timer.Interval = 500;
- timer.Tick += Timer_Tick;
- #endregion
- }
- private void LED_Load(object sender, EventArgs e)
- {
- }
- private void Timer_Tick(object sender, EventArgs e)
- {
- if(Statu==LED_Statu.Normal)
- {
- m_color = LedNormalColor;
- }
- if (Statu == LED_Statu.Error)
- {
- m_color = m_color == LampColor ? LedErrorColor : LampColor;
- }
- if (Statu == LED_Statu.Disconnected)
- {
- m_color = m_color == LampColor ? LedDisconnectedColor : LampColor;
- }
- this.Invalidate();
- }
- #region 【2】定义三个字段
- private SolidBrush sb;
- Timer timer = new Timer();
- #endregion
- #region 【3】添加一个设置Graphics的方法
- /*private void SetGraphics(Graphics g)
- {
- //设置画布的属性
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
- g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
- }*/
- #endregion
- #region 【4】根据实际控件分析的结果,创建属性
- private string m_label = "控件";
- [Category("text属性")]
- [Description("")]
- public string Label
- {
- get { return m_label; }
- set
- {
- m_label = value;
- Refresh();
- }
- }
- public enum Font_Location
- {
- Top=0,
- Bottom=1,
- Left=2,
- Right=3,
- }
- protected Font_Location m_font_location = Font_Location.Bottom;
- [Category("字体位置属性")]
- [Description("")]
- public Font_Location FontLocation
- {
- get { return m_font_location; }
- set
- {
- m_font_location = value;
- Refresh();
- }
- }
- protected bool font_horizontal = true;
- [Category("字体位置属性")]
- [Description("")]
- public bool FontHorizontal
- {
- get { return font_horizontal; }
- set
- {
- font_horizontal = value;
- Refresh();
- }
- }
- public enum LED_Statu
- {
- Normal=0,
- Error,
- Disconnected
- }
- protected LED_Statu m_led_statu = LED_Statu.Error;
- public LED_Statu Statu
- {
- get { return m_led_statu; }
- set
- {
- m_led_statu = value;
- Refresh();
- }
- }
- [Category("jason控件属性")]
- [Description("TRUE的时候LED指示灯颜色")]
- private Color m_ledNormalColor = Color.Green;
- public Color LedNormalColor
- {
- get { return m_ledNormalColor; }
- set
- {
- m_ledNormalColor = value;
- this.Invalidate();
- }
- }
- private Color m_ledErrorColor = Color.Red;
- [Category("jason控件属性")]
- [Description("False的时候LED指示灯颜色")]
- public Color LedErrorColor
- {
- get { return m_ledErrorColor; }
- set
- {
- m_ledErrorColor = value;
- this.Invalidate();
- }
- }
- private Color m_ledDisconnectedColor = Color.DimGray;
- [Category("jason控件属性")]
- [Description("False的时候LED指示灯颜色")]
- public Color LedDisconnectedColor
- {
- get { return m_ledDisconnectedColor; }
- set
- {
- m_ledDisconnectedColor = value;
- this.Invalidate();
- }
- }
- private Color m_lampColor = Color.Transparent;
- [Category("jason控件属性")]
- [Description("LED指示灯演示")]
- public Color LampColor
- {
- get { return m_lampColor; }
- set
- {
- m_lampColor = value;
- this.Invalidate();
- }
- }
- private bool isBorder = true;
- [Category("jason控件属性")]
- [Description("是否有边框")]
- public bool IsBorder
- {
- get { return isBorder; }
- set
- {
- isBorder = value;
- this.Invalidate();
- }
- }
- private int borderWidth = 5;
- [Category("jason控件属性")]
- [Description("圆环的宽度")]
- public int BorderWidth
- {
- get { return borderWidth; }
- set
- {
- borderWidth = value;
- this.Invalidate();
- }
- }
-
- private bool isHighLight = true;
- [Category("jason控件属性")]
- [Description("是否高亮")]
- public bool IsHighLight
- {
- get { return isHighLight; }
- set
- {
- isHighLight = value;
- this.Invalidate();
- }
- }
- private Color centerColor = Color.White;
- [Category("jason控件属性")]
- [Description("渐变中心的颜色")]
- public Color CenterColor
- {
- get { return centerColor; }
- set
- {
- centerColor = value;
- this.Invalidate();
- }
- }
- private bool isFlash = true;
- [Category("jason控件属性")]
- [Description("是否闪烁")]
- public bool IsFlash
- {
- get { return isFlash; }
- set
- {
- isFlash = value;
- this.Invalidate();
- }
- }
- private int flashInterval = 500;
- [Category("jason控件属性")]
- [Description("闪烁的频率")]
- public int FlashInterval
- {
- get { return flashInterval; }
- set
- {
- flashInterval = value;
- timer.Interval = flashInterval;//timer的时间间隔要放在这里
- this.Invalidate();
- }
- }
-
- #endregion
- #region 【5】创建重绘的事件
- protected override void OnPaint(PaintEventArgs e)
- {
- base.OnPaint(e);
- var g = e.Graphics;
- g.SmoothingMode = SmoothingMode.AntiAlias; //图片柔顺模式
- g.InterpolationMode = InterpolationMode.HighQualityBicubic;//高质量
- g.CompositingQuality = CompositingQuality.HighQuality;//
- g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
- //g.Clear(this.BackColor);
- System.Drawing.SizeF sizeFont = g.MeasureString(Label, this.Font);
- #region 1,画一个圆
- int bord = 2;
- int w1 = this.Width-2*bord ;
- int w2 = this.Height - (int)sizeFont.Height - 2*bord;
- int LEDWidth = Math.Min(w1, w2);
- int cx = (this.Width - LEDWidth) / 2 + 1;
- int cy = 1;
- int txtX = (this.Width - (int)sizeFont.Width) / 2 + 1;
- int txtY = Height - (int)sizeFont.Height;
- if (FontHorizontal == false)
- {
- w1 = this.Width-2*bord;
- w2 = this.Height - (int)sizeFont.Width - bord;
- LEDWidth = Math.Min(w1, w2);
- cx = (this.Width - LEDWidth) / 2 + 1;
- cy = (this.Height - (int)sizeFont.Width - bord - LEDWidth) / 2 + 1;
- txtX = (this.Width - (int)sizeFont.Height) / 2 + 1;
- txtY = Height- (int)sizeFont.Width;
- }
- if (FontLocation == Font_Location.Top)
- {
- if (FontHorizontal==true)
- {
- cy = (Height-(int)sizeFont.Height-bord)/2-LEDWidth/2+ (int)sizeFont.Height + bord;
- txtX = (this.Width - (int)sizeFont.Width) / 2 + 1;
- txtY = 1;
- }
- else
- {
- w1 = this.Width-2*bord;
- w2 = this.Height - (int)sizeFont.Width - 2*bord;
- LEDWidth = Math.Min(w1, w2);
- cx = (this.Width - LEDWidth) / 2 + 1;
- cy = (Height - (int)sizeFont.Width - bord) / 2 -LEDWidth/2+ (int)sizeFont.Width + bord;
- txtX = (this.Width - (int)sizeFont.Height) / 2 + 1;
- txtY = bord;
- }
- }
- if(FontLocation==Font_Location.Left)
- {
- //横向
- if (FontHorizontal==true)
- {
- w1 = Width - bord - (int)sizeFont.Width - bord;
- w2 = Height-2*bord ;
- LEDWidth = Math.Min(w1, w2);
- cx = bord + (int)sizeFont.Width ;
- cy = (Height - LEDWidth) / 2 + 1;
- txtX = 1;
- txtY = (Height - (int)sizeFont.Height) / 2 + 1;
- }
- else
- {
- //竖向
- w1 = Width - bord - (int)sizeFont.Height;
- w2 = Height-2*bord;
- LEDWidth = Math.Min(w1, w2);
- cx =(Width-(int)sizeFont.Height-bord)/2-LEDWidth/2+ (int)sizeFont.Height+bord;
- cy = (Height-LEDWidth)/2+1 ;
- txtX = 1;
- txtY = (Height - (int)sizeFont.Width) / 2 + 1;
- }
- }
- if (FontLocation == Font_Location.Right)
- {
-
- //横向
- if (FontHorizontal == true)
- {
- w1 = Width - (int)sizeFont.Width - bord;
- w2 = Height-2*bord;
- LEDWidth = Math.Min(w1, w2);
- cx = (Width - (int)sizeFont.Width-LEDWidth)/2+1;
- cy = (Height-LEDWidth)/2+1 ;
- txtX = Width - (int)sizeFont.Width;
- txtY = (Height - (int)sizeFont.Height) / 2 + 1;
- }
- else
- {
- //竖向
- w1 = Width - (int)sizeFont.Height - bord;
- w2 = Height-2*bord;
- LEDWidth = Math.Min(w1, w2);
- cx = (Width - (int)sizeFont.Height - LEDWidth) / 2 + 1;
- cy = (Height - LEDWidth) / 2 + 1;
- txtX = Width - (int)sizeFont.Height;
- txtY = (Height - (int)sizeFont.Width) / 2 + 1;
- }
- }
- if (LEDWidth <= 0 || cx < 0 || cy < 0)
- return;
- sb = new SolidBrush(m_color);
-
- RectangleF rec = new RectangleF(cx, cy, LEDWidth, LEDWidth);//创建矩形
- g.DrawEllipse(new Pen(m_color), rec);
- g.FillEllipse(sb, rec);//画圆
- #endregion
- #region 2,在圆里面画一个圆环
- //如果有边框,那就画一个圆环
- /* if (isBorder)//参数这里用字段或属性都可以,如果用属性,程序要都走一些判断的代码
- {
- p = new Pen(this.BackColor, borderWidth);//使用背景色
- //p = new Pen(Color.Red, borderWidth);
- float x = 1 + gapWidth + borderWidth * 0.5f;
- rec = new RectangleF(x+cx, x, LEDWidth - 2 * x, LEDWidth - 2 * x);
- g.DrawEllipse(p, rec);//画圆环
- }*/
- #endregion
- #region 3,渐变色绘制,是否高亮
- if (isHighLight)
- {
- GraphicsPath gp = new GraphicsPath();
- //float x = isBorder ? 1 + borderWidth : 1;//使用三元运算来判断,优化代码
- //rec = new RectangleF(cx-LEDWidth/2, cy-LEDWidth/2, LEDWidth , LEDWidth);
- gp.AddEllipse(rec);//把矩形添加到路径
- //渐变色画刷,高亮
- PathGradientBrush pgb = new PathGradientBrush(gp);//把路径传入
- Color[] surroundColor = new Color[] { m_color };
- pgb.CenterColor = this.centerColor;
- //设置有多少组颜色来渐变
- pgb.SurroundColors = surroundColor;
- g.FillPath(pgb, gp);
- }
- #endregion
-
- if (FontHorizontal == false)
- {
- g.DrawString(Label, Font, new SolidBrush(Color.DodgerBlue),
- new Point(txtX, txtY),new StringFormat(StringFormatFlags.DirectionVertical));
- }
- else
- {
- g.DrawString(Label, Font, new SolidBrush(Color.DodgerBlue),
- new Point(txtX, txtY));
- }
- }
- #endregion
- }
- }
|