Putting Bitmaps on Pushbuttons
The MFC library makes it easy to display a bitmap (instead of text) on a pushbutton. If you were to program this from scratch, you would set the Owner Draw property for your button and then write a message handler in your dialog class that would paint a bitmap on the button control's window. If you use the MFC CBitmapButton class instead, you end up doing a lot less work, but you have to follow a kind of "cookbook" procedure. Don't worry too much about how it all works (but be glad that you don't have to write much code!). To make a long story short, you lay out your dialog resource as usual, with unique text captions for the buttons you designate for bitmaps. Next, you add some bitmap resources to your project, and you identify those resources by name rather than by numeric ID. Finally, you add some CBitmapButton data members to your dialog class, and you call the AutoLoad member function for each one, which matches a bitmap name to a button caption. If the button caption is Copy, you add two bitmaps: COPYU for the up state and COPYD for the down state. By the way, you must still set the button's Owner Draw property. (This will all make more sense when you write a program).
Note | If you look at the MFC source code for the CBitmapButton class, you'll see that the bitmap is an ordinary GDI bitmap painted with a BitBlt call. Thus, you can't expect any palette support. That's rarely a problem because bitmaps for buttons are usually 16-color bitmaps that depend on standard VGA colors. |
The Ex06e Example
Ex06e shows how to show different bitmaps on a pushbutton. Here are the steps for building Ex06e:
Run the MFC Application Wizard to produce the Ex06e project. Accept all the defaults but two: Select Single Document and deselect Printing And Print Preview.
Modify the project's IDD_ABOUTBOX dialog resource in Resource View. We'll use the About dialog box that the MFC Application Wizard generates for hosting the bitmap button. Add three pushbuttons with captions, as shown below, accepting the default IDs IDC_BUTTON1, IDC_BUTTON2, and IDC_BUTTON3. The size of the buttons isn't important because the framework adjusts the button size at run time to match the bitmap size.

Set the Owner Draw property to True for all three buttons.
Import




Resource Name | Original File | Invert Colors |
---|---|---|
"COPYU" | ![]() | No |
"COPYD" | ![]() | Yes |
"CUTU" | ![]() | No |
"CUTD" | ![]() | Yes |
"PASTEU" | ![]() | No |
"PASTED" | ![]() | Yes |
Edit the code for the CAboutDlg class. Both the declaration and the implementation for this class are contained in the

CBitmapButton m_editCopy;
CBitmapButton m_editCut;
CBitmapButton m_editPaste;
Then use the code wizards available from the Properties window to override the OnInitDialog virtual function. This function is coded as follows:
BOOLCAboutDlg::OnInitDialog() {
CDialog::OnInitDialog();
VERIFY(m_editCopy.AutoLoad(IDC_BUTTON1, this));
VERIFY(m_editCut.AutoLoad(IDC_BUTTON2, this));
VERIFY(m_editPaste.AutoLoad(IDC_BUTTON3, this));
return TRUE;
//return TRUE unless you set the focus to a control
//EXCEPTION: OCX Property Pages should return FALSE
}
The AutoLoad function connects each button with the two matching resources. The VERIFY macro is an MFC diagnostic aid that displays a message box if you didn't code the bitmap names correctly.
Edit the OnDraw function in

pDC->TextOut(0, 0, "Choose About from the Help menu.");
Build and test the application. When the program starts, choose About from the Help menu and observe the button behavior. The following image shows the CUT button in the down state.

Note that bitmap buttons send BN_CLICKED notification messages just as ordinary buttons do. The code wizards available from the Properties window can, of course, map those messages in your dialog class.
Going Further with Bitmap Buttons
You've seen bitmaps for the buttons' up and down states. The CBitmapButton class also supports bitmaps for the focused and disabled states. For the Copy button, the focused bitmap name would be COPYF, and the disabled bitmap name would be COPYX. If you want to test the disabled option, make a COPYX bitmap, possibly with a red line through it, and then add the following line to your program:
m_editCopy.EnableWindow(FALSE);