官网

http://www.hzhcontrols.com

前提

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

添加属性

 /// <summary>
/// The is show border
/// </summary>
private bool isShowBorder = false; /// <summary>
/// Gets or sets a value indicating whether this instance is show border.
/// </summary>
/// <value><c>true</c> if this instance is show border; otherwise, <c>false</c>.</value>
[Description("是否显示边框"), Category("自定义")]
public bool IsShowBorder
{
get { return isShowBorder; }
set
{
isShowBorder = value;
Refresh();
}
} /// <summary>
/// The lamp color
/// </summary>
private Color[] lampColor = new Color[] { Color.FromArgb(, , ) }; /// <summary>
/// Gets or sets the color of the lamp.
/// </summary>
/// <value>The color of the lamp.</value>
[Description("灯颜色,当需要闪烁时,至少需要2个及以上颜色,不需要闪烁则至少需要1个颜色"), Category("自定义")]
public Color[] LampColor
{
get { return lampColor; }
set
{
if (value == null || value.Length <= )
return;
lampColor = value;
Refresh();
}
} /// <summary>
/// The is highlight
/// </summary>
private bool isHighlight = true; /// <summary>
/// Gets or sets a value indicating whether this instance is highlight.
/// </summary>
/// <value><c>true</c> if this instance is highlight; otherwise, <c>false</c>.</value>
[Description("是否高亮显示"), Category("自定义")]
public bool IsHighlight
{
get { return isHighlight; }
set
{
isHighlight = value;
Refresh();
}
} /// <summary>
/// The twinkle speed
/// </summary>
private int twinkleSpeed = ; /// <summary>
/// Gets or sets the twinkle speed.
/// </summary>
/// <value>The twinkle speed.</value>
[Description("闪烁间隔时间(毫秒),当为0时不闪烁"), Category("自定义")]
public int TwinkleSpeed
{
get { return twinkleSpeed; }
set
{
if (value < )
return;
twinkleSpeed = value;
if (value == || lampColor.Length <= )
{
timer.Enabled = false;
}
else
{
intColorIndex = ;
timer.Interval = value;
timer.Enabled = true;
}
Refresh();
}
}
/// <summary>
/// The timer
/// </summary>
Timer timer;
/// <summary>
/// The int color index
/// </summary>
int intColorIndex = ;

重绘

  protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SetGDIHigh();
Color c1 = lampColor[intColorIndex];
g.FillEllipse(new SolidBrush(c1), this.ClientRectangle); if (isHighlight)
{
GraphicsPath gp = new GraphicsPath(); Rectangle rec = new Rectangle(, , this.Width - , this.Height - );
gp.AddEllipse(rec); Color[] surroundColor = new Color[] { c1 };
PathGradientBrush pb = new PathGradientBrush(gp);
pb.CenterColor = Color.White;
pb.SurroundColors = surroundColor;
g.FillPath(pb, gp);
} if (isShowBorder)
{
g.DrawEllipse(new Pen(new SolidBrush(this.BackColor), ), new Rectangle(, , this.Width - , this.Height - ));
}
}

全部代码

 // ***********************************************************************
// Assembly : HZH_Controls
// Created : 2019-09-09
//
// ***********************************************************************
// <copyright file="UCSignalLamp.cs">
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
// </copyright>
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel; namespace HZH_Controls.Controls.FactoryControls.Lamp
{
/// <summary>
/// Class UCSignalLamp.
/// Implements the <see cref="System.Windows.Forms.UserControl" />
/// </summary>
/// <seealso cref="System.Windows.Forms.UserControl" />
public class UCSignalLamp : UserControl
{
/// <summary>
/// The is show border
/// </summary>
private bool isShowBorder = false; /// <summary>
/// Gets or sets a value indicating whether this instance is show border.
/// </summary>
/// <value><c>true</c> if this instance is show border; otherwise, <c>false</c>.</value>
[Description("是否显示边框"), Category("自定义")]
public bool IsShowBorder
{
get { return isShowBorder; }
set
{
isShowBorder = value;
Refresh();
}
} /// <summary>
/// The lamp color
/// </summary>
private Color[] lampColor = new Color[] { Color.FromArgb(, , ) }; /// <summary>
/// Gets or sets the color of the lamp.
/// </summary>
/// <value>The color of the lamp.</value>
[Description("灯颜色,当需要闪烁时,至少需要2个及以上颜色,不需要闪烁则至少需要1个颜色"), Category("自定义")]
public Color[] LampColor
{
get { return lampColor; }
set
{
if (value == null || value.Length <= )
return;
lampColor = value;
Refresh();
}
} /// <summary>
/// The is highlight
/// </summary>
private bool isHighlight = true; /// <summary>
/// Gets or sets a value indicating whether this instance is highlight.
/// </summary>
/// <value><c>true</c> if this instance is highlight; otherwise, <c>false</c>.</value>
[Description("是否高亮显示"), Category("自定义")]
public bool IsHighlight
{
get { return isHighlight; }
set
{
isHighlight = value;
Refresh();
}
} /// <summary>
/// The twinkle speed
/// </summary>
private int twinkleSpeed = ; /// <summary>
/// Gets or sets the twinkle speed.
/// </summary>
/// <value>The twinkle speed.</value>
[Description("闪烁间隔时间(毫秒),当为0时不闪烁"), Category("自定义")]
public int TwinkleSpeed
{
get { return twinkleSpeed; }
set
{
if (value < )
return;
twinkleSpeed = value;
if (value == || lampColor.Length <= )
{
timer.Enabled = false;
}
else
{
intColorIndex = ;
timer.Interval = value;
timer.Enabled = true;
}
Refresh();
}
}
/// <summary>
/// The timer
/// </summary>
Timer timer;
/// <summary>
/// The int color index
/// </summary>
int intColorIndex = ;
/// <summary>
/// Initializes a new instance of the <see cref="UCSignalLamp"/> class.
/// </summary>
public UCSignalLamp()
{
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);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.Size = new Size(, );
this.SizeChanged += UCSignalLamp_SizeChanged;
timer = new Timer();
timer.Interval = ;
timer.Tick += timer_Tick;
} /// <summary>
/// Handles the Tick event of the timer control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
void timer_Tick(object sender, EventArgs e)
{
intColorIndex++;
if (intColorIndex >= lampColor.Length)
intColorIndex = ;
Refresh();
}
/// <summary>
/// Handles the SizeChanged event of the UCSignalLamp control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
void UCSignalLamp_SizeChanged(object sender, EventArgs e)
{
var maxSize = Math.Min(this.Width, this.Height);
if (this.Width != maxSize)
this.Width = maxSize;
if (this.Height != maxSize)
this.Height = maxSize;
} /// <summary>
/// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
/// </summary>
/// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SetGDIHigh();
Color c1 = lampColor[intColorIndex];
g.FillEllipse(new SolidBrush(c1), this.ClientRectangle); if (isHighlight)
{
GraphicsPath gp = new GraphicsPath(); Rectangle rec = new Rectangle(, , this.Width - , this.Height - );
gp.AddEllipse(rec); Color[] surroundColor = new Color[] { c1 };
PathGradientBrush pb = new PathGradientBrush(gp);
pb.CenterColor = Color.White;
pb.SurroundColors = surroundColor;
g.FillPath(pb, gp);
} if (isShowBorder)
{
g.DrawEllipse(new Pen(new SolidBrush(this.BackColor), ), new Rectangle(, , this.Width - , this.Height - ));
}
}
}
}

最后的话

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

(六十一)c#Winform自定义控件-信号灯(工业)-HZHControls的更多相关文章

  1. (十六)c#Winform自定义控件-文本框哪里去了?-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  2. (四十六)c#Winform自定义控件-水波进度条-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  3. (二十六)c#Winform自定义控件-有确定取消的窗体(二)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  4. (八十六)c#Winform自定义控件-表格优化

    出处:http://www.hzhcontrols.com/原文:http://www.hzhcontrols.com/blog-149.html本文版权归www.hzhcontrols.com所有欢 ...

  5. (五十六)c#Winform自定义控件-瓶子(工业)-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  6. (八十一)c#Winform自定义控件-时间轴-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  7. (三十六)c#Winform自定义控件-步骤控件-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  8. (五十一)c#Winform自定义控件-文字提示-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  9. (六)c#Winform自定义控件-单选框

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

随机推荐

  1. 异常日志文件errorlong

    #region log ////////////////////use/////////////// /// <summary> /// 异常日志 /// </summary> ...

  2. 《Java基础知识》Java变量的声明、初始化和作用域

    一.Java变量的声明 在 Java 程序设计中,每个声明的变量都必须分配一个类型.声明一个变量时,应该先声明变量的类型,随后再声明变量的名字.下面演示了变量的声明方式. double salary; ...

  3. LeetCode刷题总结-二分查找和贪心法篇

    本文介绍LeetCode上有关二分查找和贪心法的算法题,推荐刷题总数为16道.具体考点归纳如下: 一.二分查找 1.数学问题 题号:29. 两数相除,难度中等 题号:668. 乘法表中第k小的数,难度 ...

  4. AI: Web: 2 Vulnhub Walkthrough

    靶机下载链接: https://www.vulnhub.com/entry/ai-web-2,357 主机端口扫描: 尝试SQL注入,未发现有注入漏洞,就注册创建于一账户 http://10.10.2 ...

  5. Android 共享参数 SharedPreferences

    完成共享参数的读写 public class SharedPreference { private Context context; public SharedPreference(Context c ...

  6. JVM调优之服务内存超过阈值报警

    今早收到一条短信,具体报警信息如下: [UMP JVM监控内存报警]应用名:发券worker(jdos_couponwkr);KEY[coupon.send.worker.jvm],主机名:[host ...

  7. STL常用结构与方法简明总结

    C++常用的数据结构 序列式容器 vector(向量.有序数列),list(双向链表),deque(双端队列) 适配器容器 stack(栈),queue(队列) 关联式容器 map(映射.键值对二叉树 ...

  8. springboot使用hibernate validator

    前言 在开发中经常需要写一些字段校验的代码,比如字段非空,字段长度限制,邮箱格式验证等等,写这些与业务逻辑关系不大的代码个人感觉有两个麻烦: 验证代码繁琐,重复劳动 方法内代码显得冗长 每次要看哪些参 ...

  9. vue element之axios下载文件(后端Python)

    axios 接受文件流,需要设置 {responseType:'arraybuffer'} axios.post( apiUrl, formdata, {responseType:'arraybuff ...

  10. 「SAP技术」如何看Z移动类型是复制哪个标准移动类型而创建的?

    [SAP技术]SAP MM 如何看一个自定义移动类型是复制哪个标准移动类型而创建的? 比如项目上有一个自定义移动类型Z59,是复制551移动类型而定义的. OMJJ配置界面里,是有一个Ref字段.如下 ...