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

handlers are added for both ID_FILE_SAVE and ID_FILE_SAVE_AS commands, and the corresponding<br />

member functions are implemented as follows:<br />

void CGDIDoc::OnUpdateFileSave(CCmdUI* pCmdUI)<br />

{<br />

pCmdUI->Enable(FALSE);<br />

}<br />

void CGDIDoc::OnUpdateFileSaveAs(CCmdUI* pCmdUI)<br />

{<br />

pCmdUI->Enable(m_bmpDraw.GetSafeHandle() != NULL);<br />

}<br />

It seems unnecessary to conver the DDB to DIB before saving the image to a disk file because its<br />

original format is DIB. However, if the DDB is changed after being loaded (This is possible for a graphic<br />

editor application), the new DDB is inconsistent with the original DIB data.<br />

The DDB to DIB converting procedure is a little complex. If we are programming for Windows 95 or<br />

Windows NT 4.0, we can create DIB section (will be introduced in later sections) to let the format be<br />

converted automatically. If we are writing Win32 programs that will be run on Windows 3.1, we must use<br />

the method discussed in this section to implement the conversion.<br />

10.5 Drawing DIB Directly<br />

The counterpart function of ::GetDIBits(…) is ::SetDIBits(…), it can be used to convert DIB data to<br />

device dependent bitmap. The two functions provide us a way of implementing image editing: whenever<br />

we want to make change to the image, we can first retrieve DIB data from the DDB, edit the DIB data, and<br />

set it back to DDB.<br />

New Functions<br />

Sometimes it is easier to edit DDB directly instead of using DIB data. For example, if we want to<br />

reverse every pixel of the image, we can just call one API funciton to let this be handled by lower level<br />

driver instead of editting every single pixel by ourselves. This is why we need to handle both DIB and<br />

DDB in the applications.<br />

If our application is restricted on edittng only DIB data, we can call an API function directly to draw<br />

DIB in the client window. By doing so, we eleminte the complexity of converting DIB to DDB back and<br />

forth. This function is ::SetDIBitsToDevice(…), which has the following format:<br />

int ::SetDIBitsToDevice<br />

(<br />

HDC hdc,<br />

int XDest, int YDest, DWORD dwWidth, DWORD dwHeight,<br />

int XSrc, int YSrc,<br />

UINT uStartScan, UINT cScanLines,<br />

CONST VOID *lpvBits, CONST BITMAPINFO *lpbmi, UINT fuColorUse<br />

);<br />

There are altogether 12 parameters, whose meanings are listed in the following table:<br />

Parameter<br />

hdc<br />

Xdest, Ydest<br />

dwWidth, dwHeight<br />

uStartScan<br />

UscanLines<br />

lpvBits<br />

Lpbmi<br />

Meaning<br />

Handle of target DC.<br />

Position on the target DC where the image should be drawn.<br />

Image size on the target DC.<br />

Starting scan line of the source bitmap image.<br />

Number of scan lines in the source bitmap image that will be output to the target.<br />

Pointer to the DIB bits (image data).<br />

Pointer to BITMAPINFO type object.<br />

300

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

Saved successfully!

Ooh no, something went wrong!