Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 15: Fundamental Controls


?? If you create three bitmaps, the first would display when the button is not being
accessed and another control has focus. The second picture would display when
the user is clicking the button and as long as the mouse is down on the button.
The third would be used when the button has focus but it is not being used
?? If you create four bitmaps, the first would display when the button is not being
accessed and another control has focus. The second picture would display when
the user is clicking the button and as long as the mouse is down on the button.
The third would be used when the button has focus but it is not being used or
pressed. The fourth would be used when the button is disabled
With these options, to use a bitmap button to its fullest, you should strive to provide four
bitmaps. After creating the bitmaps, you can load them into the button. This is done using
the LoadBitmaps() method. It comes in two versions as follows:

BOOL LoadBitmaps(LPCTSTR lpszBitmapResource,
LPCTSTR lpszBitmapResourceSel = NULL,
LPCTSTR lpszBitmapResourceFocus = NULL,
LPCTSTR lpszBitmapResourceDisabled = NULL );
BOOL LoadBitmaps(UINT nIDBitmapResource,
UINT nIDBitmapResourceSel = 0,
UINT nIDBitmapResourceFocus = 0,
UINT nIDBitmapResourceDisabled = 0 );

Each version takes four arguments. The first version uses the bitmaps as string resources
while the second version uses resource identifiers.

As stated above, you can use one to four bitmaps. The first argument must specify the
first or default bitmap and is required. The second argument is the name or identifier of
the bitmap that would be used when the button is selected. The third is used when the
button has focus. The last bitmap is used when the button is disabled. 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|BS_OWNERDRAW,
CRect(10,10,100,100), this, IDC_BTN_NEW);
btnMap->LoadBitmaps(IDB_BMP_BTN1, IDB_BMP_BTN2, IDB_BMP_BTN3,
IDB_BMP_BTN4);

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

It is very likely that you may not be able to design the button on one hand and the
bitmaps on the other hand at exactly the same dimensions. To adjust these measures, the
CBitmapButton class is equipped with the SizeToContent() method. Its syntax is:

void SizeToContent();

When this method is called on the button, it resizes it to the size of the bitmaps. If one
bitmap is larger and/or taller than the others, and if you had loaded more than one, this
method would resize the button to the larger or largest and taller or tallest bitmap. For
Free download pdf