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 />

……<br />

CRect rect;<br />

Three DCs are declared here. To draw a bitmap, we must create a memory DC, select the bitmap into it<br />

and copy the bitmap from the memory DC to the target DC. The target could be either a device or a<br />

memory block (we could copy bitmap between two memory DCs). A DC can select only one bitmap at any<br />

time.<br />

When there is no mask bitmap, variable memDC is used to perform normal bitmap drawing: it is used to<br />

select the normal bitmap, and copy it directly to the target DC. When there is a mask bitmap, memDC will be<br />

used along with memDCMask to implement transparent background drawing.<br />

Variable memDCImage is used to act as the target memory DC and implement transparent background<br />

drawing. It will be used in conjunction with bmpImage, which will be selected into memDCImage. To draw the<br />

bitmap, first we need to copy the image pattern from the target device to the memory bitmap, then copy the<br />

source bitmap to the memory bitmap (perform AND and XOR drawings). Finally, we can output the result<br />

from the memory bitmap to the target device.<br />

Variable bmpImage is used to create bitmap in memory.<br />

Variable memDCMask is used to select mask bitmap image.<br />

Pointer pDC will be used to store the pointer of the target device context that is created from hDC<br />

member of structure DRAWITEMSTRUCT.<br />

Pointers pBitmap and pBitmapMask will be used to store the pointers to the normal bitmap (could be<br />

one of the bitmaps indicating the four states of the button) and the mask bitmap respectively.<br />

The other three CBitmap pointers pOld, pOldMask and pOldImage are used to select the bitmaps out of<br />

the DCs (When the bitmaps are being selected into the DCs, these pointers are used to store the bitmaps<br />

selected out of the DCs. After bitmap drawing is finished, we can select old bitmaps back into the DCs, this<br />

will select our bitmaps out of the DCs automatically).<br />

Variable state is used to store the current state of button.<br />

The following portion of function MCBitmapButton::DrawItem(…) shows how to choose appropriate<br />

bitmaps:<br />

……<br />

ASSERT(lpDIS != NULL);<br />

ASSERT(m_bitmap.m_hObject != NULL);<br />

pBitmap=&m_bitmap;<br />

if(m_bitmapMask.m_hObject != NULL)<br />

{<br />

pBitmapMask=&m_bitmapMask;<br />

}<br />

else pBitmapMask=NULL;<br />

state=lpDIS->itemState;<br />

if<br />

(<br />

(state & ODS_SELECTED) &&<br />

(m_bitmapSel.m_hObject != NULL)<br />

)<br />

{<br />

pBitmap=&m_bitmapSel;<br />

}<br />

else if<br />

(<br />

(state & ODS_FOCUS) &&<br />

(m_bitmapFocus.m_hObject != NULL)<br />

)<br />

{<br />

pBitmap=&m_bitmapFocus;<br />

}<br />

else if<br />

(<br />

(state & ODS_DISABLED) &&<br />

(m_bitmapDisabled.m_hObject != NULL)<br />

)<br />

{<br />

pBitmap=&m_bitmapDisabled;<br />

}<br />

82

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

Saved successfully!

Ooh no, something went wrong!