using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class MyLED : UserControl
{
public MyLED()
{
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);
#endregion
}
#region 【2】定义三个字段
private Graphics g;
private Pen p;
private SolidBrush sb;
#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 Color ledColor = Color.Green;
[Category("jason控件属性")]
[Description("LED指示灯演示")]
public Color LedColor
{
get { return ledColor; }
set
{
ledColor = value;
this.Invalidate();
}
}
#endregion
#region 【5】创建重绘的事件
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
g = e.Graphics;//获取画布
SetGraphics(g);//设置画布
//情况1:如果宽度大于高度(以高度为准)
if (this.Width > this.Height)
{
sb = new SolidBrush(ledColor);
Rectangle rec = new Rectangle(1, 1, this.Height - 2, this.Height - 2);//创建矩形
g.FillEllipse(sb, rec);//画圆
}
}
#endregion
}
}
//调用
private void button2_Click(object sender, EventArgs e)
{
// bool myLEDStatus = true;
// this.myLED1.LedColor = Color.LimeGreen;
this.myLED1.LedColor = Color.LightBlue;
}
C# LED控件
最新推荐文章于 2024-09-25 11:40:32 发布