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

……<br />

);<br />

-rect.Height(),<br />

(LPSTR)lpBi+dwBitOffset,<br />

lpBi,<br />

DIB_RGB_COLORS,<br />

SRCCOPY<br />

Calculating the Number of Pages when the Printing Is Undergoing<br />

Sample 12.5-3\GDI uses an alternate method to calculate the number of required pages for printing. It<br />

is based on sample 12.4\GDI. Instead of calculating the number of pages before printing begins, we can set<br />

the number of pages to the maximum value then start printing. Each time a page is printed, we check if all<br />

of the image has been output to the printer. If so, we can stop printing.<br />

The advantage of this method is that the actual number of required pages need not to be decided<br />

beforehand. This is especially useful for the situation when data cannot be formatted before being printed.<br />

By applying this method, we do not need to calculate the number of required pages in function<br />

CGDIView::OnPrepareDC(…). Instead, we can first set it to the maximum possible value in function<br />

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

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

{<br />

if(DoPreparePrinting(pInfo) == FALSE)return FALSE;<br />

pInfo->SetMaxPage(0xFFFFFFFE);<br />

return TRUE;<br />

}<br />

Please note that we must use 0xFFFFFFFE instead of 0xFFFFFFFF to set the maximum page range,<br />

the latter will result in printing only the first page.<br />

If we do not add further control, the printing will not stop until 0xFFFFFFFE pages have been printed.<br />

To stop printing after all the image has been printed out, we need to calculate the total required number of<br />

pages and check if the page currently being printed is the last page in function CGDIView::OnPrint(…). If<br />

so, we set the page range again to stop printing:<br />

……<br />

……<br />

uNumPages=<br />

(<br />

((lpBi->bmiHeader.biWidth-1)/abs(rectDC.Width())+1)*<br />

((lpBi->bmiHeader.biHeight-1)/abs(rectDC.Height())+1)<br />

);<br />

if(uNumPages == pInfo->m_nCurPage)<br />

{<br />

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

}<br />

In our case the number of pages can actually be decided before the printing begins, so it seems not<br />

necessary to stop printing this way. However, for applications that the number of pages cannot be decided<br />

beforehand, this is the only method to implement multiple-page printing.<br />

12.6 Customizing Print Dialog Box<br />

In this section, we will discuss the topics on how to enhance the user interface for implementing print<br />

set up, which has nothing to do with GDI.<br />

Customizing Common Controls<br />

One customization we want to make is to disable radio buttons labeled “Pages” and “Selection”, along<br />

with the edit boxes labeled “from:” and “to:” after the user executes “File | Print” command (Figure 12-8).<br />

This dialog box is implemented by print common dialog box, which was not discussed in Chapter 7. The<br />

387

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

Saved successfully!

Ooh no, something went wrong!