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 12. Screen Capturing & Printing<br />

DIB_RGB_COLORS<br />

)<br />

);<br />

::GlobalUnlock(hDib);<br />

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

}<br />

return hDib;<br />

New Command<br />

In sample 12.1\GDI, a new command Capture | Go! is added to mainframe menu IDR_MAINFRM. This<br />

command is handled by function CGDIDoc::OnCaptureGo(). Within the function, we first minimize the<br />

application by calling function CMainFrame::ShowWindow(…) using SW_SHOWMINIMIZED flag. Then we call<br />

function CGDIView::PrepareCapture() to set the timer (This function contains only one statement).<br />

Because we will minimize our application window, the capture should be delayed for a few seconds<br />

after the user executes Capture | Go! command. Function CGDIView::PrepareCapture() does nothing but<br />

setting up a timer with time out period set to 2 seconds. Function CGDIView::Capture() is called when the<br />

timer times out. Within function CGDIView::Capture(), after the capture is made, function CGDIDoc::<br />

GetCaptureBitmap(…) will be called to convert DDB to DIB and update the client window (Within this<br />

function, CGDIDoc::ConvertDDBtoDIB(…) will be called).<br />

In CGDIView::OnDraw(…), we use function ::SetDIBitsToDevice(…) to display DIB data. This is the<br />

same with sample 10.4\GDI.<br />

Figure 12-1 shows the time sequence of function calling.<br />

Execute Capture | Go !<br />

command<br />

Call CGDIView::PrepareCapture() to<br />

set timer<br />

Call<br />

CGDIDoc::ConvertDDBtoDIB(…)<br />

to convert DDB to DIB<br />

Time out<br />

Call CGDIView::Capture() to make<br />

the capture and store the image as DDB<br />

Call CDocument::UpdateAllViews(…)<br />

to update the client window<br />

Call<br />

CGDIDoc::GetCaptureBitmap(…) to<br />

convert DDB to DIB<br />

Function<br />

CGDIDoc::GetCaptureBitmap(…)<br />

Figure 12-1. Time sequence of capturing window<br />

12.2 Capturing a Specified Window<br />

One problem of the sample implemented in the previous section is that it can capture only the whole<br />

desktop window. To improve it, we will add some new functions so that the user can specify any window<br />

for capturing.<br />

374

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

Saved successfully!

Ooh no, something went wrong!