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

DIB Section<br />

Both DIB and DDB are needed in order to implement transparent background drawing: we need DIB<br />

format to generate mask image, and need DDB to draw the image. Of course we can call ::GetDIBits(…)<br />

and ::SetDIBits(…) to convert between DIB and DDB format, however, there exists an easier way to let<br />

us handle DIB and DDB simultaneously.<br />

A DIB section can be created to manage the image so that we can have both the DIB and DDB features<br />

without doing the conversion. A DIB section is a memory section that can be shared between the process<br />

and the system. When a change is made within the process, it is automatically updated to the system. By<br />

doing this, there is no need to update the data using functions ::GetDIBits(…) and ::SetDIBits(…).<br />

We can call function ::CreateDIBSection(…) to create a DIB section. This function will return an<br />

HBITMAP handle, which can be attached to a CBitmap variable by calling function CBitmap::Attach(…).<br />

Function ::CreateDIBSection(…) has six parameters:<br />

HBITMAP ::CreateDIBSection<br />

(<br />

HDC hdc,<br />

CONST BITMAPINFO *pbmi, UINT iUsage, VOID *ppvBits, HANDLE hSection,<br />

DWORD dwOffset<br />

);<br />

Parameter<br />

hdc<br />

pbmi<br />

iUsage<br />

ppvBits<br />

hSection<br />

dwOffset<br />

Meaning<br />

The handle of the device context.<br />

A BITMAPINFO type pointer that provides the bitmap information.<br />

Specifies data type contained in member bmiColors of structure BITMAPINFO, which is<br />

pointed by pbmi. If the value is DIB_PAL_COLORS, array bmiColors contains logical palette<br />

indices; if the value is DIB_RGB_COLORS, array bmiColors contains literal RGB values.<br />

A pointer that can be used to receive device-independent bitmap’s bit values.<br />

Specifies the section handle, we can pass a file handle to it to allow the bitmap to be mapped<br />

to the file. If we pass NULL, operating system will automatically allocate memory for<br />

bitmap mapping.<br />

Specifies the offset from the beginning of the file to the bitmap data if the bitmap will be<br />

mapped to a file. The parameter will be neglected if hSection is NULL.<br />

After calling this function, we can access the buffers pointed by ppvBits and make change to the DIB<br />

bits directly, there is no need for us to do any DIB to DDB conversion or vice versa. After the change is<br />

made, we can draw the bitmap immediately by calling funciton CDC::BitBlt(…), this will draw the updated<br />

image to the window.<br />

New Variables<br />

The following new variables are declared in class CGDIView for drawing bitmap with transparancy:<br />

class CGDIView : public CScrollView<br />

{<br />

protected:<br />

BOOL m_bBitmapLoaded;<br />

CBitmap m_bmpBkd;<br />

CBitmap m_bmpDraw;<br />

CBitmap m_bmpMask;<br />

CBitmap m_bmpBS;<br />

CBitmap *m_pBmpOld;<br />

CBitmap *m_pBmpMaskOld;<br />

CBitmap *m_pBmpBSOld;<br />

CDC m_dcMem;<br />

CDC m_dcMemMask;<br />

CDC m_dcMemBS;<br />

CPalette *m_pPalOld;<br />

CPalette *m_pPalMaskOld;<br />

CPalette *m_pPalBSOld;<br />

CGDIView();<br />

317

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

Saved successfully!

Ooh no, something went wrong!