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

will be called before CView::BeginPrinting(…) is called when the printing DC needs to be prepared.<br />

Please note that CView::OnPrepareDC(…) will also be called for preparing display DC, to distinguish<br />

between the two situations, we can check if parameter pInfo is NULL, if not, it is called for the printing<br />

job.<br />

The number of required pages can be calculated by retrieving the device resolution (need to be<br />

converted to logical unit) and comparing it with the image size. If the image size is greater than the device<br />

resolution, we can print one portion at a time until the whole image is output to the target device.<br />

Sample 12.5-2\GDI is based on sample 12.4\GDI, it demonstrates how to print the captured image<br />

using this method. In the sample, first function CGDIView::OnPrepareDC(…) is overridden, within which the<br />

number of required pages is calculated as follows:<br />

……<br />

nMapMode=pDC->SetMapMode(MM_LOMETRIC);<br />

CGDIDoc* pDoc=GetDocument();<br />

ASSERT_VALID(pDoc);<br />

……<br />

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

if(hDib != NULL)<br />

{<br />

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

ASSERT(lpBi);<br />

size.cx=lpBi->bmiHeader.biWidth;<br />

size.cy=lpBi->bmiHeader.biHeight;<br />

rect.left=rect.top=0;<br />

rect.right=pDC->GetDeviceCaps(HORZRES);<br />

rect.bottom=pDC->GetDeviceCaps(VERTRES);<br />

pDC->DPtoLP(rect);<br />

nNumPages=((size.cx-1)/abs(rect.Width())+1)*((size.cy-1)/abs(rect.Height())+1);<br />

pInfo->SetMaxPage(nNumPages);<br />

::GlobalUnlock(hDib);<br />

}<br />

Here, the device resolution is retrieved by calling function CDC::GetDeviceCaps(…). Because the<br />

device mapping mode is MM_LOMETRIC, which may cause the values returned by this function to be negative<br />

for vertical dimensions, we need to use absolute value when doing the calculation.<br />

Within function CGDIView::OnPrint(…), we print the corresponding portion of the image according to<br />

the current page number. This procedure can be illustrated in Figure 12-7.<br />

The shaded area represents the image. It is divided into horizontal and vertical cells, each cell has a<br />

dimension that is the same with target device resolution. To draw the image, we need nine pages, each page<br />

print one cell that is labeled (v, u). First we need to calculate the cell label from the page number:<br />

v = (page number - 1)/(number of horizontal cells)<br />

u = (page number -1)%(number of horizontal cells)<br />

Here the page number starts from 1. The next step is to calculate the position and the dimension of the<br />

cell. Obviously, the origin of a cell rectangle can be calculated as follows:<br />

(0, 0)<br />

(0, 1)<br />

(0, 2)<br />

Image<br />

(1, 0)<br />

(1, 1)<br />

(1, 2)<br />

(v, u)<br />

(2, 0)<br />

(2, 1)<br />

(2, 2)<br />

One cell<br />

Figure 12-7. Print a portion of the image according to the current page number<br />

385

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

Saved successfully!

Ooh no, something went wrong!