Click here to Skip to main content
Click here to Skip to main content

LedButton Status Control (Owner-draw)

By , 17 Jan 2005
 

Sample Image

Table of Contents

Motivation

The motivation in writing this CLedButton control was having a LED control that is:

  • Read-Only LED control.
  • Capable of dealing with more than the traditional two-states.
  • Capable of signaling "activity" (to turn off automatically after a controllable timeout).
  • Capable to control the transition from one state to another. (The transition from one state into another to be externally controlled.)
  • Use LED icons loaded from resource files.
  • Flicker-free control.

Two years ago, I was looking for a static or button control here in CodeProject and in SourceForge that could meet my five requirements, but I failed to find one, so I decided to build one by myself.

I got the inspiration from the following controls as presented in this great site:

With this background, I started to tailor my own CLedButton control two years ago. From project to project, the code got enhanced and simplified, until it became mature and robust for being published here.

Environment used

The LED button was initially developed using VC 6.0 (Visual Studio C++ 6.0), and now I'm using VC 7.1 (Visual Studio C++ .NET 2003). I will "try" to provide instructions that are suitable for both versions.

Adding the LED button files and resources to your project

Make sure you got the following files:

  • The sources for this control: LedButton.h and LedButton.cpp, copy them to your project directory.
  • The LED icon files you want to use. (You may use the ones in the demo I'm providing.) Copy them to your project resources directory (res subdirectory).

Open your project in your developing environment and follow these steps:

  • Add the files LedButton.h and LedButton.cpp to your project.
  • Using the resource editor, add a checkbox to your dialog, change its ID to IDC_LED_CHECK. You may change the attributes of this checkbox as well, the relevant attributes are: Left text, Multiline, Horizontal alignment, Vertical alignment, Right aligned text.
  • Import the icon files using the resource editor, name the IDs as IDI_GRAY_ICON, IDI_GREEN_ICON, IDI_YELLOW_ICON, IDI_RED_ICON and IDI_BLUE_ICON, respectively.
  • Use the ClassWizard to add a member variable called m_ledCtrl as a control variable. (The type generated will be as CButton, this will be changed later on).
  • Change the type for the LED button control from CButton to CLedButton in your dialog header file.
  • Add the preprocessor #include "LedButton.h" to the same header file as well.

The dialog header file should contain:

...
#include "LedButton.h"
...
class DemoDlg : public CDialog
{
   ...
private:
   ...
   CLedButton m_ledCtrl;
};

The dialog implementation file should contain:

void CDemoDlg::DoDataExchange(CDataExchange* pDX)
{
   ...
   DDX_Control(pDX, IDC_LED_CHECK, m_ledCtrl);
   ...
}

Now is the time to initialize and configure the LED button.

A Simple LED Button

By default, a CLedButton control is created with two LedStates, you only need to setup the icons, one for each state, and then set the LedState according to your needs.

For your convenience, you may define an enumerated type that will be used to describe each state. The OFF state with the integer value of zero, and the ON state with the value of 1, or use the Afx FALSE and TRUE #defines, or use the LED button own predefined values LED_BUTTON_STATE_OFF or LED_BUTTON_STATE_ON, respectively.

Add the following code to your InitDialog() method:

    ...
    m_ledCtrl.SetIcons(IDI_GRAY_ICON, IDI_YELLOW_ICON);
    ...

The default state of the LED button is LED_BUTTON_STATE_OFF.

If you want to change the default size of the icon, you can't use the SetIcons() instead, call one of the SetIcon() methods for each LED state:

    ...
    m_ledCtrl.SetIcon(LED_BUTTON_STATE_OFF, IDI_GRAY_ICON, 14,14);
    m_ledCtrl.SetIcon(LED_BUTTON_STATE_ON, IDI_YELLOW_ICON, 14,14);
    ...

Every time you want to change the LED color, just call the method SetLedState() with new LED state to display.

    ...
    // Turn the Led ON
    m_ledCtrl.SetLedState(LED_BUTTON_STATE_ON);
    ...

That is all if you don't want a more sophisticated LED button.

No action is done by SetLedState() if this method is called with a LED state that is the same as the one that the LED is currently displaying. (This is done to reduce flickering)

A Multi-State LED Button

A Multi-State LED button is like a Simple LED, but with more than two LED states.

It may be logical to define an enumerated type that will represent each LED state, although this is not mandatory.

In this document, let's define an enumerated type called EMyLedState that will serve for Multi-State LED buttons, as follows:

    ...
    enum EMyLedState {
        GRAY_LED_STATE = LED_BUTTON_STATE_ON,     // Initial LedState
        GREEN_LED_STATE,
        YELLOW_LED_STATE,
        RED_LED_STATE,
        BLUE_LED_STATE,
        
        MY_LED_STATES_SENTINEL    // Not to be used as a EMyLedState.
    };
    ...

The MY_LED_STATES_SENTINEL is the number of LED states that we will use.

The initialization of Multi-State LED buttons require that the method SetLedStatesNumber() should be called for setting the maximum number of LED states, before setting up the Icons for each state.

The code in your InitDialog() becomes:

    ...
    m_ledCtrl.SetLedStatesNumber(MY_LED_STATES_SENTINEL);
    m_ledCtrl.SetIcon(GRAY_LED_STATE, IDI_GRAY_ICON);
    m_ledCtrl.SetIcon(GREEN_LED_STATE, IDI_GREEN_ICON);
    m_ledCtrl.SetIcon(YELLOW_LED_STATE, IDI_YELLOW_ICON);
    m_ledCtrl.SetIcon(RED_LED_STATE, IDI_RED_ICON);
    m_ledCtrl.SetIcon(BLUE_LED_STATE, IDI_BLUE_ICON);
    ...

Setting the state is the same as before, but now the possible values are from GRAY_LED_STATE to BLUE_LED_STATE.

An Activity LED Button

An Activity LED button is a LED button that automatically turns off after a controllable period, removing in this way the user responsibility of managing the turn off time.

The activity time is controlled by an internal timer that is triggered when the LED button is set to any state other than LED_BUTTON_STATE_OFF. When the timer ages, the LED button is turned off. (The LED state the Activity LED will end when the timer ages can also be controlled.)

Because timers are a limited global resource, to activate a LED button, the user should provide a timerId that will identify the timer. In this way, two or more Activity LED buttons can coexist in the same application, each one with a different timer identification.

To deactivate an Activity timer, just provide a NULL timerId. The timer is also released when the LED button window is destroyed.

First, you may want to manage all the timers you use on your code in a single place, therefore, define in some visible header file, the timer identifier:

    ...
    #define MY_ACTIVITY_LED_TIMER_ID         (WM_USER + 0x123)
    ...

Then the code in the InitDialog() becomes:

    ...
    // Simple Led with Activity detection
    m_ledCtrl.SetIcons(IDI_GRAY_ICON, IDI_YELLOW_ICON);
    m_ledCtrl.SetLedActivityTimer(MY_ACTIVITY_LED_TIMER_ID, 123);
    ...

The code above will set the LED as an Activity LED with a duration of 123 milliseconds.

A Simple, Multi-State or Conditional LED button can also be an Activity LED button.

A Conditional LED Button

To understand Conditional LED states, let's look at the following situation:

  • The LED button control is capable of displaying more than two states.
  • The LED button is used as a glitch detector.
  • A thread will be reading some data from the hardware at high speed, waiting for some signal to change. When required, it will send a notification towards the GUI with the change notification.
  • The LED should change its color normally between "ON" and "OFF" when the changes are "slow" changes. But if two consecutive changes occur in less than 50 msec, the LED should show the "ON" or "OFF" states with a different color.

The following state machine may provide a better picture:

Glitch Detector

When a LED button state depends on two (or more) inputs, then the LED button becomes a Conditional LED.

For the "Glitch Detector", the inputs are:

  • Current LED state.
  • Requested new LED state.
  • Elapsed time since the last LED change.

For setting up a Conditional LED, you should create a derived object from a class that is derived from CLedStateCondition (a class defined in the LedButton.h header file).

The subclass should overwrite the ChangeState() virtual method.

The ChangeState() method receives three parameters:

  • newLedState, the requested new LedState.
  • oldLedState, the current LedState before the transition.
  • isForcedChange, a boolean flag that instructs a forced transition to the newLedState. The derived class should fix its private FSM to reflect this forced change, and return this newLedState.

The ChangeState() should contain all the logic for deciding about the new LED state the LED button will go. (I will not provide code here for the Glitch Detector, due that I want to focus on the Conditional LED.)

You may define the following enumerated type for dealing with the Glitch Detector in some header file:

    ...
    enum EGlitchDetectorState
    {
        OFF_STATE = LED_BUTTON_STATE_ON,      // Green LED,
        ON_STATE,                             // Yellow LED,
        GLITCH_OFF_STATE,                     // Blue LED,
        GLITCH_ON_STATE,                      // Red LED,
        IDLE_STATE,                           // No LED Icon,
        
        GLITCH_DETECTOR_STATES_SENTINEL // Not to be used as a EGlitchDetectorState.
    };
    ...

The hardware notifications will come as TRUE and FALSE respectively.

For this example, the name of the CLedStateCondition derived class will be CGlitchDetector. Put an instance of this CGlitchDetector in the CDemoDlg and call it m_glitchDetector.

The code on your dialog InitDialog() could be:

    ...
    //
    // 1) Initialize the m_glitchDetector as required (code not shown here)
    // 2) Set the led as a multi-state led.
    // 3) Add the Glitch Detector to the Led Button.
    // 4) Set the initial state as IDLE_STATE. (Force this state)
    //
    m_ledCtrl.SetLedStatesNumber(GLITCH_DETECTOR_STATES_SENTINEL);
    m_ledCtrl.SetIcon(OFF_STATE, IDI_GREEN_ICON);
    m_ledCtrl.SetIcon(ON_STATE, IDI_YELLOW_ICON);
    m_ledCtrl.SetIcon(GLITCH_ON_STATE, IDI_RED_ICON);
    m_ledCtrl.SetIcon(GLITCH_OFF_STATE, IDI_BLUE_ICON);
    m_ledCtrl.SetIcon(IDLE_STATE, 0,0,0); // No Icon on purpose.
    
    m_ledCtrl.SetLedStateCondition(&m_glitchDetector);
    
    m_ledCtrl.SetLedState(IDLE_STATE, true); // Force IDLE_STATE.
    ...

In the code that receives the hardware notifications, we will call the SetLedState() method with the OFF_STATE or ON_STATE values, respectively.

afx_msg LONG CDemoDlg::OnHWUpdate(WPARAM wparam, LPARAM /*lparam*/)
{
    ...
    LedState ledState = (TRUE == wparam) ? ON_STATE : OFF_STATE;
    m_ledCtrl(ledState);
    ...
}

A Simple, Multi-State or Activity LED button can also be a Conditional LED button.

Text Attributes

It is possible to change the text foreground color or/and button background color for each LED state. By default, the text foreground color is ::GetSysColor(COLOR_BTNTEXT) and the button background color is ::GetSysColor(COLOR_BTNFACE).

The following methods control the LED state colors:

  • SetTextForeground():

    Set the text foreground color for a specific LED state.

  • GetTextForeground():

    Get the text foreground color for a specific LED state.

  • SetTextBackground():

    Set the text background color for a specific LED state.

  • GetTextBackground():

    Get the text background color for a specific LED state.

  • SetTextColors():

    Set both text foreground and button background colors for a specific LED state.

  • RestoreDefaultColors():

    Restore the default colors for all LED states, use with care.

ToolTips

This LED button control can show a "tool tip", a small pop-up window that displays some text describing the purpose of the LED. The tooltip is hidden most of the time, appearing only when the user puts the cursor over the LED button and leaves it there for half a second. The tool tip appears near the cursor and disappears when the user clicks a mouse button or moves the cursor off the tool.

The following methods control tooltips:

  • SetTooltipText():

    Set the tooltip text, from a resource string or from the provided text.

  • ActivateTooltip():

    Activate/deactivate the tooltip.

On the current version, only "rectangular" tool tips are supported.

Code Documentation

The code contains Doxygen comments, used to generate HTML documentation out of the code. I strongly advice you to use such tools, directly or indirectly.

Doxygen is a documentation system for C++, C, Java, Objective-C, IDL (CORBA and Microsoft flavors), and to some extent, PHP, C# and D. It can be found at Doxygen[^].

The companion help file was generated using the KingsTools [^] Visual Studio .NET add-in by SteveKing [^]. This add-in contains several useful tools you may like to have integrated in your developing environment. (Doxygen, code-statistics, syntax coloring, etc.)

Thanks to SteveKing for this tool, and his disclaimer note I'm using in my code.

Provided Files

The following files are provided in the sources and demo project that you can use on your own projects:

Source files: LedButton.h and LedButton.cpp
LED icon files: 13 Led Icons with different colors. (LedButton_src.zip)
Light bulb files: LightBulbOff.ico, LightBulbOn.ico, LightBulbBroken.ico (in the demo project)

Disclaimer

This code and the accompanying files are provided "as is" with no expressed or implied warranty. No responsibilities for possible damages, or side effects in its functionality. The user must assume the entire risk of using this code. The author accepts no liability if it causes any damage to your computer, causes your pet to fall ill, increases baldness or makes your car start emitting strange noises when you start it up. This code has no bugs, just undocumented features!.

Terms of use

This code is free for personal use, or freeware applications. If you plan to use this code in a commercial or shareware application, you are politely asked to contact the author for his permission.

ChangeLog

Date Rev. Description
2004/Jan/16 1.1 Flicker-free update (Thanks to Iain Clarke[^])
2004/Dec/25 1.0 Debut in CodeProject
2002/Aug/16 --- Created

Epilogue

Hope that this LED control is useful for you.

To help other users in CodeProject, please Rate this Article :)

License

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

About the Author

rbid
Software Developer (Senior)
Israel Israel
I was born in Tarija - Bolivia, and since 1981 I'm in Israel. Currently work for Given Imaging Ltd. as Embedded Software Engineer.
 
My software knowledge spans from sysadmin of Unix machines in the 90'(HPUX, Ultrix, SunOS, Solaris, Linux, Perl, XEmacs), Embedded programming for more than a decade (old pSOS, VxWorks, 68K, PPC, 960, ARM and others), Java with Network Management using SNMP, deep knowledge of Networking (I was the Network Architect for wireless systems using 802.16/802.11, in my previous workplace).
 
My interests are photography and travell to interesting places around our globe.
 
I really enjoy www.codeproject.com. It has saved me an incredible amount of time. I only hope my small contributions have given back some of what I'm using.
 
RBID stands for Ricky, Brenda, Irit and Daniel (My family), my real name is Ricky Marek.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalgreat work !!memberidan_bismut3-Nov-09 0:36 
very nice work, thanks for sharing Smile | :)
Generalnice jobmemberlxsure3-May-09 19:46 
keep the good work going~
QuestionHow to create CLedButton dynamically with Create-method?membermpietarin20-Jan-08 9:32 
Hi,
 
I need to create CLedButton-object dynamically. I tried to just add following member in my CMainFrame-class:
 
CLedButton m_MyLedButton;
 
Then on CMainFrame's OnCreate-method where I needed to add the LedButton-control to the statusbar I saw that object was created but its hWnd-member was zero and nothing was seen on statusbar except empty place holder for ledbutton. So obviously I needed to call the CButton's Create-method. Does anyone know how to call it, because anything I try crashes (something about "can't attach something that is allready attached to somewhere", couldn't really figure it out). The following code works for 'plain' CButton (hWnd-member is not zero after the call) but doesn't work for CLedButton-object:
 
m_MyLedButton.Create(
"label",
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, // tried different combinations with no success
CRect(0,0,25,25), // just some button size
&m_wndStatusBar, // the statusbar as parent
IDS_PANE_LED_BUTTON // resource id
);
GeneralRe: How to create CLedButton dynamically with Create-method?member rbid31-Jan-08 9:00 
Hello,
 
It was long ago since I have created this control.. I will check why it does not work in the way you mentioned.
 
IMHO, You can use the native status bar control to add an icon with the following code:
 
void YourDialogClass::UpdateStatusBarIcons(int status)
{
    UINT    iconId = IDI_LED_NONE_ICON;
 
    switch(status)
    {
        case 0:
            break;
        case 1:
            iconId = IDI_LED_YELLOW_ICON;
            break;
        case 2:
            iconId = IDI_LED_GREEN_ICON;
            break;
        case 3:
            iconId = IDI_LED_RED_ICON;
            break;
        default:
            break;
    }
 
    if (iconId)
    {
    
	// Find correct resource handle
	HINSTANCE hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(iconId), RT_GROUP_ICON);
 
	// Set icon when the mouse is IN the button
	HICON hIcon = (HICON)::LoadImage(hInstResource, MAKEINTRESOURCE(iconId), IMAGE_ICON, 16, 16, 0);
 
        m_statusBar.SetIcon(3, hIcon);
    }
    else
    {
        m_statusBar.SetIcon(3, NULL);
    }
 
}
 
The StatusBar has 4 parts, therefore the Icon is located on the last one. Just call this function from your dialog each time you want to change the Icon on the status bar.
 
If you need some timers, you may use the same code as in CLedButton for changing the icon used in your status bar.
 
Hope it helps. (Remember: there is always another way to do it)
 
-- Ricky Marek (AKA: rbid)
-- "Things are only impossible until they are not" --- Jean-Luc Picard
 
My articles
 

GeneralA Conditional LED ButtonmemberBurzredion2-Oct-06 6:27 
How can I use the Conditional LED Button to indicate a change of the color when I click the check box?
I'm a newbie and I want to use your LedButton Status Control to make a software for controlling paralell port and indicate the state of the LED clicking on the check box then I'll use another button to send the data by paralell port. I need an advice to do that. :$
 
Thanks.
 
Share the knowledge.
 
If you hear, you forget; If you see, you remember; If you do, you understand.
Proverb.

GeneralRe: A Conditional LED Button [modified]memberrbid2-Oct-06 9:58 
The CLedButton is to be a "Read-Only" control.
 
In order to have the functionality you requested, there are several changes you need
to do on the code of the CLedButton:
- Reflect the BM_CLICKED message,
- deal with the BM_SETCHECK and BM_GETCHECK messages.
- Provide a SetCheck and GetCheck method to get/set the check.
 
I would create a member variable to store the led check (as int m_ledCheck), the functions SetCheck and GetCheck would set/get the values from this member variable.
Create two methods that will reflect the BM_SET_CHECK and BM_GET_CHECK that will call the SetCheck or GetCheck method. and the BM_CLICKED will toggle the state of the member variable.
 
After this is implemented, you can use in your code the following:
Add to your dialog message map:
BEGIN_MESSAGE_MAP(CLedButtonDemoDlg, CDialog)
    ...
    ON_BN_CLICKED(IDC_CONDITIONAL_LED_CHECK, OnConditionalLedBnClicked)
    ...
END_MESSAGE_MAP()
When IDC_CONDITIONAL_LED_CHECK is the control that contains the Led and m_conditionalLedCtrl is the CLedButton control variable.
The function OnConditionalLedBnClicked will deal with the checkbox state and reflect it to the corresponding Led state (using the method GetCheck):
void CDemoDlg::OnConditionalLedBnClicked()
{
    bool isChecked = (BST_CHECKED == m_conditionalLedCtrl.GetCheck());
    // use isChecked for your needs...
}
 
 
Hope this helps to solve your problem.. or I did not fully understand your question Blush | :O
 
Have a nice day.

 

-- modified at 9:01 Monday 9th October, 2006
 
-- Ricky Marek (AKA: rbid)
-- "Things are only impossible until they are not" --- Jean-Luc Picard
 
My articles
 

GeneralUsing as a .NET control in VB .NETmembersbouli4-Feb-05 3:47 
Hi,
 
I would love to use this little object in a VB form, is there a way to compil it as a dll and add it to the toolbox in order to just drag and drop a led in a VB form ?
 
Thanks for your help

 
Stéphane
GeneralRe: Using as a .NET control in VB .NETmemberrbid4-Feb-05 10:31 
Hello,
 
I don't have knowledge about .Net(yet), but I guess that it would be better to have it as an ActiveX control Moving from an MFC control to an ActiveX control is not imposible.
 
This control was intended to be used as a simple MFC control, so I didn't have in mind to port it to other architecture.. but your request may be posible some day. keep tuned.
 

 
-- Ricky Marek (AKA: rbid)
-- "Things are only impossible until they are not" --- Jean-Luc Picard
 
My articles
 

GeneralFlash Led ControlmemberHoria Tudosie19-Jan-05 11:31 
See also http://www.codeproject.com/cs/miscctrl/FlashLED.asp OMG | :OMG:
 
Horia Tudosie
GeneralRe: Flash Led Controlmemberrbid19-Jan-05 20:38 
Thanks for the link, hope is useful for C# people. Currently I'm not in C# yet.Unsure | :~
 

 
-- Ricky Marek (AKA: rbid)
-- "Things are only impossible until they are not" --- Jean-Luc Picard
 
My articles
 

GeneralReducing flicker & Prefined Icons. (Long).memberIain Clarke13-Jan-05 3:50 

I put your class to use, and immediately made a couple of modifications.
 
The first was to reduce the flicker. I had a routine monitoring something in
hardware tens of times a second, and setting the led states. One LED seemed
stable, but the other flickered quite badly.
 
Possibly this could have been fixed by using Invalidate (FALSE), but that could
have caused unintentional problems. Instead I did the quick fix below.
 
"If nothing has changed, don't bother to redraw it!".
 
I wouldn't like to guarantee this wouldn't have an effect on glitch testing,
but I doubt it.
 
void CLedButton::SetLedState(LedState ledState, bool isForcedChange /*=false*/)
{
    // Iain - changed this to verify that the state is different before we change it!
    if (m_ledState == ledState)
        return;
 
    ...
}
 
Secondly, I wanted to put the class in my Utility dll, a dll I use in a bunch of
projects. My MFC booster. But I didn't want all (OK, 3) my different apps to have
to know the icons for the LED, and the icon IDs. So I added an enum, a table, and
a couple of new methods...
 
In the LedButton.h...
class UTILSEXPORT CLedButton : public CButton
{
    ....
public:
    CLedButton();
 
    // Added by Iain.
    enum ledPredefined { ledNone,
        ledBlue, ledGray, ledGreen, ledRed, ledYellow,
        ledSmileyAngry, ledSmilyCrazy, ledSmilyHappy, ledSmilySick, ledSmilyUnsure,
        ledBulbBroken, ledBulbOff, ledBulbOn };
 
    ....
 
    bool SetIcon(LedState ledState, HICON hIcon);
    bool SetPredefinedIcon (LedState ledState, ledPredefined nIcon); // Iain.

    ....
 
    bool SetIcons(UINT offIconId, UINT onIconId);
    bool SetPredefinedIcons(ledPredefined offIconId, ledPredefined onIconId);
 
    ....
 
protected:
    // A member struct, just so I don't pollute the workspace.
    struct LedPredefined
    {
        ledPredefined predef;
        UINT  nIconID;
    };
    static LedPredefined m_Predefined [];
    UINT LookupPredefined (ledPredefined predef);
   
    ....
};
 
And in LedButton.cpp...
////////////
// Added by Iain.

CLedButton::LedPredefined CLedButton::m_Predefined [] = {
	{ ledNone, IDI_GRAY_LED_ICON },
	{ ledGray, IDI_GRAY_LED_ICON },
	{ ledBlue, IDI_BLUE_LED_ICON },
	{ ledGreen, IDI_GREEN_LED_ICON },
	{ ledRed, IDI_RED_LED_ICON },
	{ ledYellow, IDI_YELLOW_LED_ICON },
	{ ledSmileyAngry, IDI_ANGRY_SMILY_ICON },
	{ ledSmilyCrazy, IDI_CRAZY_SMILY_ICON },
	{ ledSmilyHappy, IDI_HAPPY_SMILY_ICON },
	{ ledSmilySick, IDI_SICK_SMILY_ICON },
	{ ledSmilyUnsure, IDI_UNSURE_SMILY_ICON },
	{ ledBulbBroken, IDI_LIGHT_BULB_BROKEN_ICON },
	{ ledBulbOff, IDI_LIGHT_BULB_OFF_ICON },
	{ ledBulbOn, IDI_LIGHT_BULB_ON_ICON }
};
 
UINT CLedButton::LookupPredefined (ledPredefined predef)
{
	for (int n = 0; n < sizeof (m_Predefined)/sizeof(m_Predefined[0]); n++)
	{
		if (m_Predefined [n].predef == predef)
			return m_Predefined [n].nIconID;
	}
 
	return m_Predefined [0].nIconID; // Default case.
}
 
bool CLedButton::SetPredefinedIcons(ledPredefined offIconId, ledPredefined onIconId) // Iain
{
	return SetIcons (LookupPredefined (offIconId), LookupPredefined (onIconId));
}
 
bool CLedButton::SetPredefinedIcon (LedState ledState, ledPredefined nIcon) // Iain.
{
	HICON hIcon = AfxGetApp ()->LoadIcon (LookupPredefined (nIcon));
	if (!hIcon)
		return false;
 
	return SetIcon (ledState, hIcon);
}
 
 

Iain.
GeneralRe: Reducing flicker &amp; Prefined Icons. (Long).memberrbid13-Jan-05 8:17 
Thanks!.
 
About the flicker reduction, you are right, and I forgot to mention on
my article that the SetLedState() method should be called only only when the led state changes. I will add the fix on my next update.
 
I never used as a dll, that was nice to read that the led can be also
used there with your modifications.
 
BTW, the smily stuff was just to show that you can use other icons, I guess that most of our projects do not need these icons Laugh | :laugh:
 
Have a nice day.
 
-- Ricky Marek (AKA: rbid)
-- "Things are only impossible until they are not" --- Jean-Luc Picard
GeneralRe: Reducing flicker &amp; Prefined Icons. (Long).memberIain Clarke13-Jan-05 12:08 
rbid wrote:
BTW, the smily stuff was just to show that you can use other icons, I guess that most of our projects do not need these icons
 
I was just being thorough. Adding those was a cut and paste job!
 
Iain.

GeneralRe: Reducing flicker &amp; Prefined Icons. (Long).memberJaime Stuardo8-Mar-05 12:53 
I have used this control in a DLL too that is exported by mean of AFX_EXT_CLASS. The only thing I had to do in order for it to work is to add this enum:
 
public:
CLedButton();
 
enum ledIcons {ledNone = IDI_LEDBUTTON_NONE,
ledBlack = IDI_LEDBUTTON_BLACK,
ledBrown = IDI_LEDBUTTON_BROWN,
ledCyan = IDI_LEDBUTTON_CYAN,
ledDarkBlue = IDI_LEDBUTTON_DARKBLUE,
ledDarkRed = IDI_LEDBUTTON_DARKRED,
ledPurple = IDI_LEDBUTTON_PURPLE,
ledTrasparent = IDI_LEDBUTTON_TRASPARENT,
ledWhite = IDI_LEDBUTTON_WHITE,
ledBlue = IDI_LEDBUTTON_BLUE,
ledGreen = IDI_LEDBUTTON_GREEN,
ledRed = IDI_LEDBUTTON_RED,
ledYellow = IDI_LEDBUTTON_YELLOW};

 
And from the exe program (or even from other DLL) I had to do:
m_bitLock.SetIcons(CLedButton::ledNone, CLedButton::ledYellow);
 
and it worked.
 
I think your thinking was about having default icons in the DLL where CLedButton is, and custom icons in the program that uses CLedButton control.
If this is the case, I think you have to rename SetIcon member function to be SetPredefinedIcon because it uses AfxFindResourceHandle to retrieve the instance handle of the DLL, that is, where the predefined icons are.
 
Your function uses AfxGetApp ()->LoadIcon to get the icons, which should load them from the application resource.
 
Anyway, the solution I posted at the beginning of this comment worked for me very well Smile | :)
 
Jaime

GeneralArrows in state diagram seem to be wrongmemberDon Clugston11-Jan-05 19:09 
The 'Slow On' and 'Slow Off' arrows seem to be reversed.
Otherwise, looks good! I wrote an app a couple of years ago where this is _exactly_ what I wanted.
 

GeneralRe: Arrows in state diagram seem to be wrongsussrbid11-Jan-05 20:13 
Hello Don,
 
Don Clugston wrote:
The 'Slow On' and 'Slow Off' arrows seem to be reversed.
Otherwise, looks good! I wrote an app a couple of years ago where this is _exactly_ what I wanted.

 
Yes, you are right. I will update it.
 


 
-- Ricky Marek (AKA: rbid)
-- "Things are only impossible until they are not" --- Jean-Luc Picard
GeneralNice Postmembersudhir mangla9-Jan-05 16:54 
nice Post
 
Sudhir Mangla
 
http://Programmerworld.net

(Free books , articles , Source Code and Programming Tools and Utilities)
GeneralRe: Nice Postsussrbid9-Jan-05 20:36 
Thanks,Smile | :) Cool | :cool:
 
Nice books and stuff on your site too.
 

 
-- Ricky Marek (AKA: rbid)
-- "Things are only impossible until they are not" --- Jean-Luc Picard
GeneralGraphic improvementsmembernorm.net12-Jan-05 23:12 
You could draw your LEDs on the fly, using GDI+ and especially the LinearGradientBrushes.
 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 18 Jan 2005
Article Copyright 2005 by rbid
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid