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

)<br />

{<br />

(<br />

hRes=::FindResource<br />

(<br />

AfxGetResourceHandle(),<br />

MAKEINTRESOURCE(IDB_BITMAP),<br />

RT_BITMAP<br />

)<br />

) != NULL &&<br />

(<br />

hData=::LoadResource<br />

(<br />

AfxGetResourceHandle(),<br />

hRes<br />

)<br />

) != NULL<br />

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

ASSERT(lpBi);<br />

We call funcitons ::FindResource(…) and ::LoadResource(…) to load the bitmap resource. Function<br />

::FindResource(…) will return a handle to the specified resource block, which can be passed to<br />

::LoadResource(…) for loading the resource. This function returns a global memory handle. We can call<br />

::LockResource(…) to lock the resource. This function will return an address that can be used to access the<br />

bitmap data. In the sample, we use pointer lpBi to store this address.<br />

Next, we must calculate the size of color table and allocate enough memory for creating logical palette.<br />

The color table size can be calculated from “bit count per pixel” information of the bitmap as follows:<br />

……<br />

……<br />

switch(lpBi->bmiHeader.biBitCount)<br />

{<br />

case 1:<br />

{<br />

nSizeCT=2;<br />

break;<br />

}<br />

case 4:<br />

{<br />

nSizeCT=16;<br />

break;<br />

}<br />

case 8:<br />

{<br />

nSizeCT=256;<br />

break;<br />

}<br />

default: nSizeCT=0;<br />

}<br />

The color table size is stored in variable nSizeCT. Next, the logical palette is created from the color<br />

table stored in the DIB data:<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 />

290

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

Saved successfully!

Ooh no, something went wrong!