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

lpLogPal->palVersion=0x300;<br />

lpLogPal->palNumEntries=nSizeCT;<br />

for(i=0; ipalPalEntry[i].peRed=lpBi->bmiColors[i].rgbRed;<br />

lpLogPal->palPalEntry[i].peGreen=lpBi->bmiColors[i].rgbGreen;<br />

lpLogPal->palPalEntry[i].peBlue=lpBi->bmiColors[i].rgbBlue;<br />

lpLogPal->palPalEntry[i].peFlags=NULL;<br />

}<br />

}<br />

VERIFY(pPalDraw->CreatePalette(lpLogPal));<br />

delete [](BYTE *)lpLogPal;<br />

pPalOld=dc.SelectPalette(pPalDraw, FALSE);<br />

dc.RealizePalette();<br />

……<br />

CleanUp();<br />

hBmp=::CreateDIBSection<br />

(<br />

dc.GetSafeHdc(),<br />

lpBi,<br />

DIB_RGB_COLORS,<br />

(void **)&pBits,<br />

NULL,<br />

0<br />

);<br />

memcpy<br />

(<br />

(LPSTR)pBits,<br />

(LPSTR)lpBi+sizeof(BITMAPINFOHEADER)+nSizeCT*sizeof(RGBQUAD),<br />

lpBi->bmiHeader.biSizeImage<br />

);<br />

ASSERT(hBmp);<br />

m_bmpDraw.Attach(hBmp);<br />

Please note that function CGDIView::CleanUp() is called before the bitmap is created. After the DIB<br />

section is created, we use the DIB data passed through hData parameter to initialize the image. The buffers<br />

that store DIB bit values are pointed by pointer pBits. We can use it to edit the image pixels directly, there<br />

is no need to convert between DDB and DIB foramts any more.<br />

After the bitmap is loaded, we need to create the mask bitmap, memory bitmap, and theree memory<br />

DCs. We also need to select the bitmaps and the logical palette into the DCs if necessary:<br />

……<br />

m_dcMem.CreateCompatibleDC(&dc);<br />

m_dcMemMask.CreateCompatibleDC(&dc);<br />

m_dcMemBS.CreateCompatibleDC(&dc);<br />

ASSERT(m_dcMem.GetSafeHdc());<br />

ASSERT(m_dcMemMask.GetSafeHdc());<br />

ASSERT(m_dcMemBS.GetSafeHdc());<br />

if(nSizeCT != 0)<br />

{<br />

m_pPalOld=m_dcMem.SelectPalette(pPalDraw, FALSE);<br />

m_pPalMaskOld=m_dcMemMask.SelectPalette(pPalDraw, FALSE);<br />

m_pPalBSOld=m_dcMemBS.SelectPalette(pPalDraw, FALSE);<br />

}<br />

m_bmpMask.CreateCompatibleBitmap<br />

(<br />

&dc,<br />

lpBi->bmiHeader.biWidth,<br />

lpBi->bmiHeader.biHeight<br />

);<br />

m_bmpBS.CreateCompatibleBitmap<br />

(<br />

&dc,<br />

lpBi->bmiHeader.biWidth,<br />

lpBi->bmiHeader.biHeight<br />

);<br />

320

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

Saved successfully!

Ooh no, something went wrong!