Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 16: Text-Based Controls Visual C++ and MFC Fundamentals


15.6.1..Overview...............................................................................................


A bitmap button is a button that display a picture or a picture and text on its face. This is
usually intended to make the button a little explicit. There are two ways you can create a
bitmap button: with or without an existing resource identifier.

A bitmap button is created using the CBitmapButton class, which is derived from
CButton, which in turn is a descendent of the CWnd class.

Because the bitmap button is in fact a customized version of a button, you must first
declare a variable or pointer of it:

CBitmapButton *btnMap = new CBitmapButton;

Using the variable or pointer, you can call the Create() method to formally create the
button. Here is an example:

BOOL CDialog6Dlg::OnInitDialog()
{
CDialog::OnInitDialog();

// TODO: Add extra initialization here
CBitmapButton *btnMap = new CBitmapButton;

btnMap->Create(NULL, WS_CHILD | WS_VISIBLE,
CRect(10,10,100,100), this, IDC_BTN_NEW);

return TRUE; // return TRUE unless you set the focus to a control
}

15.6.2..Bitmap Button Implementation.........................................................


Creating a bitmap button without an identifier is considered creating it from scratch.
Creating a bitmap button using an existing identifier is more practical because you can
easily use the messages of such a button and refer to it in code. Therefore, before creating
a bitmap button, you can first add a button to a dialog box and specify its identifier. For a
button to display a picture, it must have the BS_OWNERDRAW style. This means that
you should check or set to True the Owner Draw option.

Visually, the most appealing aspect of a bitmap button is the picture it displays. The
bitmap can be designed following the same tools and instructions we reviewed for the
icons. To give a 3-D or realistic appearance to the button, you should mimic the focus
effect when designing the bitmaps.

Therefore, you should create one to four bitmaps for the control. Here is how they would
work:

?? If you create one bitmap, it would be used for all clicks of the button. Since the
button would always appear the same, you should avoid using only one picture
?? If you create two bitmaps, the first would display as the regular picture for the
button. That is, when the button is not being clicked. The second would be used
when the button is clicked and the mouse is down on the button
Free download pdf