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

📄 ch21.htm

📁 Visual C++ 的学习资料 Visual C++ 的学习资料
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<H4><FONT COLOR="#000077">TYPE: Listing 21.7. Printing a header and text using the
OnPrint function.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CMFCPrintView::OnPrint(CDC* pDC, CPrintInfo* pInfo)</TT>
<TT>{</TT>
<TT>    CPoint      pt( 5000, -7000 );</TT>
<TT>    TEXTMETRIC  tm;</TT>

<TT>    //Since the DC has been modified, it's always a good idea to reset</TT>
<TT>    //the mapping mode, no matter which one you use. In our case, since</TT>
<TT>    //we use MM_TWIPS, we have to reset the mapping mode for each page.</TT>
<TT>    pDC-&gt;SetMapMode( MM_TWIPS );</TT>
<TT>    PrintHeader( pDC );</TT>
<TT>    CFont* pOldFont = pDC-&gt;SelectObject( m_pFntBold );</TT>
<TT>    pDC-&gt;GetTextMetrics( &amp;tm );</TT>
<TT>    int cyText = tm.tmHeight + tm.tmExternalLeading;</TT>

<TT>    m_nCurrentPrintedPage++;</TT>
<TT>    pDC-&gt;TextOut( pt.x, pt.y, &quot;Hello Printer!!!&quot; );</TT>

<TT>    pt.y += cyText;</TT>
<TT>    CString  szPageInfo;</TT>
<TT>    szPageInfo.Format( TEXT(&quot;Page number %d&quot;),</TT>
<TT>                       m_nCurrentPrintedPage );</TT>
<TT>    pDC-&gt;TextOut( pt.x, pt.y, szPageInfo );</TT>

<TT>    pDC-&gt;SelectObject( pOldFont );</TT>
<TT>    PrintFooter( pDC );</TT>
<TT>}</TT> </FONT></PRE>
<P>Listing 21.8 provides the source code used to print the header and footer. Add
these two functions to the <TT>MFCPrintView.cpp</TT> source file.
<H4><FONT COLOR="#000077">TYPE: Listing 21.8. Printing the header and footer.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CMFCPrintView::PrintFooter( CDC* pDC )</TT>
<TT>{</TT>
<TT>    ASSERT( pDC );</TT>
<TT>    TEXTMETRIC  tm;</TT>
<TT>    CPoint  pt( 0, -14400 );</TT>

<TT>    //Select the smaller font used for the file name.</TT>
<TT>    ASSERT( m_pFntHighlight );</TT>
<TT>    CFont* pOldFont = pDC-&gt;SelectObject( m_pFntHighlight );</TT>
<TT>    ASSERT( pOldFont );</TT>
<TT>    pDC-&gt;GetTextMetrics( &amp;tm );</TT>
<TT>    int cyText = tm.tmHeight + tm.tmExternalLeading;</TT>

<TT>    // Print the underline bar. This is the same pen used to draw</TT>
<TT>    // black lines in the control. 10000 twips is about 7 inches or so.</TT>
<TT>    CPen* pOldPen = pDC-&gt;SelectObject( &amp;m_penBlack );</TT>
<TT>    ASSERT( pOldPen );</TT>
<TT>    pt.y -= (cyText / 2);</TT>
<TT>    pDC-&gt;MoveTo( pt );</TT>
<TT>    pDC-&gt;LineTo( 10000, pt.y );</TT>

<TT>    pt.y -= cyText;</TT>
<TT>    pDC-&gt;TextOut( pt.x, pt.y, TEXT(&quot;Every page needs a footer&quot;) );</TT>
<TT>    // Restore GDI objects.</TT>
<TT>    pDC-&gt;SelectObject( pOldFont );</TT>
<TT>    pDC-&gt;SelectObject( pOldPen );</TT>
<TT>}</TT>
<TT>void CMFCPrintView::PrintHeader( CDC* pDC )</TT>
<TT>{</TT>
<TT>    ASSERT( pDC );</TT>
<TT>    TEXTMETRIC  tm;</TT>
<TT>    CPoint      pt( 0, 0 );</TT>

<TT>    // Select the banner font, and print the headline.</TT>
<TT>    CFont* pOldFont = pDC-&gt;SelectObject( m_pFntBanner );</TT>
<TT>    ASSERT( pOldFont );</TT>
<TT>    pDC-&gt;GetTextMetrics( &amp;tm );</TT>
<TT>    int cyText = tm.tmHeight + tm.tmExternalLeading;</TT>
<TT>    pt.y -= cyText;</TT>
<TT>    pDC-&gt;TextOut( pt.x, pt.y, &quot; Teach Yourself Visual C++ in 24 Hours&quot; );</TT>
<TT>    // Move down one line, and print and underline bar. This is the same</TT>
<TT>    // pen used to draw black lines in the control. 10000 twips is about</TT>
<TT>    // 7 inches or so.</TT>
<TT>    CPen* pOldPen = pDC-&gt;SelectObject( &amp;m_penBlack );</TT>
<TT>    ASSERT( pOldPen );</TT>
<TT>    pt.y -= cyText;</TT>
<TT>    pDC-&gt;MoveTo( pt );</TT>
<TT>    pDC-&gt;LineTo( 10000, pt.y );</TT>
<TT>    // We move down about 1/2 line, and print the report type using the</TT>
<TT>    // smaller font.</TT>
<TT>    VERIFY( pDC-&gt;SelectObject( m_pFntHighlight ) );</TT>
<TT>    pDC-&gt;GetTextMetrics( &amp;tm );</TT>
<TT>    cyText = tm.tmHeight + tm.tmExternalLeading;</TT>
<TT>    pt.y -= (cyText / 2);</TT>
<TT>    pDC-&gt;TextOut( pt.x, pt.y, &quot;Printing Demonstration&quot; );</TT>
<TT>    // Restore GDI objects.</TT>
<TT>    pDC-&gt;SelectObject( pOldFont );</TT>
<TT>    pDC-&gt;SelectObject( pOldPen );</TT>
<TT>}</TT></FONT></PRE>
<H3><FONT COLOR="#000077"><B>Using the <TT>OnEndPrinting</TT> Function to Release
Resources</B></FONT></H3>
<P>The <TT>OnEndPrinting</TT> function is called once per print job, but only if
the <TT>OnBeginPrinting</TT> function has been called. Use this function to release
the resources allocated in <TT>OnBeginPrinting</TT>.


<BLOCKQUOTE>
	<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>CAUTION:</B></FONT><B> </B>You must match all of
	your allocations made in <TT>OnBeginPrinting</TT> with deallocations in <TT>OnEndPrinting</TT>.
	If you don't, you will get a memory or resource leak. 
<HR>


</BLOCKQUOTE>

<P>Listing 21.9 provides the source code for the <TT>OnEndPrinting</TT> function
used in <TT>MFCPrintView</TT>. As in the <TT>OnBeginPrinting</TT> function presented
in Listing 21.5, AppWizard comments out the <TT>pDC</TT> and <TT>pInfo</TT> parameters.
If you use these parameters, you must remove the comments.
<H4><FONT COLOR="#000077">TYPE: Listing 21.9. Releasing resources in the OnEndPrinting
function.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CMFCPrintView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)</TT>
<TT>{</TT>
<TT>    delete m_pFntBold;</TT>
<TT>    delete m_pFntBanner;</TT>
<TT>    delete m_pFntHighlight;</TT>
<TT>    // Since the destructor also deletes these fonts, we have</TT>
<TT>    // to set pointers to 0 to avoid dangling pointers and exceptions</TT>
<TT>    // generated by invoking delete on a non-valid pointer.</TT>
<TT>    m_pFntBold = 0;</TT>
<TT>    m_pFntBanner = 0;</TT>
<TT>    m_pFntHighlight = 0;</TT>
<TT>    CView::OnEndPrinting(pDC, pInfo);</TT>
<TT>}</TT> </FONT></PRE>
<P>Compile and run the Print project, and send the output to the printer using either
the File menu or the toolbar icon. Send the sample printout pages to the printer.
<H2><FONT COLOR="#000077"><B>Summary</B></FONT></H2>
<P>In this hour you learned about the print functions and support offered by MFC
and the Document/View architecture. You also created a small sample program that
sent three pages of text to the printer.
<H2><FONT COLOR="#000077"><B>Q&amp;A</B></FONT></H2>

<DL>
	<DD><B>Q How can I draw graphics such as rectangles and ellipses on my printouts?</B><BR>
	<BR>
	<B>A</B> The same way that you draw them to the screen--you can use all the basic
	GDI functions when printing; this includes <TT>Ellipse</TT> and <TT>Rectangle</TT>.<BR>
	<BR>
	<B>Q How can I change my printout to have landscape instead of portrait orientation?</B><BR>
	<BR>
	<B>A</B> To change the page orientation to landscape, you must change a printing
	attribute attached to the device context. Due to minor differences in the way in
	which Windows 95 and Windows NT handle printing details, this must be done for each
	page during the <TT>OnPrepareDC</TT> function. Add the following code at the top
	of <TT>CMFCPrintView::OnPrepareDC</TT>:
</DL>



<BLOCKQUOTE>
	<PRE><FONT COLOR="#0066FF"><TT>if(pDC-&gt;IsPrinting())</TT>
<TT>{</TT>
<TT>    LPDEVMODE  pDevMode;</TT>
<TT>    pDevMode = pInfo-&gt;m_pPD-&gt;GetDevMode();</TT>
<TT>    pDevMode-&gt;dmOrientation = DMORIENT_LANDSCAPE;</TT>
<TT>    pDC-&gt;ResetDC(pDevMode);</TT>
<TT>}</TT></FONT></PRE>

</BLOCKQUOTE>

<PRE><FONT COLOR="#0066FF"><TT></TT></FONT></PRE>
<H2><FONT COLOR="#000077"><B>Workshop</B></FONT></H2>
<P>The Workshop is designed to help you anticipate possible questions, review what
you've learned, and begin thinking ahead to putting your knowledge into practice.
The answers to the quiz are in Appendix B, &quot;Quiz Answers.&quot;
<H3><FONT COLOR="#000077"><B>Quiz</B></FONT></H3>

<DL>
	<DD>1. How can you determine whether a printer supports <TT>BitBlt</TT> operations?<BR>
	<BR>
	2. What are the five MFC view functions that are most commonly overridden for printing?<BR>
	<BR>
	3. Which MFC view functions are called once for every printed page, and which functions
	are called once per print job?<BR>
	<BR>
	4. What class is used to store information about the state of a print job?<BR>
	<BR>
	5. Which view function is used to allocate resources used to render the printout?<BR>
	<BR>
	6. Approximately how many twips are in an inch?<BR>
	<BR>
	7. What <TT>CPrintInfo</TT> member variable must be set for multiple page printouts?<BR>
	<BR>
	8. When using the <TT>MM_TWIPS</TT> mapping mode, which direction is positive: up
	or down?<BR>
	<BR>
	9. When using the <TT>MM_TWIPS</TT> mapping mode, which direction is positive: left
	or right?<BR>
	<BR>
	10. Which MFC view function should be used to release resources allocated for printing?
</DL>

<H3><FONT COLOR="#000077"><B>Exercises</B></FONT></H3>

<DL>
	<DD>1. Modify the MFCPrint project so that it prints the page number at the foot
	of each page.<BR>
	<BR>
	2. Modify the MFCPrint project so that it prints the time printed at the top of each
	page.<FONT COLOR="#000077"></FONT>
</DL>

<CENTER>
<P>
<HR>
<A HREF="../ch20/ch20.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch22/ch22.htm"><IMG
SRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <BR>
<BR>
<BR>
<IMG SRC="../button/corp.gif" WIDTH="284" HEIGHT="45" ALIGN="BOTTOM" ALT="Macmillan Computer Publishing USA"
BORDER="0"></P>

<P>&copy; <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. All
rights reserved.
</CENTER>


</BODY>

</HTML>

⌨️ 快捷键说明

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