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

if(hData != NULL)<br />

{<br />

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

ASSERT(lpBi);<br />

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

lpLogPal=(LPLOGPALETTE) new BYTE<br />

[<br />

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

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

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

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

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

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

}<br />

::GlobalUnlock(hData);<br />

m_bBitmapLoaded=TRUE;<br />

SetScrollSizes<br />

(<br />

MM_TEXT,<br />

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

);<br />

Invalidate();<br />

}<br />

else SetScrollSizes(MM_TEXT, CSize(0, 0));<br />

In this functon, DIB handle is obtained from the document and locked. From the global memory<br />

buffers, the color table contained in the DIB is obtained and is used for creating the logical palette. The a<br />

flag is set to indicate that the bitmap is loaded successfully.<br />

In function CGDIView::OnDraw(…), the DIB is painted to the client window:<br />

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

{<br />

CPalette *pPal;<br />

CPalette *pPalOld;<br />

HGLOBAL hDib;<br />

LPBITMAPINFO lpBi;<br />

DWORD dwBitOffset;<br />

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

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

ASSERT_VALID(pDoc);<br />

hDib=pDoc->GetHDib();<br />

ASSERT(hDib);<br />

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

ASSERT(lpBi);<br />

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

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

pDC->RealizePalette();<br />

dwBitOffset=<br />

(<br />

sizeof(BITMAPINFOHEADER)+<br />

pDoc->GetColorTableSize<br />

(<br />

lpBi->bmiHeader.biBitCount<br />

)*sizeof(RGBQUAD)<br />

);<br />

::SetDIBitsToDevice<br />

303

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

Saved successfully!

Ooh no, something went wrong!