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.
-
Create an MFC Dialog based application or a normal SDI or MDI MFC application
with a dialog box.
-
Place a Command Button on the dialog. This sample
assumes the id of the command button as
ID_BITMAP_BUTTON_SAMPLE.
-
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.
-
Create a bitmap resource using the Menu -->
Insert --> Resource --> Bitmap.
-
Open the Class Wizard by pressing Ctrl + W key
combination or Menu --> View --> ClassWizard.
-
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;
-
In the SampleDialog.H file, modify the CButton
m_bmpSample declaration as CBitmapButton
m_bmpSample.
-
In the OnInitDialog function call the LoadBitmaps
function, a member of CBitmapButton.
m_bmpSample.LoadBitmaps(IDB_BITMAP1);
- The function can accommodate upto 4 bitmaps depending
on the state of the command button.
- Build and run the application. The bitmap will be loaded
on the command button.