Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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


private:
CBitmapButton btnBmpOK, btnBmpCancel, btnBmpHelp;
};


  1. Use the constructor to load the bitmaps of each button:


CCompanySheet::CCompanySheet(UINT nIDCaption, CWnd* pParentWnd, UINT
iSelectPage)
:CPropertySheet(nIDCaption, pParentWnd, iSelectPage)
{
m_psh.dwFlags |= PSH_NOAPPLYNOW;
AddPage(&Forms);
AddPage(&Projects);

btnBmpOK.LoadBitmaps(IDB_OK_DEF, IDB_OK_SEL,
IDB_OK_FOC, IDB_OK_DIS);
btnBmpCancel.LoadBitmaps(IDB_CANCEL_NRM, IDB_CANCEL_SEL,
IDB_CANCEL_FOC, IDB_CANCEL_DIS);
btnBmpHelp.LoadBitmaps(IDB_HELP_NRM, IDB_HELP_SEL,
IDB_HELP_FOC, IDB_HELP_DIS);
}


  1. To customize the existing buttons, implement the OnInitDialog event of the
    CCompanySheet class as follows (most of the code is used because the buttons were
    already created by default; this means that we have to manually change their style):


BOOL CCompanySheet::OnInitDialog()
{
BOOL bResult = CPropertySheet::OnInitDialog();

// TODO: Add your specialized code here
// We need a handle to each
CButton *btnOK, *btnCancel, *btnHelp;

// Get a handle to each of the existing buttons
btnOK = reinterpret_cast<CButton *>(GetDlgItem(IDOK));
btnCancel = reinterpret_cast<CButton *>(GetDlgItem(IDCANCEL));
btnHelp = reinterpret_cast<CButton *>(GetDlgItem(IDHELP));

// Get the style of the button(s)
LONG GWLOK = GetWindowLong(btnOK->m_hWnd, GWL_STYLE);
LONG GWLCancel = GetWindowLong(btnCancel->m_hWnd, GWL_STYLE);
LONG GWLHelp = GetWindowLong(btnHelp->m_hWnd, GWL_STYLE);

// Change the button's style to BS_OWNERDRAW
SetWindowLong(btnOK->m_hWnd, GWL_STYLE, GWLOK | BS_OWNERDRAW);
SetWindowLong(btnCancel->m_hWnd, GWL_STYLE, GWLCancel |
BS_OWNERDRAW);
SetWindowLong(btnHelp->m_hWnd, GWL_STYLE, GWLHelp |
BS_OWNERDRAW);

// Subclass each button
btnBmpOK.SubclassDlgItem(IDOK, this);
btnBmpCancel.SubclassDlgItem(IDCANCEL, this);
btnBmpHelp.SubclassDlgItem(IDHELP, this);

return bResult;
}


  1. Test the application:

Free download pdf