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

}<br />

HGLOBAL ConvertDDBtoDIB(CBitmap *);<br />

DWORD GetColorTableSize(WORD);<br />

Function CGDIDoc::ConvertDDBtoDIB(…) converts DDB to DIB, its input parameter is a CBitmap type<br />

pointer and its return value is a global memory handle. Function CGDIDoc::GetColorTableSize(…) is used<br />

to calculate the size of color table from bit count per pixel information (In the previouse samples, color<br />

table size calculation is implemented within function CGDIView::OnInitialUpdate(). Since we need color<br />

table size information more frequently now, this calculation is implemented as a single member function):<br />

DWORD CGDIDoc::GetColorTableSize(WORD wBitCount)<br />

{<br />

DWORD dwSizeCT;<br />

}<br />

switch(wBitCount)<br />

{<br />

case 1:<br />

{<br />

dwSizeCT=2;<br />

break;<br />

}<br />

case 4:<br />

{<br />

dwSizeCT=16;<br />

break;<br />

}<br />

case 8:<br />

{<br />

dwSizeCT=256;<br />

break;<br />

}<br />

case 24:<br />

{<br />

dwSizeCT=0;<br />

}<br />

}<br />

return dwSizeCT;<br />

In function CGDIDoc::ConvertDDBtoDIB(…), first we must obtain a handle to the client window that<br />

can be used to create a DC:<br />

HGLOBAL CGDIDoc::ConvertDDBtoDIB(CBitmap *pBmp)<br />

{<br />

BITMAP bm;<br />

BITMAPINFOHEADER bi;<br />

LPBITMAPINFO lpBi;<br />

DWORD dwSizeCT;<br />

DWORD dwDibLen;<br />

CGDIView *ptrView;<br />

POSITION pos;<br />

CPalette *pPalOld;<br />

HGLOBAL hDib;<br />

……<br />

}<br />

pos=GetFirstViewPosition();<br />

ptrView=(CGDIView *)GetNextView(pos);<br />

ASSERT(ptrView->IsKindOf(RUNTIME_CLASS(CGDIView)));<br />

CClientDC dc(ptrView);<br />

Then function CBitmap::GetBitmap(…) is called to retrieve the information of bitmap and allocate<br />

enough buffers for storing structure BITMAPINFOHEADER and color table:<br />

……<br />

297

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

Saved successfully!

Ooh no, something went wrong!