⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch06.htm

📁 A very good resource on Visual C++ 6.0 environment. It teaches through step by step approach and fin
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<PRE>}
</PRE>
<P>The MFC framework calls OnPrepareDC() right before it displays data onscreen or
before it prints the data to the printer. (One strength of the device context approach
to screen display is that the same code can often be used for display and printing.)
If the application is about to display data, you (probably) don't want to change
the default processing performed by OnPrepareDC(). So, you must check whether the
application is printing data by calling IsPrinting(), a member function of the device
context class.</P>
<P>If the application is printing, you must determine which part of the data belongs
on the current page. You need the height in dots of a printed page, so you call GetDeviceCaps()
again.</P>
<P>Next, you must determine a new viewport origin (the position of the coordinates
0,0) for the display. Changing the origin tells MFC where to begin displaying data.
For page one, the origin is zero; for page two, it's moved down by the number of
dots on a page. In general, the vertical component is the page size times the current
page minus one. The page number is a member variable of the CPrintInfo class.</P>
<P>After you calculate the new origin, you only need to give it to the device context
by calling SetViewportOrg(). Your changes to OnPrepareDC() are complete.</P>
<P>To see your changes in action, build and run your new version of Print1. When
the program's main window appears, click twice in the window to add two rectangles
to the display. (The displayed rectangle count should be seven.) Again, choose File,
Print Preview and look at the two-page print preview window (see Figure 6.14). Now
the program previews the document correctly. If you print the document, it will look
the same in hard copy as it does in the preview.</P>
<P><A HREF="javascript:popUp('06uvc14.gif')"><B>FIG. 6.14</B></A><B> </B><I>Print1
finally previews and prints properly.</I></P>
<P>
<H2><A NAME="Heading5"></A>MFC and Printing</H2>
<P>Now you've seen MFC's printing and print preview support in action. As you added
more functionality to the Print1 application, you modified several member functions
that were overridden in the view class, including OnDraw(), OnBeginPrinting(), and
OnPrepareDC(). These functions are important to the printing and print preview processes.
However, other functions also enable you to add even more printing power to your
applications. Table 6.2 describes the functions important to the printing process.</P>
<P>
<H4>Table 6.2&#160;&#160;Printing Functions of a View Class</H4>
<P>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT"><B>Function</B></TD>
		<TD ALIGN="LEFT"><B>Description</B></TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">OnBeginPrinting()</TD>
		<TD ALIGN="LEFT">Override this function to create resources, such as fonts, that you need for printing
			the document. You also set the maximum page count here.</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">OnDraw()</TD>
		<TD ALIGN="LEFT">This function serves triple duty, displaying data in a frame window, a print preview
			window, or on the printer, depending on the device context sent as the function's
			parameter.</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">OnEndPrinting()</TD>
		<TD ALIGN="LEFT">Override this function to release resources created in OnBeginPrinting().</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">OnPrepareDC()</TD>
		<TD ALIGN="LEFT">Override this function to modify the device context used to display or print the
			document. You can, for example, handle pagination here.</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">OnPreparePrinting()</TD>
		<TD ALIGN="LEFT">Override this function to provide a maximum page count for the document. If you don't
			set the page count here, you should set it in OnBeginPrinting().</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">OnPrint()</TD>
		<TD ALIGN="LEFT">Override this function to provide additional printing services, such as printing
			headers and footers, not provided in OnDraw().</TD>
	</TR>
</TABLE>
</P>
<P>To print a document, MFC calls the functions listed in Table 6.2 in a specific
order. First it calls OnPreparePrinting(), which simply calls DoPreparePrinting(),
as shown in Listing 6.6. DoPreparePrinting() is responsible for displaying the Print
dialog box and creating the printer DC.</P>
<P>
<H4>Listing 6.6&#160;&#160;print1View.cpp --CPrint1View::OnPreparePrinting() as Generated
by AppWizard</H4>
<PRE>BOOL CPrint1View::OnPreparePrinting(CPrintInfo* pInfo)
{
     // default preparation
     return DoPreparePrinting(pInfo);
</PRE>
<PRE>}
</PRE>
<P>As you can see, OnPreparePrinting() receives as a parameter a pointer to a CPrintInfo
object. By using this object, you can obtain information about the print job as well
as initialize attributes such as the maximum page number. Table 6.3 describes the
most useful data and function members of the CPrintInfo class.</P>
<P>
<H4>Table 6.3&#160;&#160;Members of the CPrintInfo Class</H4>
<P>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT"><B>Member</B></TD>
		<TD ALIGN="LEFT"><B>Description</B></TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">SetMaxPage()</TD>
		<TD ALIGN="LEFT">Sets the document's maximum page number.</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">SetMinPage()</TD>
		<TD ALIGN="LEFT">Sets the document's minimum page number.</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">GetFromPage()</TD>
		<TD ALIGN="LEFT">Gets the number of the first page that users selected for printing.</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">GetMaxPage()</TD>
		<TD ALIGN="LEFT">Gets the document's maximum page number, which may be changed in OnBeginPrinting().</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">GetMinPage()</TD>
		<TD ALIGN="LEFT">Gets the document's minimum page number, which may be changed in OnBeginPrinting().</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">GetToPage()</TD>
		<TD ALIGN="LEFT">Gets the number of the last page users selected for printing.</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">m_bContinuePrinting</TD>
		<TD ALIGN="LEFT">Controls the printing process. Setting the flag to FALSE ends the print job.</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">m_bDirect</TD>
		<TD ALIGN="LEFT">Indicates whether the document is being directly printed.</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">m_bPreview</TD>
		<TD ALIGN="LEFT">Indicates whether the document is in print preview.</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">m_nCurPage</TD>
		<TD ALIGN="LEFT">Holds the current number of the page being printed.</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">m_nNumPreviewPages</TD>
		<TD ALIGN="LEFT">Holds the number of pages (1 or 2) being displayed in print preview.</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">m_pPD</TD>
		<TD ALIGN="LEFT">Holds a pointer to the print job's CPrintDialog object.</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">m_rectDraw</TD>
		<TD ALIGN="LEFT">Holds a rectangle that defines the usable area for the current page.</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">m_strPageDesc</TD>
		<TD ALIGN="LEFT">Holds a page-number format string.</TD>
	</TR>
</TABLE>
</P>
<P>When the DoPreparePrinting() function displays the Print dialog box, users can
set the value of many data members of the CPrintInfo class. Your program then can
use or set any of these values. Usually, you'll at least call SetMaxPage(), which
sets the document's maximum page number, before DoPreparePrinting() so that the maximum
page number displays in the Print dialog box. If you can't determine the number of
pages until you calculate a page length based on the selected printer, you have to
wait until you have a printer DC for the printer.</P>
<P>After OnPreparePrinting(), MFC calls OnBeginPrinting(), which is not only another
place to set the maximum page count but also the place to create resources, such
as fonts, that you need to complete the print job. OnPreparePrinting() receives as
parameters a pointer to the printer DC and a pointer to the associated CPrintInfo
object.</P>
<P>Next, MFC calls OnPrepareDC() for the first page in the document. This is the
beginning of a print loop that's executed once for each page in the document. OnPrepareDC()
is the place to control what part of the whole document prints on the current page.
As you saw previously, you handle this task by setting the document's viewport origin.</P>
<P>After OnPrepareDC(), MFC calls OnPrint() to print the actual page. Normally, OnPrint()
calls OnDraw() with the printer DC, which automatically directs OnDraw()'s output
to the printer rather than onscreen. You can override OnPrint() to control how the
document is printed. You can print headers and footers in OnPrint() and then call
the base class's version (which in turn calls OnDraw()) to print the body of the
document, as demonstrated in Listing 6.7. (The footer will appear below the body,
even though PrintFooter() is called before OnPrint()--don't worry.) To prevent the
base class version from overwriting your header and footer area, restrict the printable
area by setting the m_rectDraw member of the CPrintInfo object to a rectangle that
doesn't overlap the header or footer.</P>
<P>
<H4>Listing 6.7&#160;&#160;Possible OnPrint() with Headers and Footers</H4>
<PRE>void CPrint1View::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
    // TODO: Add your specialized code here and/or call the base class
    // Call local functions to print a header and footer.
    PrintHeader();
    PrintFooter();
    CView::OnPrint(pDC, pInfo);
</PRE>
<PRE>}
</PRE>
<P>Alternatively, you can remove OnDraw() from the print loop entirely by doing your
own printing in OnPrint() and not calling OnDraw() at all (see Listing 6.8).</P>
<P>
<H4>Listing 6.8&#160;&#160;Possible OnPrint() Without OnDraw()</H4>
<PRE>void CPrint1View::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
    // TODO: Add your specialized code here and/or call the base class
    // Call local functions to print a header and footer.
    PrintHeader();
    PrintFooter();
    // Call a local function to print the body of the document.
    PrintDocument();
</PRE>
<PRE>}
</PRE>
<P>As long as there are more pages to print, MFC continues to call OnPrepareDC()
and OnPrint() for each page in the document. After the last page is printed, MFC
calls OnEndPrinting(), where you can destroy any resources you created in OnBeginPrinting().
Figure 6.15 summarizes the entire printing process.</P>
<P><A HREF="javascript:popUp('06uvc15.gif')"><B>FIG. 6.15</B></A><B> </B><I>MFC calls
various member functions during the printing process.</I></P>
<H1><I></I></H1>
<CENTER>
<P>
<HR>
<A HREF="ch05.htm" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/ch05/ch05.htm"><IMG SRC="previous.gif" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="ch07.htm" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/ch07/ch07.htm"><IMG
SRC="next.gif" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="index.htm" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/index.htm"><IMG SRC="contents.gif" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <BR>
</P>

<P>&#169; <A HREF="copy.htm" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/copy.htm">Copyright</A>, Macmillan Computer Publishing. All
rights reserved.
</CENTER>


</BODY>

</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -