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

}<br />

The function is implemented as follows (Most part of this function is copied from function<br />

CGDIView::OnInitialUpdate()):<br />

void CGDIView::LoadBitmap(HGLOBAL hData)<br />

{<br />

CPalette *pPalDraw;<br />

LPBITMAPINFO lpBi;<br />

LPLOGPALETTE lpLogPal;<br />

int nSizeCT;<br />

int i;<br />

CGDIDoc *pDoc;<br />

pDoc=GetDocument();<br />

ASSERT_VALID(pDoc);<br />

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

ASSERT(lpBi);<br />

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

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

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

if(nSizeCT != 0)<br />

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

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

With this function, CGDIView::OnInitialUpdate() can be simplified to the following:<br />

void CGDIView::OnInitialUpdate()<br />

{<br />

CGDIDoc *pDoc;<br />

HGLOBAL hData;<br />

CScrollView::OnInitialUpdate();<br />

pDoc=GetDocument();<br />

ASSERT_VALID(pDoc);<br />

}<br />

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

if(hData != NULL)LoadBitmap(hData);<br />

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

308

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

Saved successfully!

Ooh no, something went wrong!