Click here to Skip to main content
13,290,729 members (80,324 online)
Click here to Skip to main content
Add your own
alternative version


78 bookmarked
Posted 16 May 2000

A Better Bitmap Button Class

, 14 Oct 2001
Rate this:
Please Sign up or sign in to vote.
An improvement on the CBitmapButton class.


I never much liked the CBitmapButton class. It has several major defects.

  • It requires an unnatural paradigm of naming resources.
  • It does not work well with multiple screen resolutions.
  • It requires too much work on the part of the programmer to create the button images.

This class has too much generality and power for the "typical" case. So it makes the simple case as hard as the most general case. I don't need the most general case, and you probably don't either. You just want to display an interesting bitmap in a button, rather than some boring piece of text. The rest you'd like to work properly without any effort on your part.

The CBitmapButton class is well and truly weird in that it nominally requires you to create one-to-four images of the button, all nicely designed with the right kinds of edges and everything, and name them with funky names like "ACTIOND", "ACTIONU", "ACTIONF" and "ACTIONX". Of course, you can actually use IDs, and call the LoadBitmaps method yourself, but you still have to create those bitmaps.

Do you know how to create a really good-looking 3D edge? It's tricky. It is even more tricky when you have to create one that will work at any resolution. Do you know how to create a focus rectangle that looks good at any resolution? And what if you don't want your buttons to have to resize to the bitmap?

But why bother worrying about any of these issues? Mostly when I do a bitmap button I want a button that looks like an ordinary button. It has the appropriate edges, the appropriate edge changes on push-down, and a focus rectangle nicely displayed. The only thing I want different is the bitmap. And I'd like to draw only one, please.

The bitmap class described in this article solves all these problems. I've had code floating around for a while, but the number of recent bitmap questions prompted me to clean it up and get it into a publishable form.

CImageButton: A smart bitmap button class

CImageButton draws a button in a raised or depressed state and draws a focus rectangle around the image when the control has the focus.

This class requires only one bitmap, although you can supply up to three. You don't need the fourth (focus) bitmap because the focus rectangle is automatically drawn.

The bitmaps are specified solely by their resource IDs. No need to use nonstandard naming conventions. And if you want to use different bitmaps for different resolutions, you can do so just by calling LoadBitmaps with the desired bitmap IDs in your OnInitDialog handler.

If you supply only a single bitmap (the ideal situation), then the same bitmap is used for up and down, and will be grayed out with alternate-gray pixels when disabled. If you don't like the gray-out algorithm, you can supply an explicit gray-out image of your own devising for the disabled state.

Similarly, since the edges and focus are provided for you, you usually don't need to supply a special bitmap for the "down" state. The only real trick in doing the "down" state is that the focus rectangle and bitmap image shift right-and-down to preserve a visual illusion of a button push.

What about the problem of "dialog units", that nightmare that haunts everyone who ever tries to do something with a bitmap and dialog? If you don't know what this problem is, Microsoft cleverly decided that a dialog should resize itself based on screen resolution, so a dialog that looks reasonable at 640×480 will look equally reasonable at 1600×1200. Actually, this is a good idea, but its implementation leaves a lot to be desired. The most serious defect is that anything that uses a bitmap is in deep trouble, because bitmaps are always in pixel resolution. So your really nice-looking 16×16 bitmap at 640×480 is nearly invisible at 1600×1200. At 640×480 it is 1/40×1/30 of the screen. At 1600×1200 it is 1/100×1/75 of the screen. It has been reduced in size by a factor of 2.5.

I handle this by a provision to "fill" the button area with the bitmap image. This uses the StretchBlt function to expand the bitmap. For some images this works well; for others, it produces really ugly effects such as aliasing artifacts. But for a large number of simple images you use for your buttons, it is satisfactory, and saves having to create bitmaps at multiple resolutions.

What if you want an image that doesn't quite go from edge-to-edge in your button, but you want it to resize? Simply create a larger bitmap, and fill the bitmap in with the nominal 3D face color, which is RGB(192, 192, 192), "gray". I load the bitmap with LR_LOADMAP3DCOLORS, so instances of dark gray, gray, and light gray will all be replaced with the user's current display scheme settings. I use the COLOR_3DFACE to "gray out" the image if you haven't supplied a grayed-out image of your own, so the color scheme the user selects will be followed.

If you have chosen to not use the "fill" feature, this class honors the various button styles, such as BS_CENTER, BS_LEFT, and BS_RIGHT for horizontal alignment and BS_VCENTER, BS_TOP, and BS_BOTTOM for vertical alignment.

Actually, the only really tricky part of all this code was handling all the weird cases, such as what if the bitmap is larger than the button and the caller has specified a right-justified image? (Answer: the image is truncated on the left). You can read the code to discover all the other strange cases.

To create an image button, you create a button in the dialog editor, and mark it as owner-draw. Any caption you supply will be ignored. You then go into the Class Wizard and create a control variable for it, of type CImageButton. Note that you may have to rebuild the .clw file after adding CImageButton to your project. Delete the .clw file and invoke ClassWizard, then tell it to reconstruct the file from the existing project files.

CImageButton Methods

What I provide in my CImageBitmap class is a simple set of methods that extended the basic CButton class.

void CImageBitmap::LoadBitmaps(UINT up, UINT down = 0, UINT disabled = 0)

You call this method to tell the button what images to use. Only the up image is required. The rest are optional, and are supplied implicitly at drawing time according to the table below.

Bitmaps provided

Bitmaps used

-- LoadBitmaps parameters--

-- Button state --

void CImageBitmap::GetBitmaps(UINT &up, 
                              UINT &down, 
                              UINT &disabled)

This can be used to retrieve the resource IDs of the bitmaps set by the last LoadBitmaps call.

DWORD CImageButton::SetVPos(DWORD style)
This can be used to set the vertical position of the image. It can be one of BS_TOP, BS_BOTTOM, or BS_VCENTER. It returns the previous vertical style.

DWORD CImageButton::SetHPos(DWORD style)
This can be used to set the horizontal position of the image. It can be one of BS_LEFT, BS_RIGHT, or BS_CENTER. It returns the previous horizontal style.

BOOL CImageButton::m_fill;
This value is set to FALSE by the constructor. You can set it explicitly to TRUE if you want image-filling to occur.

Note that if you change the value of this variable after the OnInitDialog has completed, you should call InvaldiateRect on the CImageButton object to force it to redraw.

(Note: you may have read my diatribes about the silly m_ notation being used gratuitously; I've even seen people so misled by it that they declare local variables on the stack using this naming convention! But the one place I use it is to name member variables that are used to communicate information into a class instance from outside the class. This is one such instance.)

CToggleButton: A toggle button class

Just because there were several requests for how to make a button that "toggles", I've created a toggle bitmap class, which also downloads with this project. Look for the CToggleButton class in ToggleButton.cpp and ToggleButton.h. To create a toggle button, you create a button in the dialog editor, add a caption, and mark it as owner-draw. You then go into the Class Wizard and create a control variable for it, of type CToggleButton. Note that you may have to rebuild the .clw file after adding CToggleButton to your project. Delete the .clw file and invoke ClassWizard, then tell it to reconstruct the file from the existing project files.

CToggleButton methods

BOOL CToggleButton::GetState()
This returns the current button-state, TRUE if the button is toggled on (image is depressed) and FALSE if the button is toggled off (image is not depressed).

BOOL CToggleButton::SetState(BOOL newState)
This sets the state. If newState is TRUE the state is "on" (depressed) and if newState is FALSE the state is "off" (not depressed). This returns the previous state of the button.

DWORD CToggleButton::SetVPos(DWORD style)
This can be used to set the vertical position of the text. It can be one of BS_TOP, BS_BOTTOM, or BS_VCENTER. It returns the previous vertical style.

DWORD CToggleButton::SetHPos(DWORD style)
This can be used to set the horizontal position of the text. It can be one of BS_LEFT, BS_RIGHT, or BS_CENTER. It returns the previous horizontal style.

The source file for these classes, and a project that I used for testing them, can be downloaded directly by clicking the link at the top of the page.

Updates by Jeff Malett

  1. The CImageButton class was extended to support toggling, so now you can have an image button that toggles. Basically, I did this by merging code from your CToggleButton class into your CImageButton class, calling the result CImageToggleButton. Whether or not the button toggles is controlled by a new class member variable called m_toggle, which can be changed at any time. Merging the two classes was a requested feature from somebody on the codeproject message board.
  2. I changed both CImageToggleButton and CToggleButton so that when they are toggled their backgrounds are drawn with the currently set button highlight color. This is the more standard behavior for toggle buttons.
  3. I extended the dialog demo to show two image buttons, one with toggling and one without.
  4. I extended the dialog demo to show an MFC text toggle button (you can do this by making a checkbox and giving it the "Push-like" property) side-by-side with the CToggleButton. Note that they behave slightly differently, e.g. when you click on a depressed toggle button, it is fully released on mouse-down in your version and on mouse-up in MFC's. I kind of prefer the UI of the MFC version, but CToggleButton is superior in that the text can be justified in different ways (not sure why you'd want to do that though.)

The views expressed in these essays are those of the author, and in no way represent, nor are they endorsed by, Microsoft.

Send mail to with questions or comments about this article.
Copyright © 1999. All Rights Reserved


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

Joseph M. Newcomer
United States United States
PhD, Computer Science, Carnegie Mellon University, 1975
Certificate in Forensic Science and the Law, Duquesne University, 2008

Co-Author, [i]Win32 Programming[/i]

You may also be interested in...


Comments and Discussions

Questionexporting data Pin
stavros11-Dec-07 0:11
memberstavros11-Dec-07 0:11 
Questionexporting data Pin
stavros11-Dec-07 0:10
memberstavros11-Dec-07 0:10 
Questionexporting data form a picturebox Pin
stavros11-Dec-07 0:09
memberstavros11-Dec-07 0:09 
QuestionDefault focus Pin
greekgoddj14-May-07 2:14
membergreekgoddj14-May-07 2:14 
AnswerRe: Default focus Pin
Joseph M. Newcomer14-May-07 3:15
memberJoseph M. Newcomer14-May-07 3:15 
GeneralRe: Default focus Pin
greekgoddj14-May-07 5:18
membergreekgoddj14-May-07 5:18 
Generalscreen DPI Pin
Softydevel28-Nov-06 8:44
memberSoftydevel28-Nov-06 8:44 
Generalxp style Pin
locoone22-Jun-06 13:59
memberlocoone22-Jun-06 13:59 
GeneralThank you so much ! Pin
nguyen_nd14-Apr-06 3:11
membernguyen_nd14-Apr-06 3:11 
GeneralChicken and egg problem... Pin
blackbox7914-Jan-06 17:21
memberblackbox7914-Jan-06 17:21 

I followed your advice and took a shot at OnDrawItem within by my button sub-class:

<br />
void Button_Class::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)<br />
{<br />
switch(nIDCtl)<br />
{<br />
case IDC_BTN_0:<br />
{<br />
...load image masks for button 0<br />
}<br />
<br />
case IDC_BTN_1:<br />
{<br />
...load image masks for button 1<br />
}<br />
<br />
case IDC_BTN_2:<br />
{<br />
...load image masks for button 2<br />
}<br />
<br />
default: return;<br />
}<br />

Easy enough, right? Well, here's the catch... and it's not what you think. I decided to take the ActiveX approach for owner-drawn. And since I'm compiling this project as an OCX library, the compiler complains about the above mentioned case statement.

Since I make such resource ID's definitions in my dialog code, the OCX library does not yet know 'IDC_BTN_0' exists. So... here's my question...

Is there anyway I can suppress these compiler messages for my case statement? I don't see any harm in doing this since I have a default case.


-- modified at 22:23 Saturday 14th January, 2006
GeneralRe: Chicken and egg problem... Pin
Joseph M. Newcomer14-Jan-06 18:56
memberJoseph M. Newcomer14-Jan-06 18:56 
GeneralRe: Chicken and egg problem... Pin
blackbox7915-Jan-06 9:07
memberblackbox7915-Jan-06 9:07 
GeneralRe: Chicken and egg problem... Pin
blackbox7916-Jan-06 9:46
memberblackbox7916-Jan-06 9:46 
QuestionHow to catch OnKillFocus Event ? Pin
_tomtom_12-Apr-05 0:17
member_tomtom_12-Apr-05 0:17 
AnswerRe: How to catch OnKillFocus Event ? Pin
_tomtom_13-Apr-05 22:05
member_tomtom_13-Apr-05 22:05 
GeneralRe: How to catch OnKillFocus Event ? Pin
Joseph M. Newcomer14-Apr-05 5:10
memberJoseph M. Newcomer14-Apr-05 5:10 
GeneralRe: How to catch OnKillFocus Event ? Pin
_tomtom_14-Apr-05 9:48
member_tomtom_14-Apr-05 9:48 
QuestionRe: How to catch OnKillFocus Event ? Pin
blackbox7911-Jan-06 15:07
memberblackbox7911-Jan-06 15:07 
GeneralReading bmp file Pin
Arrun4-Nov-04 19:06
sussArrun4-Nov-04 19:06 
GeneralReading bmp file Pin
Arrun4-Nov-04 19:06
sussArrun4-Nov-04 19:06 
GeneralReading a bmp file Pin
Arrun3-Nov-04 21:27
sussArrun3-Nov-04 21:27 
GeneralRe: Reading a bmp file Pin
Joseph M. Newcomer3-Nov-04 22:36
memberJoseph M. Newcomer3-Nov-04 22:36 
GeneralRe: Reading a bmp file Pin
Arrun4-Nov-04 18:45
sussArrun4-Nov-04 18:45 
GeneralRe: Reading a bmp file Pin
Joseph M. Newcomer4-Nov-04 19:39
memberJoseph M. Newcomer4-Nov-04 19:39 
QuestionDrawItem method? Pin
Gene Sewell22-Mar-04 9:40
sussGene Sewell22-Mar-04 9:40 
AnswerRe: DrawItem method? Pin
Joseph M. Newcomer22-Mar-04 9:48
memberJoseph M. Newcomer22-Mar-04 9:48 
GeneralRe: DrawItem method? Pin
Gene Sewell22-Mar-04 10:13
sussGene Sewell22-Mar-04 10:13 
GeneralRe: DrawItem method? Pin
iknowindigo25-Mar-04 9:36
memberiknowindigo25-Mar-04 9:36 
GeneralBehavior question Pin
jnelso9918-Feb-04 8:59
memberjnelso9918-Feb-04 8:59 
GeneralRe: Behavior question Pin
Joseph M. Newcomer18-Feb-04 9:10
memberJoseph M. Newcomer18-Feb-04 9:10 
GeneralRe: Behavior question Pin
jnelso9918-Feb-04 16:37
memberjnelso9918-Feb-04 16:37 
GeneralRe: Behavior question Pin
starecho15-Jul-08 22:07
memberstarecho15-Jul-08 22:07 
GeneralToggle button Pin
Chris Hills1-Feb-04 8:06
memberChris Hills1-Feb-04 8:06 
GeneralRe: Toggle button Pin
Joseph M. Newcomer1-Feb-04 19:28
memberJoseph M. Newcomer1-Feb-04 19:28 
GeneralRe: Toggle button Pin
Chris Hills3-Nov-05 4:08
memberChris Hills3-Nov-05 4:08 
QuestionCan i use your code please? Pin
chaim19-Nov-03 7:55
susschaim19-Nov-03 7:55 
GeneralToggle Button text problems Pin
Hallr15-Oct-03 15:21
memberHallr15-Oct-03 15:21 
GeneralRe: Toggle Button text problems Pin
conrad Braam18-Feb-04 18:57
memberconrad Braam18-Feb-04 18:57 
GeneralRe: Toggle Button text problems Pin
Joseph M. Newcomer18-Feb-04 19:21
memberJoseph M. Newcomer18-Feb-04 19:21 
GeneralA better way to gray out the image if no disabled image is supplied Pin
Mario Orlandi12-Jun-03 23:03
memberMario Orlandi12-Jun-03 23:03 
GeneralRe: A better way to gray out the image if no disabled image is supplied Pin
Joseph M. Newcomer13-Jun-03 5:01
memberJoseph M. Newcomer13-Jun-03 5:01 
GeneralRe: A better way to gray out the image if no disabled image is supplied Pin
Mario Orlandi13-Jun-03 6:33
memberMario Orlandi13-Jun-03 6:33 
GeneralResource leak Pin
Jon14-Jul-02 23:53
memberJon14-Jul-02 23:53 
GeneralProblem with MFC extension DLL (AFXDLL) project Pin
Blair13-May-02 12:11
memberBlair13-May-02 12:11 
GeneralRe: Problem with MFC extension DLL (AFXDLL) project Pin
Joseph M. Newcomer13-May-02 14:38
memberJoseph M. Newcomer13-May-02 14:38 
GeneralRe: Problem with MFC extension DLL (AFXDLL) project Pin
Anonymous14-May-02 5:07
memberAnonymous14-May-02 5:07 
GeneralComments on this job Pin
Bliven5-Nov-01 1:53
memberBliven5-Nov-01 1:53 
GeneralRe: Comments on this job Pin
suger20-Dec-01 22:27
membersuger20-Dec-01 22:27 
GeneralSlow Response Pin
Anonymous22-Oct-01 18:24
memberAnonymous22-Oct-01 18:24 
GeneralRe: Slow Response Pin
Anonymous14-Feb-02 23:14
memberAnonymous14-Feb-02 23:14 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 15 Oct 2001
Article Copyright 2000 by Joseph M. Newcomer
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid