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

First pBitmap is assigned the address of variable m_bitmap, which holds the default bitmap. Then we<br />

check if the mask bitmap exists, if so, we assign its address to pBitmapMask. The current state of the button<br />

is read into variable state, whose ODS_SELECTED, ODS_FOCUS and ODS_DISABLED bits are examined in turn.<br />

If any of them is set, the corresponding bitmap’s address will be stored in pBitmap.<br />

The following portion of this function creates the memory DCs and selects relative bitmaps into<br />

different DCs:<br />

……<br />

……<br />

pDC=CDC::FromHandle(lpDIS->hDC);<br />

memDC.CreateCompatibleDC(pDC);<br />

if(pBitmapMask != NULL)<br />

{<br />

memDCMask.CreateCompatibleDC(pDC);<br />

memDCImage.CreateCompatibleDC(pDC);<br />

}<br />

pOld=memDC.SelectObject(pBitmap);<br />

if(pBitmapMask != NULL)<br />

{<br />

pOldMask=memDCMask.SelectObject(pBitmapMask);<br />

pBitmap->GetBitmap(&bm);<br />

bmpImage.CreateCompatibleBitmap(pDC, bm.bmWidth, bm.bmHeight);<br />

pOldImage=memDCImage.SelectObject(&bmpImage);<br />

}<br />

First, the address of the target DC is obtained from the DC handle by calling function CDC::<br />

FromHandle(…). Then the memory DC that will select source image is created by calling function<br />

CDC::CreateCompatibleDC(…). Since the bitmap could be copied only between compatible DCs, each time<br />

we create a memory DC, we need to make sure that it is compatible with the target DC. Next, if the mask<br />

bitmap exists, we create three DCs: memDC for normal bitmap, memDCMask for mask bitmap and memDCImage<br />

for memory target bitmap (It will act as temparory target device DC). In this case, we also create a memory<br />

bitmap using variable bmpImage, which is selected into memDCImage (This bitmap must also be compatible<br />

with the DC that will select it). In the above implementation, we call function CBitmap::GetBitmap(…) to<br />

obtain the dimension information of a bitmap and call function CBitmap::CreateCompatibleBitmap(…) to<br />

create compatible memory bitmap). The mask bitmap is selected into memDCMask. The normal bitmap is<br />

always selected into memDC.<br />

The following portion of function MCBitmapButton::DrawItem(…) draws the bitmap by copying<br />

normal and mask bitmaps among different DCs:<br />

……<br />

rect.CopyRect(&lpDIS->rcItem);<br />

if(pBitmapMask == NULL)<br />

{<br />

pDC->BitBlt<br />

(<br />

rect.left,<br />

rect.top,<br />

rect.Width(),<br />

rect.Height(),<br />

&memDC,<br />

0,<br />

0,<br />

SRCCOPY<br />

);<br />

}<br />

else<br />

{<br />

memDCImage.BitBlt<br />

(<br />

0,<br />

0,<br />

rect.Width(),<br />

rect.Height(),<br />

83

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

Saved successfully!

Ooh no, something went wrong!