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 10. Bitmap<br />

}<br />

}<br />

if(m_pBmpMaskOld != NULL)<br />

{<br />

m_dcMemMask.SelectObject(m_pBmpMaskOld);<br />

m_pBmpMaskOld=NULL;<br />

}<br />

if(m_pBmpBSOld != NULL)<br />

{<br />

m_dcMemBS.SelectObject(m_pBmpBSOld);<br />

m_pBmpBSOld=NULL;<br />

}<br />

if(m_dcMem.GetSafeHdc() != NULL)m_dcMem.DeleteDC();<br />

if(m_dcMemMask.GetSafeHdc() != NULL)m_dcMemMask.DeleteDC();<br />

if(m_dcMemBS.GetSafeHdc() != NULL)m_dcMemBS.DeleteDC();<br />

if(m_bmpDraw.GetSafeHandle() != NULL)m_bmpDraw.DeleteObject();<br />

if(m_bmpMask.GetSafeHandle() != NULL)m_bmpMask.DeleteObject();<br />

if(m_bmpBS.GetSafeHandle() != NULL)m_bmpBS.DeleteObject();<br />

If a pointer is not NULL, it means that there is an object being currently selected by the DC, so<br />

funciton CGDIObject::SelectObject(…) is called to select the object (palette or bitmap) out of the DC<br />

before destroying it.<br />

We need to call this function just before the application exits. In the sample, a WM_DESTROY message<br />

handler is added to class CGDIView through using Class Wizard. The corresponding member function is<br />

implemented as follows:<br />

void CGDIView::OnDestroy()<br />

{<br />

CleanUp();<br />

CScrollView::OnDestroy();<br />

}<br />

Loading Bitmap & Creating Mask Bitmap<br />

In the sample, funciton CGDIView::LoadBitmap(…) is changed. In the new function, a DIB section is<br />

created from the DIB data, and the mask bitmap image is generated from the normal image. The palette<br />

creation precedure is still the same. The handle retruned from the DIB section is attached to variable<br />

CGDIView::m_bmpDraw:<br />

void CGDIView::LoadBitmap(HGLOBAL hData)<br />

{<br />

CPalette *pPalDraw;<br />

CPalette *pPalOld;<br />

CClientDC dc(this);<br />

HBITMAP hBmp;<br />

LPBYTE pBits;<br />

LPBITMAPINFO lpBi;<br />

LPLOGPALETTE lpLogPal;<br />

int nSizeCT;<br />

int i, j;<br />

CGDIDoc *pDoc;<br />

pDoc=GetDocument();<br />

ASSERT_VALID(pDoc);<br />

AfxGetApp()->DoWaitCursor(TRUE);<br />

lpBi=(LPBITMAPINFO)::GlobalLock(hData);<br />

ASSERT(lpBi);<br />

nSizeCT=pDoc->GetColorTableSize(lpBi->bmiHeader.biBitCount);<br />

pPalDraw=pDoc->GetPalette();<br />

if(pPalDraw->GetSafeHandle() != NULL)pPalDraw->DeleteObject();<br />

if(nSizeCT != 0)<br />

{<br />

lpLogPal=(LPLOGPALETTE) new BYTE<br />

[<br />

sizeof(LOGPALETTE)+(nSizeCT-1)*sizeof(PALETTEENTRY)<br />

];<br />

319

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

Saved successfully!

Ooh no, something went wrong!