Bitmaps on Command Button using CBitmapButton

CBitmapButton class provided by MFC can be used to place bitmaps on command buttons. It allows up to four bitmaps to be placed on the command button depending on the button’s state viz., Normal, Selected, Focused and Disabled.

Bitmap Button on a Dialog – CBitmapButton:

The following steps explain how to place a bitmap button on the dialog using CBitmapButton.

  1. Create an MFC Dialog based application or a normal SDI or MDI MFC application with a dialog box.
  2. Place a Command Button on the dialog. This sample assumes the id of the command button as ID_BITMAP_BUTTON_SAMPLE.
  3. In the Properties dialog –> Styles tab, set the Owner Draw style to TRUE. i.e., keep the check box in checked state. This enables Windows to send WM_DRAWITEM and WM_MEASUREITEM messages for the bitmap button.
  4. Create a bitmap resource using the Menu –> Insert  –> Resource –> Bitmap.
  5. Open the Class Wizard by pressing Ctrl + W key combination or Menu –> View –> ClassWizard.
  6. In the Member Variables tab, Add a member for ID_BITMAP_BUTTON_SAMPLE with the control class as CButton. The sample assumes the variable name as m_bmpSample;
  7. In the SampleDialog.H file, modify the CButton m_bmpSample declaration as CBitmapButton m_bmpSample.
  8. In the OnInitDialog function call the LoadBitmaps function, a member of CBitmapButton.m_bmpSample.LoadBitmaps(IDB_BITMAP1);
  9. The function can accommodate upto 4 bitmaps depending on the state of the command button.
  10. Build and run the application. The bitmap will be loaded on the command button.