Creating and Using A Dynamic LED Control

Environment: Compiled using VC6.0 Sp3 and tested using Win95/98 WinNT4.0 and Win 2000


I saw the article "Static LED Control" by Monte Variakojis at and I had a look at the questions asked there. This class enables the user to have a blinking effect with control over the blinking rate per second. They can set the timer of the control. Sometimes, it would be better to show the status of an ongoing operation or if there is any warning; it would be better to display it using an LED control. I faced this situation when developing for a client whose end users were security guards who had no knowledge of computers. So, the interface for them was designed like a TV remote control and I had to incorporate a lot of controls like this to make them feel at ease. I would like to tell the people who are going to use this class that it's best suited for small controls rather than making it bigger where the effect is lost. It looks more like an ellipse than a LED at bigger proportions.


To use the CDynamicLED class, you can follow the guidelines stated below.

  1. Insert a new static frame where you want the LED. Setting the client edge property or the modal frame property for it looks better.
  2. Rename the static frame to something other than IDC_STATIC. Name it something like IDC_DYN_LED.
  3. Using the MFC ClassWizard, add a new member variable for IDC__DYN_LED. The category should be a control and the Variable Type should be CDynamicLED. If the CDynamicLED type does not show up in the Variable type dropdown combo, you need to re-create the .clw file. Delete the .clw file and run the class wizard again.
  4. Remember to add "DynamicLED.h" to your dialog's header file.


The various features in the CDynamicLED Class are outlined below.

  1. Use the following command:
    SetLED( CWnd *pWnd, 
            UINT nIDColor,
            UINT nIDShape, 
            int nTimerInterval)
    where pWnd is your static window where you want the LED. nIDColor can be any of the following values:
    NIDShape can be any of the following values:

    Here, the nIDShape value determines whether the shape of the LED will be round or square.

    And the nTimerInterval parameter denotes the number of milliseconds. The LED would flash once in every period denoted by this parameter. You can either have a rapidly blinking LED by setting this parameter to 100 or have a normally blinking LED which blinks once per second by setting this value to 1000.

    This is the ONLY function that you need to know to use this class.
  2. In case you need more functionality to switch on or switch off the LED, you have two functions named:

    These two functions don't need any parameters.

Now, let's go to the implementation of this control in your dialog-based application. I have assumed that you have named your dialog class CMyDialog.

Remember that you have created a variable for this static frame. If you have forgotten about that, please refer to the implementation section above. Assuming that you have named your variable m_dynLEDRed for a LED control which is round in shape, it is going to blink once every half second and which is red in colour.

You have to add the following lines in your OnInitDialog function. I have also assumed that you have named your static frame IDC_STATIC_LED_RED.

CWnd *pWndRed = (CWnd *)GetDlgItem(IDC_STATIC_LED_RED);

In case you want to change the blinking interval of the LED at runtime from half a second to a full second, you can use the following code.

// Change the time interval of the LED to 1000
CWnd *pWndRed = (CWnd *)GetDlgItem(IDC_STATIC_LED_RED);

To change the shape of the LED from round to square or vice versa , you can follow this piece of code.

// Change the shape of the Blue LED from round to square
CWnd *pWndRed  = (CWnd *)GetDlgItem(IDC_STATIC_LED_RED);

If you want to turn off the LED (I mean switching it off), you can use this.

// Switch OFF the Red LED
CWnd *pWndRed = (CWnd *)GetDlgItem(IDC_STATIC_LED_RED);

and to switch it on again, use

// Switch ON the Red LED
CWnd *pWndRed = (CWnd *)GetDlgItem(IDC_STATIC_LED_RED);

That's all, folks. All luck and have a great time.

With warm regards,


Download demo project - 48KB
Download source - 4KB


  • Nice LED but must it blink?

    Posted by Legacy on 05/05/2003 12:00am

    Originally posted by: George Fredal

    I like your LED but would like to eliminate the TIMER and display it in a constate BRIGHT state. Can you suggest how I might accomplish this?
    Thanks in advance for any advice.
    Regards George

  • I'll update the code asap

    Posted by Legacy on 08/10/2002 12:00am

    Originally posted by: VGirish

    Dear All,

    I'm terribly sorry about the code that has been posted. It DOES contain resource leaks. I had posted the WRONG code and i noticed it the moment i saw dterr's comments.

    The code that has been posted contains some more typos like i select the red led but i have used the blue led's variable name instead !!!!!

    Infact, i had used Feng Yuan's GDI resource leak detector on my final version and checked if there are any leaks. But i made a mistake of posting the wrong version in a haste. :(

    I'm terribly sorry and please accept my apologies.


    p.s. Incase you want to use this code before i fix up the bugs, then all that you have to do is to store the state of the previous brushes and pens and restore it after you have completed the drawing.

    With warm regards,

  • Why so much redundant code?

    Posted by Legacy on 08/09/2002 12:00am

    Originally posted by: Joe Sonderegger

    Most of the code seems to be redundant.
    Why is this?

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 16, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you starting an on-premise-to-cloud data migration project? Have you thought about how much space you might need for your online platform or how to handle data that might be related to users who no longer exist? If these questions or any other concerns have been plaguing you about your migration project, check out this eSeminar. Join our speakers Betsy Bilhorn, VP, Product Management at Scribe, Mike Virnig, PowerSucess Manager and Michele …

  • Best-in-Class organizations execute on a strategy that supports the multi-channel nature of customer requests. These leading organizations do not just open up their service infrastructures to accommodate new channels, but also empower their teams to deliver an effective and consistent experience regardless of the channel selected by the customer. This document will highlight the key business capabilities that support a Best-in-Class customer engagement strategy.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds