11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 10. Bitmap<br />

……<br />

m_pBmpOld=m_dcMem.SelectObject(&m_bmpDraw);<br />

m_pBmpMaskOld=m_dcMemMask.SelectObject(&m_bmpMask);<br />

m_pBmpBSOld=m_dcMemBS.SelectObject(&m_bmpBS);<br />

The mask and memory bitmaps must be created by calling function CBitmap::<br />

CreateCompatibleBitmap(…), this will allow the created bitmaps to be compatible with the device context.<br />

Next, the mask bitmap is generated from the normal bitmap image:<br />

……<br />

for(j=0; jbmiHeader.biHeight; j++)<br />

{<br />

for(i=0; ibmiHeader.biWidth; i++)<br />

{<br />

if(m_dcMem.GetPixel(i, j) == RGB(255, 255, 255))<br />

{<br />

m_dcMemMask.SetPixel(i, j, RGB(255, 255, 255));<br />

}<br />

else m_dcMemMask.SetPixel(i, j, RGB(0, 0, 0));<br />

}<br />

}<br />

m_bBitmapLoaded=TRUE;<br />

SetScrollSizes<br />

(<br />

MM_TEXT,<br />

CSize(lpBi->bmiHeader.biWidth, lpBi->bmiHeader.biHeight)<br />

);<br />

::GlobalUnlock(hData);<br />

if(nSizeCT != 0)dc.SelectPalette(pPalOld, FALSE);<br />

}<br />

Invalidate();<br />

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

Every pixel of the normal image is examined to generate the mask image. Here functions<br />

CDC::GetPixel(…) and CDC::SetPixel(…) are called for manipulating single pixels. Although the two<br />

functions hide the details of device context and bitmap format, they are very slow, and should not be used<br />

for fast bitmap drawing or image processing.<br />

Drawing Bitmap with Transparancy<br />

Function CGDIView::OnDraw(…) is modified as follows for drawing bitmap with transparency:<br />

void CGDIView::OnDraw(CDC *pDC)<br />

{<br />

CPalette *pPal;<br />

CPalette *pPalOld;<br />

BITMAP bm;<br />

if(m_bBitmapLoaded == FALSE)return;<br />

CGDIDoc *pDoc=GetDocument();<br />

ASSERT_VALID(pDoc);<br />

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

pPalOld=pDC->SelectPalette(pPal, FALSE);<br />

pDC->RealizePalette();<br />

m_bmpDraw.GetBitmap(&bm);<br />

m_dcMemBS.BitBlt<br />

(<br />

0,<br />

0,<br />

bm.bmWidth,<br />

bm.bmHeight,<br />

pDC,<br />

0,<br />

0,<br />

321

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

Saved successfully!

Ooh no, something went wrong!