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

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

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

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

……<br />

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

dc.RealizePalette();<br />

The color table is obtained from member bmiColors of structure BITMAPINFO (pointed by lpBi). Since<br />

the palette is stored in the document, we first call CGDIDoc::GetPalette() to obtain the address of the<br />

palette (CGDIDoc::m_palDraw), then call CPalette::CreatePalette(…) to create the palette. After this we<br />

select the palette into the client DC, and call CDC::RealizePalette() to let the logical palette be mapped<br />

to the system palette.<br />

Then, we create the DDB from DIB data:<br />

……<br />

……<br />

hBmp=::CreateDIBitmap<br />

(<br />

dc.GetSafeHdc(),<br />

&lpBi->bmiHeader,<br />

CBM_INIT,<br />

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

lpBi,<br />

DIB_RGB_COLORS<br />

);<br />

ASSERT(hBmp);<br />

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

pBmp=pDoc->GetBitmap();<br />

pBmp->Attach(hBmp);<br />

Function ::CreateDIBitmap(…) returns an HBITMAP type handle, which must be associated with a<br />

CBitmap type varible by calling function CBitmap::Attach(…).<br />

The rest part of this funciton fills variable CGDIView::m_bmInfo with bitmap information, sets the<br />

scroll sizes, create the memory DC, select bitmap and palette into it, then free the bitmap resource loaded<br />

before:<br />

……<br />

}<br />

}<br />

pBmp->GetBitmap(&m_bmInfo);<br />

SetScrollSizes(MM_TEXT, CSize(m_bmInfo.bmWidth, m_bmInfo.bmHeight));<br />

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

m_pBmpMemOld=m_dcMem.SelectObject(pBmp);<br />

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

::FreeResource(hData);<br />

Because the bitmap and the palette are selected into the memory DC here, we must select them out<br />

before application exits. The best place to do this is in WM_DESTROY message handler. In the sample, a<br />

WM_DESTROY message handler is added to class CGDIView through using Class Wizard, and the<br />

corresponding function CGDIView::OnDestroy() is implemented as follows:<br />

void CGDIView::OnDestroy()<br />

{<br />

m_dcMem.SelectPalette(m_pPalMemOld, FALSE);<br />

m_dcMem.SelectObject(m_pBmpMemOld);<br />

}<br />

CScrollView::OnDestroy();<br />

291

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

Saved successfully!

Ooh no, something went wrong!