Click here to Skip to main content
15,880,392 members
Articles / Desktop Programming / Windows Forms

A Simple Vector-Based LED User Control

Rate me:
Please Sign up or sign in to vote.
4.95/5 (86 votes)
5 Apr 2013CPOL2 min read 176.6K   17.7K   215   50
The LEDBulb is a .NET user control for Windows Forms that emulates an LED light. Its purpose is to provide a sleek looking representation of an LED light that is sizable, has a transparent background and can be set to different colors.
LEDBulb/sshot-1.png

Introduction

The LEDBulb is a .NET user control for Windows Forms that emulates an LED light with two states On and Off. The purpose of the control is to provide a sleek looking representation of an LED light that is sizable, has a transparent background and can be set to different colors.

C#
LedBulb bulb = new LedBulb();
bulb.Size = new Size(25, 25);
bulb.Color = Color.LawnGreen;
bulb.On = true;
this.Controls.Add(bulb); 

Sizeable

I recently needed to add a custom user control to a Window Forms project of mine that would represent an LED bulb commonly seen on household electronics. After a quick search, most of the existing controls I found were ugly and used static images, which means they wouldn’t scale well. This control uses the System.Drawing.Drawing2D namespace to draw a vector image so that it not only looks clean but will also scale to any size without affecting the image quality.

LEDBulb/sshot-3.png

Rendering the control starts with drawing a solid circle with our background color. Then we draw a radial gradient over it with our highlight color, transitioning to transparent. To draw the reflection, we draw another radial gradient, from white to transparent, but in a smaller rectangle shifted up and left. To prevent the white gradient from showing up outside the bulb, we set the clip parameter to the bounds of our original circle.

C#
// Fill in the background circle 
g.FillEllipse(new SolidBrush(darkColor), drawRectangle);

// Draw the glow gradient
GraphicsPath path = new GraphicsPath();
path.AddEllipse(drawRectangle);
PathGradientBrush pathBrush = new PathGradientBrush(path);
pathBrush.CenterColor = lightColor;
pathBrush.SurroundColors = new Color[] { Color.FromArgb(0, lightColor) };
g.FillEllipse(pathBrush, drawRectangle);

// Set the clip boundary to the edge of the ellipse
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(drawRectangle);
g.SetClip(gp);

// Draw the white reflection gradient
GraphicsPath path1 = new GraphicsPath();
path1.AddEllipse(whiteRectangle); // a smaller rectangle set to the top left
PathGradientBrush pathBrush1 = new PathGradientBrush(path);
pathBrush1.CenterColor = Color.FromArgb(180, 255, 255, 255);
pathBrush1.SurroundColors = new Color[] { Color.FromArgb(0, 255, 255, 255) };
g.FillEllipse(pathBrush1, whiteRect);

Transparent Background

It is important that this control has a transparent background. To do this, we need to add a command to the constructor.

C#
SetStyle(ControlStyles.DoubleBuffer
    | ControlStyles.AllPaintingInWmPaint
    | ControlStyles.ResizeRedraw
    | ControlStyles.UserPaint
    | ControlStyles.SupportsTransparentBackColor, true
); 

Customizable Colors

You can customize the color of the LED display by setting the Color property. Setting this property also automatically calculates two other Properties ColorDark and ColorDarkDark which are used for gradients when the control is drawn. These colors are calculated using the using the ControlPaint class, a Control helper class in the System.Windows.Forms namespace.

C#
this.DarkColor = ControlPaint.Dark(_this.Color);
this.DarkDarkColor = ControlPaint.DarkDark(_this.Color); 
LEDBulb/sshot-6.png

LEDBulb/sshot-5.png

Blink

The control has a blink method that will make the control start blinking. You pass the number of milliseconds between blinks. To turn the blinking off just call the blink function again passing 0 as the argument.

C#
led.Blink(2000); // Slow blink
led.Blink(500);  // Fast blink
led.Blink(0);    // Turn off blinking

Points of Interest

This control uses double-buffering by drawing its image to an off-screen bitmap first, then sending the final rendering to the client. Double buffering creates a smoother drawing of the control and avoids flicker when moving or re-sizing. The code to accomplish this is below.

C#
protected override void OnPaint(PaintEventArgs e){
    // Create an offscreen graphics object for double buffering
    Bitmap offScreenBmp = new Bitmap(this.ClientRectangle.Width, 
                this.ClientRectangle.Height);
    System.Drawing.Graphics g = Graphics.FromImage(offScreenBmp);
    g.SmoothingMode = SmoothingMode.HighQuality;

    // Render the control to the off-screen bitmap
    drawControl(g);

    // Draw the image to the screen
    e.Graphics.DrawImageUnscaled(offScreenBmp, 0, 0);    
}  

Links

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Stephen Marsh has over 10 years of experience developing enterprise applications built on the .Net framework. He specializes in building expert systems that serve the financial industry.

Comments and Discussions

 
QuestionOff Color Pin
Boresh Kar31-Mar-24 15:20
Boresh Kar31-Mar-24 15:20 
QuestionAdditional Property would be nice Pin
S Macha15-Apr-21 8:56
S Macha15-Apr-21 8:56 
QuestionHow to use as a dll in other project. Pin
Member 1250021526-Jun-19 6:33
Member 1250021526-Jun-19 6:33 
AnswerRe: How to use as a dll in other project. Pin
C T 202124-Sep-22 12:27
C T 202124-Sep-22 12:27 
QuestionLink to DLL update missing Pin
Member 43099320-May-19 7:39
Member 43099320-May-19 7:39 
QuestionLED Square Pin
smokehouse_joe2-Oct-18 12:50
smokehouse_joe2-Oct-18 12:50 
SuggestionInclude an Importable User Control .DLL Pin
Protocol Droid13-Sep-17 10:38
Protocol Droid13-Sep-17 10:38 
GeneralRe: Include an Importable User Control .DLL Pin
snah19694-Dec-17 22:50
snah19694-Dec-17 22:50 
GeneralRe: Include an Importable User Control .DLL Pin
Protocol Droid5-Dec-17 5:40
Protocol Droid5-Dec-17 5:40 
GeneralRe: Include an Importable User Control .DLL Pin
snah19695-Dec-17 11:16
snah19695-Dec-17 11:16 
QuestionVB.NET Project Pin
Geraluca7-Jun-17 3:16
Geraluca7-Jun-17 3:16 
AnswerRe: VB.NET Project Pin
Protocol Droid5-Dec-17 12:16
Protocol Droid5-Dec-17 12:16 
QuestionIt's just what I'm looking for. Pin
Member 1306344521-Mar-17 14:46
Member 1306344521-Mar-17 14:46 
AnswerRe: It's just what I'm looking for. Pin
ElektroStudios25-Mar-17 5:50
ElektroStudios25-Mar-17 5:50 
GeneralRe: It's just what I'm looking for. Pin
S Macha15-Apr-21 16:30
S Macha15-Apr-21 16:30 
QuestionHow can I use this? Pin
Member 128375247-Nov-16 9:04
Member 128375247-Nov-16 9:04 
QuestionUsing this in a WPF Project Pin
Member 127212505-Sep-16 7:40
Member 127212505-Sep-16 7:40 
AnswerRe: Using this in a WPF Project Pin
Da_Hero26-Oct-16 2:24
professionalDa_Hero26-Oct-16 2:24 
AnswerRe: Using this in a WPF Project Pin
Linxzhang6-Sep-19 21:59
Linxzhang6-Sep-19 21:59 
GeneralMy vote of 5 Pin
backupluis31-Jan-16 17:13
backupluis31-Jan-16 17:13 
QuestionGreat control! What a pity! The source is unavailable. Pin
wwx395331-Jul-15 14:07
wwx395331-Jul-15 14:07 
Would somebody kindly send me it?thanks.
QuestionResources are not disposed Pin
getthecodeforme23-Jan-15 13:50
getthecodeforme23-Jan-15 13:50 
AnswerRe: Resources are not disposed Pin
TymekDP5-Feb-15 23:34
TymekDP5-Feb-15 23:34 
QuestionWhy doesn't the Bulb.ledBulb show up in the toolbox? Pin
blipton5-Nov-14 18:48
blipton5-Nov-14 18:48 
QuestionMy vote of 5 Pin
chen.zd2-Apr-14 17:50
chen.zd2-Apr-14 17:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.