Click here to Skip to main content
6,306,412 members and growing! (18,942 online)
Email Password   helpLost your password?
Desktop Development » Toolbars & Docking windows » General     Intermediate

Generating inactive/disabled images for toolbar

By Member 307612

Provides an extensible class for drawing disabled and inactive toolbar button images.
VC6, VC7Win2K, WinXP, MFC, Dev
Posted:18 May 2003
Views:100,423
Bookmarked:52 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
15 votes for this article.
Popularity: 5.47 Rating: 4.65 out of 5

1

2
1 vote, 6.7%
3
3 votes, 20.0%
4
11 votes, 73.3%
5

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

About the Author

Member 307612


Member

Location: Belarus Belarus

Other popular Toolbars & Docking windows articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 34 (Total in Forum: 34) (Refresh)FirstPrevNext
GeneralHandling toolbars of non-default size PinmemberDiomidis Spinellis23:33 22 Jan '09  
GeneralThank you! Pinmemberthready5:06 11 Aug '07  
GeneralRe: Thank you! PinmemberKeith K. S. Lee21:35 16 Sep '07  
GeneralAdding new 24 bit in exiting toolbar PinmemberRajan Kapadia22:41 28 Dec '06  
GeneralRe: Adding new 24 bit in exiting toolbar PinmemberYogesh P. Dhakad4:55 16 Jul '07  
GeneralAfxGetGrayBitmap PinmemberSarath.1:03 15 Oct '06  
QuestionIncompatibility between TBSTYLE_CHECK and SetDisabledImageList? PinmemberLStewart11:06 15 Mar '06  
GeneralRe: Incompatibility between TBSTYLE_CHECK and SetDisabledImageList? PinmemberLStewart15:33 15 Mar '06  
Generalhow to adjust image size Pinmemberhakers22:10 17 Aug '05  
GeneralRe: How to add this code Visual Basic Project PinmemberChristian Graus17:43 3 Apr '05  
GeneralDisabled Button on CToolBarCtrl remains Active PinmemberMarkus Kitzig1:59 14 Jan '05  
GeneralRe: Disabled Button on CToolBarCtrl remains Active PinmemberIgor Green2:20 14 Jan '05  
GeneralRe: Disabled Button on CToolBarCtrl remains Active PinmemberMarkus Kitzig4:53 14 Jan '05  
GeneralRe: Disabled Button on CToolBarCtrl remains Active PinmemberIgor Green5:11 14 Jan '05  
Generalhelp!!!! Pinmemberzhxih3:14 12 Dec '04  
GeneralRe: help!!!! PinmemberIgor Green6:54 12 Dec '04  
GeneralRe: help!!!! Pinmemberzhxih20:36 14 Dec '04  
Generalabout the position of bitmap in toolbar Pinmembergettercomic1:00 16 Sep '04  
GeneralRe: about the position of bitmap in toolbar PinsussAnonymous7:33 8 Oct '04  
Generalbackground color not transparent PinmemberWindSeven8:17 3 Sep '04  
GeneralRe: background color not transparent PinmemberIgor Green9:03 3 Sep '04  
GeneralRe: background color not transparent Pinmemberschmurgulum6:55 8 Oct '04  
GeneralRe: background color not transparent PinmemberWindSeven20:22 9 Oct '04  
Generalhow to add dropdown buttons + combo-box ? Pinmemberdarthmaul5:53 15 Dec '03  
GeneralChanging Graphic in VC6 Application PinmemberAJOsborne11:00 6 Aug '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 May 2003
Editor: Heath Stewart
Copyright 2003 by Member 307612
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project