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 2. Menu<br />

CDC *ptrDC;<br />

CDC dcMem;<br />

CBitmap *ptrBmpOld;<br />

CBitmap *ptrBmp;<br />

CRect rect;<br />

if(!(lpDrawItemStruct->CtlType & ODT_MENU))<br />

{<br />

CMenu::DrawItem(lpDrawItemStruct);<br />

return;<br />

}<br />

ptrDC=CDC::FromHandle(lpDrawItemStruct->hDC);<br />

dcMem.CreateCompatibleDC(ptrDC);<br />

if(lpDrawItemStruct->itemState & ODS_SELECTED)<br />

{<br />

switch(lpDrawItemStruct->itemData)<br />

{<br />

case MENUTYPE_SMILE:<br />

{<br />

ptrBmp=&m_bmpSmileSel;<br />

break;<br />

}<br />

case MENUTYPE_QUESTION:<br />

{<br />

ptrBmp=&m_bmpQuestionSel;<br />

break;<br />

}<br />

}<br />

}<br />

else<br />

{<br />

switch(lpDrawItemStruct->itemData)<br />

{<br />

case MENUTYPE_SMILE:<br />

{<br />

ptrBmp=&m_bmpSmile;<br />

break;<br />

}<br />

case MENUTYPE_QUESTION:<br />

{<br />

ptrBmp=&m_bmpQuestion;<br />

break;<br />

}<br />

}<br />

}<br />

}<br />

ptrBmpOld=dcMem.SelectObject(ptrBmp);<br />

rect=lpDrawItemStruct->rcItem;<br />

ptrDC->BitBlt<br />

(<br />

rect.left,<br />

rect.top,<br />

rect.Width(),<br />

rect.Height(),<br />

&dcMem,<br />

0,<br />

0,<br />

SRCCOPY<br />

);<br />

dcMem.SelectObject(ptrBmpOld);<br />

First, we check if the item is a menu. If not, we call the same function implemented by the base class<br />

and return. If so, first we obtain a CDC type pointer to the target device by calling function<br />

CDC::FromHandle(…). Then, we create a compatible memory DC with target DC, which will be used to<br />

53

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

Saved successfully!

Ooh no, something went wrong!