Click here to Skip to main content
11,583,859 members (46,792 online)
Click here to Skip to main content

Generating inactive/disabled images for toolbar

, 18 May 2003 144.3K 4.7K 66
Rate this:
Please Sign up or sign in to vote.
Provides an extensible class for drawing disabled and inactive toolbar button images.

Inactive/disabled images

Introduction

Everybody who used full-color images for toolbars have been faced with the problem of creating disabled/inactive images. Whether you order images from a professional artist, or get them from shell32.dll from WinXP, in most cases you only have normal images. The common way is preparing copies of bitmaps for disabled/inactive glyphs using a simple image editor. For example, I have used editing features of IrfanView. But this approach has several drawbacks - you have to store multiple images which makes your app bigger; and, most importantly, you will have to do a lot of tedious work if you decided to add/modify toolbar images later.

The alternative approach is automatically generate sub images from the original picture. To my surprise, I have not found on CodeProject any ready-to-use solutions, so I have combined several existing techniques from other authors.

Background

The code for this article is based on following articles from CodeProject:

  1. CColor - RGB and HLS combined in one class by Christian Rodemeyer, for RGB-HLS conversion (color.cpp, color.h).
  2. Add fast user-extensible image processing support to CBitmap by Daniel Godson, for bitmap's pixel-by-pixel processing (enbitmap.cpp, enbitmap.h).

I have to admit that my own additions to these authors is no more than 10 lines of code, but I still hope you will find the result useful. I have also removed all unused stuff from both source codes to minimize the size of the resulting stand-alone component.

Using the Code

At the top of all other authors' source code sits CToolBar24 - a drop-in replacement of CToolBar class in your code. For an MDI/SDI application, simply replace the type of your CMainFrame::m_wndToolBar member to CToolBar24 (declared in enbitmap.h), and make a single additional call:

// standard wizard generated toolbar initialization
if (!m_wndToolBar.CreateEx(this,TBSTYLE_FLAT,WS_CHILD | WS_VISIBLE | CBRS_TOP
    | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
    !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
    TRACE0("Failed to create toolbar\n");
    return -1;      // fail to create
}
    
// HERE WE GO: 
m_wndToolBar.SetFullColorImage(IDR_MAINFRAME_24, RGB(255, 0, 255));

Just one function is added: void CToolBar24::SetFullColorImage(UINT ID, COLORREF rgbBack), where ID is the resource ID of the full color bitmap, and rgbBack is the background color of your bitmap (in my sample this is RGB(255, 0, 255)).

As result, you will have a full-color toolbar, with inactive buttons looking a bit lighter, and disabled buttons grayed out.

Of course, you don't have to like this look and feel of special state images. Due to the modular structure, you can easily modify inactive/disabled image generation. Just read next section.

Behind the Scenes

Image processing itself is done by the CEnBitmap class, which can modify pixels by using special ImageProcessor classes. Two processors are provided with this project: CImageGrayer, which grays the picture out; and CImageHigh, which can make image look lighter or darker. Look for the CEnBitmap::MakeDisabled and CEnBitmap::MakeNotActive functions to modify the appropriate button appearance. For example, here is how the current MakeDisabled works:

BOOL CEnBitmap::MakeDisabled(COLORREF bk)
{
    int R = GetRValue(bk);
    int G = GetGValue(bk);
    int B = GetBValue(bk);

    CImageHigh high(0.08f);
    high.SetBkColor(R, G, B);
    CImageGrayer gray;
    gray.SetBkColor(R, G, B);

    C32BIPArray aProcessors;
    aProcessors.Add(&gray);
    for (int i=0; i<3; i++)
        aProcessors.Add(&high);

    return ProcessImage(aProcessors);
}

As you can see, an array of processors is prepared and handled. For details of processors implementation I suggest you check the original article. But in most cases you will find reasonable picture results by playing with the given 2 processors just like I did.

Do not miss the constructor parameter for CImageHigh(float fLum=0.1). fLum is the offset for Luminance, which is applied every time CImageHigh processor is used. fLum should be between -1 and 1, negative values will make image look dark. For detailed description of the HLS model see this article.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Member 307612
Belarus Belarus
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralHandling toolbars of non-default size Pin
Diomidis Spinellis22-Jan-09 22:33
memberDiomidis Spinellis22-Jan-09 22:33 
GeneralThank you! Pin
thready11-Aug-07 4:06
memberthready11-Aug-07 4:06 
GeneralRe: Thank you! Pin
Keith K. S. Lee16-Sep-07 20:35
memberKeith K. S. Lee16-Sep-07 20:35 
GeneralAdding new 24 bit in exiting toolbar Pin
Rajan Kapadia28-Dec-06 21:41
memberRajan Kapadia28-Dec-06 21:41 
GeneralRe: Adding new 24 bit in exiting toolbar Pin
Yogesh P. Dhakad16-Jul-07 3:55
memberYogesh P. Dhakad16-Jul-07 3:55 
GeneralAfxGetGrayBitmap Pin
Sarath.15-Oct-06 0:03
memberSarath.15-Oct-06 0:03 
QuestionIncompatibility between TBSTYLE_CHECK and SetDisabledImageList? Pin
LStewart15-Mar-06 10:06
memberLStewart15-Mar-06 10:06 
GeneralRe: Incompatibility between TBSTYLE_CHECK and SetDisabledImageList? Pin
LStewart15-Mar-06 14:33
memberLStewart15-Mar-06 14:33 
Questionhow to adjust image size Pin
hakers17-Aug-05 21:10
memberhakers17-Aug-05 21:10 
AnswerRe: how to adjust image size Pin
vt123456789023-Dec-10 17:59
membervt123456789023-Dec-10 17:59 
GeneralRe: How to add this code Visual Basic Project Pin
Christian Graus3-Apr-05 16:43
memberChristian Graus3-Apr-05 16:43 
GeneralDisabled Button on CToolBarCtrl remains Active Pin
Markus Kitzig14-Jan-05 0:59
memberMarkus Kitzig14-Jan-05 0:59 
GeneralRe: Disabled Button on CToolBarCtrl remains Active Pin
Igor Green14-Jan-05 1:20
memberIgor Green14-Jan-05 1:20 
GeneralRe: Disabled Button on CToolBarCtrl remains Active Pin
Markus Kitzig14-Jan-05 3:53
memberMarkus Kitzig14-Jan-05 3:53 
GeneralRe: Disabled Button on CToolBarCtrl remains Active Pin
Igor Green14-Jan-05 4:11
memberIgor Green14-Jan-05 4:11 
Generalhelp!!!! Pin
zhxih12-Dec-04 2:14
memberzhxih12-Dec-04 2:14 
GeneralRe: help!!!! Pin
Igor Green12-Dec-04 5:54
memberIgor Green12-Dec-04 5:54 
GeneralRe: help!!!! Pin
zhxih14-Dec-04 19:36
memberzhxih14-Dec-04 19:36 
Generalabout the position of bitmap in toolbar Pin
gettercomic16-Sep-04 0:00
membergettercomic16-Sep-04 0:00 
GeneralRe: about the position of bitmap in toolbar Pin
Anonymous8-Oct-04 6:33
sussAnonymous8-Oct-04 6:33 
Generalbackground color not transparent Pin
WindSeven3-Sep-04 7:17
memberWindSeven3-Sep-04 7:17 
GeneralRe: background color not transparent Pin
Igor Green3-Sep-04 8:03
memberIgor Green3-Sep-04 8:03 
GeneralRe: background color not transparent Pin
schmurgulum8-Oct-04 5:55
memberschmurgulum8-Oct-04 5:55 
GeneralRe: background color not transparent Pin
WindSeven9-Oct-04 19:22
memberWindSeven9-Oct-04 19:22 
GeneralRe: background color not transparent Pin
Kaylene Thaler1-Apr-10 21:11
memberKaylene Thaler1-Apr-10 21:11 
Questionhow to add dropdown buttons + combo-box ? Pin
darthmaul15-Dec-03 4:53
memberdarthmaul15-Dec-03 4:53 
GeneralChanging Graphic in VC6 Application Pin
AJOsborne6-Aug-03 10:00
memberAJOsborne6-Aug-03 10:00 
GeneralImages initialize disabled Pin
andyandlaurie31-Jul-03 12:30
memberandyandlaurie31-Jul-03 12:30 
GeneralRe: Images initialize disabled Pin
grigsoft5-Aug-03 22:32
membergrigsoft5-Aug-03 22:32 
GeneralRe: Images initialize disabled Pin
thready9-Aug-07 18:01
memberthready9-Aug-07 18:01 
GeneralglyFX Pin
William Brendel14-Jun-03 2:30
memberWilliam Brendel14-Jun-03 2:30 
GeneralRe: glyFX Pin
Hockey30-Oct-03 20:42
memberHockey30-Oct-03 20:42 
GeneralExtra image needed for article Pin
Roger Allen19-May-03 5:44
memberRoger Allen19-May-03 5:44 
GeneralRe: Extra image needed for article Pin
grigsoft19-May-03 6:09
membergrigsoft19-May-03 6:09 
GeneralRe: Extra image needed for article Pin
Roger Allen19-May-03 6:22
memberRoger Allen19-May-03 6:22 
GeneralRe: Extra image needed for article Pin
Uwe Keim19-May-03 19:16
sitebuilderUwe Keim19-May-03 19:16 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 19 May 2003
Article Copyright 2003 by Member 307612
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid