📄 ch07.htm
字号:
<TD>
<pre><font color="#008000">OnPrepareDC()</font></pre>
<TD>
<P>Override this function to modify the device context that is used to display or print the document. You can, for example, handle pagination here.</P>
<TR>
<TD>
<pre><font color="#008000">OnPreparePrinting()</font></pre>
<TD>
<P>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 <font color="#008000">OnBeginPrinting()</font>.</P>
<TR>
<TD>
<pre><font color="#008000">OnPrint()</font></pre>
<TD>
<P>Override this function to provide additional printing services, such as printing headers and footers, not provided in <font color="#008000">OnDraw()</font>.</P></TABLE>
<P>To print a document, MFC calls the functions listed in Table 7.2 in a specific order. First it calls <font color="#008000">OnPreparePrinting()</font>, which simply calls <font color="#008000">DoPreparePrinting()</font>, as shown in Listing 7.6. <font
color="#008000">DoPreparePrinting()</font> is responsible for displaying the Print dialog box and creating the printer DC.</P>
<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>
<P><I>Listing </I><I>7.6—print1View.cpp—CPrint1View::</I>OnPreparePrinting() as generated by AppWizard</P>
<pre><font color="#008000">BOOL CPrint1View::OnPreparePrinting(CPrintInfo* pInfo)</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> // default preparation</font></pre>
<pre><font color="#008000"> return DoPreparePrinting(pInfo);</font></pre>
<pre><font color="#008000">}</font></pre>
<P>As you can see, <font color="#008000">OnPreparePrinting()</font> receives as a parameter a pointer to a <font color="#008000">CPrintInfo</font> object. Using this object, you can obtain information about the print job, as well as initialize attributes
such as the maximum page number. Table 7.3 lists the most useful data and function members of the <font color="#008000">CPrintInfo</font> class, along with their descriptions.</P>
<P><I>Table 7.3—Members of the </I>CPrintInfo<I> Class</I></P>
<TABLE BORDER>
<TR>
<TD>
<P><B>Member</B></P>
<TD>
<P><b>Description</b></P>
<TR>
<TD>
<pre><font color="#008000">SetMaxPage()</font></pre>
<TD>
<P>Sets the document's maximum page number.</P>
<TR>
<TD>
<pre><font color="#008000">SetMinPage()</font></pre>
<TD>
<P>Sets the document's minimum page number.</P>
<TR>
<TD>
<pre><font color="#008000">GetFromPage()</font></pre>
<TD>
<P>Gets the number of the first page that the user selected for printing.</P>
<TR>
<TD>
<pre><font color="#008000">GetMaxPage()</font></pre>
<TD>
<P>Gets the document's maximum page number, which may be changed in <font color="#008000">OnBeginPrinting()</font>.</P>
<TR>
<TD>
<pre><font color="#008000">GetMinPage()</font></pre>
<TD>
<P>Gets the document's minimum page number, which may be changed in <font color="#008000">OnBeginPrinting()</font>.</P>
<TR>
<TD>
<pre><font color="#008000">GetToPage()</font></pre>
<TD>
<P>Gets the number of the last page the user selected for printing.</P>
<TR>
<TD>
<pre><font color="#008000">m_bContinuePrinting</font></pre>
<TD>
<P>Controls the printing process. Setting the flag to <font color="#008000">FALSE</font> ends the print job.</P>
<TR>
<TD>
<pre><font color="#008000">m_bDirect</font></pre>
<TD>
<P>Indicates whether the document is being directly printed.</P>
<TR>
<TD>
<pre><font color="#008000">m_bPreview</font></pre>
<TD>
<P>Indicates whether the document is in print preview.</P>
<TR>
<TD>
<pre><font color="#008000">m_nCurPage</font></pre>
<TD>
<P>Holds the current number of the page being printed.</P>
<TR>
<TD>
<pre><font color="#008000">m_nNumPreviewPages</font></pre>
<TD>
<P>Holds the number of pages (1 or 2) that are being displayed in print preview.</P>
<TR>
<TD>
<pre><font color="#008000">m_pPD</font></pre>
<TD>
<P>Holds a pointer to the print job's <font color="#008000">CPrintDialog</font> object.</P>
<TR>
<TD>
<pre><font color="#008000">m_rectDraw</font></pre>
<TD>
<P>Holds a rectangle that defines the usable area for the current page.</P>
<TR>
<TD>
<pre><font color="#008000">m_strPageDesc</font></pre>
<TD>
<P>Holds a page-number format string.</P></TABLE>
<P>When the <font color="#008000">DoPreparePrinting()</font> function displays the Print dialog box, the user can set the value of many of the data members of the <font color="#008000">CPrintInfo</font> class. Your program then can use or set any of these
values. Usually, you'll at least call <font color="#008000">SetMaxPage()</font>, which sets the document's maximum page number, before <font color="#008000">DoPreparePrinting()</font>, so that the maximum page number displays in the Print dialog box. If
you cannot 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 <font color="#008000">OnPreparePrinting()</font>, MFC calls <font color="#008000">OnBeginPrinting()</font>, 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. <font color="#008000">OnPreparePrinting()</font> receives, as parameters, a pointer to the printer DC and a pointer to the associated <font color="#008000">CPrintInfo</font> object.</P>
<P>Next, MFC calls <font color="#008000">OnPrepareDC()</font> for the first page in the document. This is the beginning of a print loop that is executed once for each page in the document. <font color="#008000">OnPrepareDC()</font> 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 <font color="#008000">OnPrepareDC()</font>, MFC calls <font color="#008000">OnPrint()</font> to print the actual page. Normally, <font color="#008000">OnPrint()</font> calls <font color="#008000">OnDraw()</font> with the printer DC, which
automatically directs <font color="#008000">OnDraw()</font>'s output to the printer rather than to the screen. You can override <font color="#008000">OnPrint()</font> to control how the document is printed. You can print headers and footers in <font
color="#008000">OnPrint()</font> and then call the base class's version (which in turn calls OnDraw()) to print the body of the document, as demonstrated in Listing 7.7. To prevent the base class version from overwriting your header and footer area,
restrict the printable area by setting the <font color="#008000">m_rectDraw</font> member of the <font color="#008000">CPrintInfo</font> object to a rectangle that does not overlap the header or footer. </P>
<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>
<P><I>Listing 7.7——Possible </I>OnPrint()<I> </I><I>with Headers and Footers</I></P>
<pre><font color="#008000">void CPrint1View::OnPrint(CDC* pDC, CPrintInfo* pInfo) </font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> // TODO: Add your specialized code here and/or call the base class</font></pre>
<pre><font color="#008000"> </font></pre>
<pre><font color="#008000"> // Call local functions to print a header and footer.</font></pre>
<pre><font color="#008000"> PrintHeader();</font></pre>
<pre><font color="#008000"> PrintFooter();</font></pre>
<pre><font color="#008000"> CView::OnPrint(pDC, pInfo);</font></pre>
<pre><font color="#008000">}</font></pre>
<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>
<P>Or you can remove <font color="#008000">OnDraw()</font> from the print loop entirely by doing your own printing in <font color="#008000">OnPrint()</font> and not calling <font color="#008000">OnDraw()</font> at all (see Listing 7.8).</P>
<P><I>Listing 7.8—Possible </I>OnPrint()<I> </I><I>without </I>OnDraw()</P>
<pre><font color="#008000">void CPrint1View::OnPrint(CDC* pDC, CPrintInfo* pInfo) </font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> // TODO: Add your specialized code here and/or call the base class</font></pre>
<pre><font color="#008000"> </font></pre>
<pre><font color="#008000"> // Call local functions to print a header and footer.</font></pre>
<pre><font color="#008000"> PrintHeader();</font></pre>
<pre><font color="#008000"> PrintFooter();</font></pre>
<pre><font color="#008000"> // Call a local function to print the body of the document.</font></pre>
<pre><font color="#008000"> PrintDocument();</font></pre>
<pre><font color="#008000">}</font></pre>
<P>As long as there are more pages to print, MFC continues to call <font color="#008000">OnPrepareDC()</font> and <font color="#008000">OnPrint()</font> for each page in the document. After the last page is printed, MFC calls <font
color="#008000">OnEndPrinting()</font>, where you can destroy any resources you created in <font color="#008000">OnBeginPrinting()</font>. The entire printing process is summarized in Figure 7.15.</P>
<B><A HREF="Ifig15.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch07/Ifig15.gif">FIG. 7.15</A></b>
<P><I>MFC calls various member functions during the printing process.</I></P>
<H3><A ID="I14" NAME="I14"><A ID="I15" NAME="I15"><B>From Here</B></A><B>...</B></A></H3>
<P>Under MFC, printing and print preview can be as simple or complex as you want or need it to be. For example, MFC can print simple one-page documents almost automatically. All you have to do is supply code for the <font color="#008000">OnDraw()</font>
function, which is responsible for displaying data both in a window and on the printer. If you need to, however, you can override other member functions of the view class to gain more control over the printing process. You have to do this, for example,
when printing multiple-page documents or when you want to separate the display duties of <font color="#008000">OnDraw()</font> from the printing and print preview process.</P>
<P>Please refer to the following for more information on the topics covered in this chapter:</P>
<ul>
<li> <A HREF="index05.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index05.htm" target="text">Chapter 5</A>, "Documents and Views," explains how to coordinate your document and view classes in an MFC application.</P>
<li> <A HREF="index06.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index06.htm" target="text">Chapter 6</A>, "Drawing on the Screen," describes how to display graphics and text in a window.</P>
</ul>
<p><hr></p>
<center>
<p><font size=-2>
© 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a
Simon and Schuster Company.</font></p>
</center>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -