?? ch21.htm
字號:
<H4><FONT COLOR="#000077">TYPE: Listing 21.7. Printing a header and text using theOnPrint 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->SetMapMode( MM_TWIPS );</TT><TT> PrintHeader( pDC );</TT><TT> CFont* pOldFont = pDC->SelectObject( m_pFntBold );</TT><TT> pDC->GetTextMetrics( &tm );</TT><TT> int cyText = tm.tmHeight + tm.tmExternalLeading;</TT><TT> m_nCurrentPrintedPage++;</TT><TT> pDC->TextOut( pt.x, pt.y, "Hello Printer!!!" );</TT><TT> pt.y += cyText;</TT><TT> CString szPageInfo;</TT><TT> szPageInfo.Format( TEXT("Page number %d"),</TT><TT> m_nCurrentPrintedPage );</TT><TT> pDC->TextOut( pt.x, pt.y, szPageInfo );</TT><TT> pDC->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. Addthese 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->SelectObject( m_pFntHighlight );</TT><TT> ASSERT( pOldFont );</TT><TT> pDC->GetTextMetrics( &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->SelectObject( &m_penBlack );</TT><TT> ASSERT( pOldPen );</TT><TT> pt.y -= (cyText / 2);</TT><TT> pDC->MoveTo( pt );</TT><TT> pDC->LineTo( 10000, pt.y );</TT><TT> pt.y -= cyText;</TT><TT> pDC->TextOut( pt.x, pt.y, TEXT("Every page needs a footer") );</TT><TT> // Restore GDI objects.</TT><TT> pDC->SelectObject( pOldFont );</TT><TT> pDC->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->SelectObject( m_pFntBanner );</TT><TT> ASSERT( pOldFont );</TT><TT> pDC->GetTextMetrics( &tm );</TT><TT> int cyText = tm.tmHeight + tm.tmExternalLeading;</TT><TT> pt.y -= cyText;</TT><TT> pDC->TextOut( pt.x, pt.y, " Teach Yourself Visual C++ in 24 Hours" );</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->SelectObject( &m_penBlack );</TT><TT> ASSERT( pOldPen );</TT><TT> pt.y -= cyText;</TT><TT> pDC->MoveTo( pt );</TT><TT> pDC->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->SelectObject( m_pFntHighlight ) );</TT><TT> pDC->GetTextMetrics( &tm );</TT><TT> cyText = tm.tmHeight + tm.tmExternalLeading;</TT><TT> pt.y -= (cyText / 2);</TT><TT> pDC->TextOut( pt.x, pt.y, "Printing Demonstration" );</TT><TT> // Restore GDI objects.</TT><TT> pDC->SelectObject( pOldFont );</TT><TT> pDC->SelectObject( pOldPen );</TT><TT>}</TT></FONT></PRE><H3><FONT COLOR="#000077"><B>Using the <TT>OnEndPrinting</TT> Function to ReleaseResources</B></FONT></H3><P>The <TT>OnEndPrinting</TT> function is called once per print job, but only ifthe <TT>OnBeginPrinting</TT> function has been called. Use this function to releasethe 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> functionused in <TT>MFCPrintView</TT>. As in the <TT>OnBeginPrinting</TT> function presentedin 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 OnEndPrintingfunction.</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 eitherthe 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 MFCand the Document/View architecture. You also created a small sample program thatsent three pages of text to the printer.<H2><FONT COLOR="#000077"><B>Q&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->IsPrinting())</TT><TT>{</TT><TT> LPDEVMODE pDevMode;</TT><TT> pDevMode = pInfo->m_pPD->GetDevMode();</TT><TT> pDevMode->dmOrientation = DMORIENT_LANDSCAPE;</TT><TT> pDC->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 whatyou've learned, and begin thinking ahead to putting your knowledge into practice.The answers to the quiz are in Appendix B, "Quiz Answers."<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"><IMGSRC="../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>© <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. Allrights reserved.</CENTER></BODY></HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -