Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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


this reason, always make sure that you design bitmaps that have the same dimension.
Here is an example of using this method:

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);
btnMap->SizeToContent();

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

As you may realize, if you create a bitmap button strictly using this approach, it may not
work. The suggestion is to "subclass" your button class so the messages sent to the
bitmap button would be applied effectively. The function used to do this is the
CWnd::SubclassDlgItem() method and its syntax is:

BOOL SubclassDlgItem( UINT nID, CWnd* pParent );

The first argument is the resource ID of the existing button. The second argument is the
control that is hosting the button; this is usually the dialog box but it can be another
control container.

Practical Learning: Creating Bitmap Buttons



  1. Continue from the Associates application.
    To add a help button, in the constructor of the CCompanyForms class, add the
    PSP_HASHELP flag as follows:


CCompanyForms::CCompanyForms()
: CPropertyPage(CCompanyForms::IDD)
{
m_psp.dwFlags |= PSP_HASHELP;
}


  1. From the resources that accompany this book, import the following bitmaps and
    change their IDs as follows


File ID File ID
canceldis.bmp IDB_CANCEL_DIS cancelfoc.bmp IDB_CANCEL_FOC
cancelnrm.bmp IDB_CANCEL_NRM cancelsel.bmp IDB_CANCEL_SEL
helpdis.bmp IDB_HELP_DIS helpfoc.bmp IDB_HELP_FOC
helpnrm.bmp IDB_HELP_NRM helpsel.bmp IDB_HELP_SEL
okdef.bmp IDB_OK_DEF okdis.bmp IDB_OK_DIS
okfoc.bmp IDB_OK_FOC oksel.bmp IDB_OK_SEL


  1. In the header of the CCompanySheet class, declare three CBitmapButton variables as
    follows:

Free download pdf