11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4. Button<br />

LPCTSTR lpszBitmapResourceFocus=NULL,<br />

LPCTSTR lpszBitmapResourceDisabled=NULL,<br />

LPCTSTR lpszBitmapResourceMask=NULL<br />

);<br />

BOOL LoadBitmaps<br />

(<br />

UINT nIDBitmapResource,<br />

UINT nIDBitmapResourceSel=0,<br />

UINT nIDBitmapResourceFocus=0,<br />

UINT nIDBitmapResourceDisabled=0,<br />

UINT nIDBitmapResourceMask=0<br />

);<br />

BOOL AutoLoad(UINT nID, CWnd* pParent);<br />

protected:<br />

CBitmap m_bitmapMask;<br />

virtual void DrawItem(LPDRAWITEMSTRUCT lpDIS);<br />

DECLARE_DYNAMIC(MCBitmapButton)<br />

DECLARE_MESSAGE_MAP()<br />

};<br />

Function LoadBitmaps(…) has two versions, one is used to load bitmaps with string IDs, the other is<br />

used to load bitmaps with integer IDs. Both functions have five parameters.<br />

Overriding Function CBitmapButton::LoadBitmaps(…)<br />

When overriding functions, we can utilize the features implemented by the base class to load standard<br />

four bitmaps, and add our own code to load the mask bitmap. The following is the implementation of<br />

function MCBitmapButton::LoadBitmaps(…):<br />

BOOL MCBitmapButton::LoadBitmaps<br />

(<br />

LPCTSTR lpszBitmapResource,<br />

LPCTSTR lpszBitmapResourceSel,<br />

LPCTSTR lpszBitmapResourceFocus,<br />

LPCTSTR lpszBitmapResourceDisabled,<br />

LPCTSTR lpszBitmapResourceMask<br />

)<br />

{<br />

BOOL bAllLoaded;<br />

}<br />

m_bitmapMask.DeleteObject();<br />

bAllLoaded=CBitmapButton::LoadBitmaps<br />

(<br />

lpszBitmapResource,<br />

lpszBitmapResourceSel,<br />

lpszBitmapResourceFocus,<br />

lpszBitmapResourceDisabled<br />

);<br />

if(lpszBitmapResourceMask != NULL)<br />

{<br />

if(!m_bitmapMask.LoadBitmap(lpszBitmapResourceMask))<br />

{<br />

TRACE0(“Failed to load bitmap for normal background image.\n”);<br />

bAllLoaded=FALSE;<br />

}<br />

}<br />

return bAllLoaded;<br />

First we must use variable m_bitmapMask to call function CBitmap::DeleteObject(), which is<br />

inherited from class CGdiObject. Since bitmap is a GDI (graphics device interface) object, once it is<br />

initialized, it will allocate some memory. If we want to initialize it again, we must first release the<br />

previously allocated memory. Function CGdiObject::DeleteObject() can be used for this purpose.<br />

Next we call function CBitmapButton::LoadBitmaps(…) to load the default four bitmaps, and see if<br />

the mask bitmap is available. If so, we use m_bitmapMask to load the mask bitmap.<br />

80

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!