11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 12. Screen Capturing & Printing<br />

12.5 Printing on Separate Pages<br />

If we want to output an image on separate papers, there are two situations: 1) Number of required<br />

pages is known before the printing starts. 2) Number of required pages has to be decided after the printing<br />

starts. For different situations, we need to use different approaches.<br />

Number of required Pages is Known Beforehand<br />

Sample 12.5-1\GDI is based on sample 12.4\GDI and demonstrates how to implement printing when<br />

the total number of pages is known beforehand.<br />

For some applications, the number of required pages for printing is fixed. For this situation, we need to<br />

override function CView::OnPreparePrinting(…), and call function CPrintInfo::SetMaxPage(…) to set<br />

the page range. By doing this, when the printing is being processed, function CView:OnPrint(…) will be<br />

called repeatedly until all the pages are printed out. Within CView::OnPrint(…), the page information can<br />

be obtained from member CPrintInfo::m_nCurPage (The second parameter of this function is a pointer to<br />

class CPrintInfo), which stands for the current page number that is being printed. According to this<br />

number, we can output different contents to different pages. The printing will be stopped after all the pages<br />

are printed out.<br />

For example, suppose whenever we want to print out the captured image, we’d like to make two<br />

copies, one with 100% ratio and one with 200% ratio. In this case the number of pages is determined<br />

beforehand. Before the printing begins, we can set the number of pages in function<br />

CGDIView::OnPreparePrinting(…) as follows:<br />

BOOL CGDIView::OnPreparePrinting(CPrintInfo *pInfo)<br />

{<br />

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

return DoPreparePrinting(pInfo);<br />

}<br />

By doing this, if the user executes printing command, in the popped up dialog box, the total number of<br />

pages will be set to 2. The user can choose to print any of the pages or both of them. In function<br />

CGDIView::OnPrint(…), we need to check which page is being printed and call function<br />

::StretchDIBits(…) using the corresponding ratio:<br />

……<br />

……<br />

……<br />

nRatio=pInfo->m_nCurPage;<br />

::StretchDIBits<br />

(<br />

pDC->GetSafeHdc(),<br />

0,<br />

0,<br />

nRatio*lpBi->bmiHeader.biWidth,<br />

-nRatio*lpBi->bmiHeader.biHeight,<br />

0,<br />

Setting Number of Pages Just Before Printing Starts<br />

However this is not a normal case. Because the user can actually change the printing ratio, the number<br />

of pages actually needed depends on print settings. For example, when the user set the printing ratio to<br />

400%, the image that can originally fit into one page (when the ratio is 100%) often requires more than one<br />

page now. So the actual number of pages needed depends on the printing ratio, which can range from 25%<br />

to 400%.<br />

One solution to this problem is to calculate the actual number of pages just before the printing starts.<br />

At this time, the print setting will not be changed any more, so we can calculate the number of required<br />

pages and call function CPrintInfo::SetMaxPage(…) to set the page range.<br />

This can be done in either function CView::BeginPrinting(…) or in function CView::<br />

OnPrepareDC(…). The first function will be called just before the print job begins, and the second function<br />

384

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

Saved successfully!

Ooh no, something went wrong!