?? ch06.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function popUp(pPage) {
var fullURL = document.location;
var textURL = fullURL.toString();
var URLlen = textURL.length;
var lenMinusPage = textURL.lastIndexOf("/");
lenMinusPage += 1;
var fullPath = textURL.substring(0,lenMinusPage);
popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394');
figDoc= popUpWin.document;
zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>';
zhtm += '<link rel="stylesheet" href="/includes/stylesheets/ebooks.css"></head>';
zhtm += '<BODY bgcolor="#FFFFFF">';
zhtm += '<IMG SRC="' + fullPath + pPage + '">';
zhtm += '<P><B>' + pPage + '</B>';
zhtm += '</BODY></HTML>';
window.popUpWin.document.write(zhtm);
window.popUpWin.document.close();
// Johnny Jackson 4/28/98
}
//-->
</SCRIPT>
<link rel="stylesheet" href="/includes/stylesheets/ebooks.css">
<TITLE>Special Edition Using Visual C++ 6 -- Ch 6 -- Printing and Print Preview</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<CENTER>
<H1><IMG SRC="../button/que.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>
Special Edition Using Visual C++ 6</H1>
</CENTER>
<CENTER>
<P><A HREF="../ch05/ch05.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch07/ch07.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>
<HR>
</CENTER>
<CENTER>
<H1>- 6 -</H1>
</CENTER>
<CENTER>
<H1>Printing and Print Preview</H1>
</CENTER>
<UL>
<LI><A HREF="#Heading1">Understanding Basic Printing and Print Preview with MFC</A>
<LI><A HREF="#Heading2">Scaling</A>
<LI><A HREF="#Heading3">Printing Multiple Pages</A>
<LI><A HREF="#Heading4">Setting the Origin</A>
<LI><A HREF="#Heading5">MFC and Printing</A>
</UL>
<P>
<HR SIZE="4">
<CENTER>
<H1></H1>
</CENTER>
<H2><A NAME="Heading1"></A>Understanding Basic Printing and Print Preview with MFC</H2>
<P>If you brought together 10 Windows programmers and asked them what part of creating
Windows applications they thought was the hardest, probably at least half of them
would choose printing documents. Although the device-independent nature of Windows
makes it easier for users to get peripherals working properly, programmers must take
up some of the slack by programming all devices in a general way. At one time, printing
from a Windows application was a nightmare that only the most experienced programmers
could handle. Now, however, thanks to application frameworks such as MFC, the job
of printing documents from a Windows application is much simpler.</P>
<P>MFC handles so much of the printing task for you that, when it comes to simple
one-page documents, you have little to do on your own. To see what I mean, follow
these steps to create a basic MFC application that supports printing and print preview:</P>
<DL>
<DT></DT>
<DD><B>1. </B>Choose File, New; select the Projects tab and start a new AppWizard
project workspace called Print1 (see Figure 6.1).
</DL>
<P><A HREF="javascript:popUp('06uvc01.gif')"><B>FIG. 6.1</B></A><B> </B><I>Start
an AppWizard project workspace called Print1.</I></P>
<DL>
<DD><B>2. </B>Give the new project the following settings in the AppWizard dialog
boxes. The New Project Information dialog box should then look like Figure 6.2.
</DL>
<BLOCKQUOTE>
<P>Step 1: Choose Single Document.</P>
<P>Step 2: Don't change the defaults presented by AppWizard.</P>
<P>Step 3: Don't change the defaults presented by AppWizard.</P>
<P>Step 4: Turn off all features except Printing and Print Preview.</P>
<P>Step 5: Don't change the defaults presented by AppWizard.</P>
<P>Step 6: Don't change the defaults presented by AppWizard.</P>
</BLOCKQUOTE>
<P><A HREF="javascript:popUp('06uvc02.gif')"><B>FIG. 6.2</B></A><B> </B><I>The New
Project Information dialog box.</I></P>
<DL>
<DD><B>3.</B> Expand the classes in ClassView, expand CPrint1View, double-click the
OnDraw() function, and add the following line of code to it, right after the comment
TODO: add draw code for native data here:
</DL>
<BLOCKQUOTE>
<PRE>pDC->Rectangle(20, 20, 220, 220);</PRE>
</BLOCKQUOTE>
<PRE></PRE>
<P>You've seen the Rectangle() function twice already: in the Recs app of Chapter
4, "Documents and Views," and the Paint1 app of Chapter 5, "Drawing
on the Screen." Adding this function to the OnDraw() function of an MFC program's
view class causes the program to draw a rectangle. This one is 200 pixels by 200
pixels, located 20 pixels down from the top of the view and 20 pixels from the left
edge.</P>
<BLOCKQUOTE>
<P>
<HR>
<strong>TIP:</strong> If you haven't read Chapter 5 and aren't comfortable with device contexts,
go back and read it now. Also, if you didn't read Chapter 4 and aren't comfortable
with the document/view paradigm, you should read it, too. In this chapter, you override
a number of virtual functions in your view class and work extensively with device
contexts.
<HR>
</BLOCKQUOTE>
<P>Believe it or not, you've just created a fully print-capable application that
can display its data (a rectangle) not only in its main window but also in a print
preview window and on the printer. To run the Print1 application, first compile and
link the source code by choosing Build, Build or by pressing F7. Then, choose Build,
Execute to run the program. You will see the window shown in Figure 6.3. This window
contains the application's output data, which is simply a rectangle. Next, choose
File, Print Preview. You see the print preview window, which displays the document
as it will appear if you print it (see Figure 6.4). Go ahead and print the document
(choose File, Print). These commands have been implemented for you because you chose
support for printing and print preview when you created this application with AppWizard.</P>
<P><A HREF="javascript:popUp('06uvc03.gif')"><B>FIG. 6.3</B></A><B> </B><I>Print1
displays a rectangle when you first run it.</I></P>
<P><A HREF="javascript:popUp('06uvc04.gif')"><B>FIG. 6.4</B></A><B> </B><I>The Print1
application automatically handles print previewing, thanks to the MFC AppWizard.</I></P>
<H2><A NAME="Heading2"></A>Scaling</H2>
<P>One thing you may notice about the printed document and the one displayed onscreen
is that, although the screen version of the rectangle takes up a fairly large portion
of the application's window, the printed version is tiny. That's because the pixels
onscreen and the dots on your printer are different sizes. Although the rectangle
is 200 dots square in both cases, the smaller printer dots yield a rectangle that
appears smaller. This is how the default Windows MM_TEXT graphics mapping mode works.
If you want to scale the printed image to a specific size, you might want to choose
a different mapping mode. Table 6.1 lists the mapping modes from which you can choose.</P>
<P>
<H4>Table 6.1  Mapping Modes</H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"><B>Mode</B></TD>
<TD ALIGN="LEFT"><B>Unit</B></TD>
<TD ALIGN="LEFT"><B>X</B></TD>
<TD ALIGN="LEFT"><B>Y</B></TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">MM_HIENGLISH</TD>
<TD ALIGN="LEFT">0.001 inch</TD>
<TD ALIGN="LEFT">Increases right</TD>
<TD ALIGN="LEFT">Increases up</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">MM_HIMETRIC</TD>
<TD ALIGN="LEFT">0.01 millimeter</TD>
<TD ALIGN="LEFT">Increases right</TD>
<TD ALIGN="LEFT">Increases up</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">MM_ISOTROPIC</TD>
<TD ALIGN="LEFT">User-defined</TD>
<TD ALIGN="LEFT">User-defined</TD>
<TD ALIGN="LEFT">User-defined</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">MM_LOENGLISH</TD>
<TD ALIGN="LEFT">0.01 inch</TD>
<TD ALIGN="LEFT">Increases right</TD>
<TD ALIGN="LEFT">Increases up</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">MM_LOMETRIC</TD>
<TD ALIGN="LEFT">0.1 millimeter</TD>
<TD ALIGN="LEFT">Increases right</TD>
<TD ALIGN="LEFT">Increases up</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">MM_TEXT</TD>
<TD ALIGN="LEFT">Device pixel</TD>
<TD ALIGN="LEFT">Increases right</TD>
<TD ALIGN="LEFT">Increases down</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">MM_TWIPS</TD>
<TD ALIGN="LEFT">1/1440 inch</TD>
<TD ALIGN="LEFT">Increases right</TD>
<TD ALIGN="LEFT">Increases up</TD>
</TR>
</TABLE>
</P>
<P>Working with graphics in MM_TEXT mode causes problems when printers and screens
can accommodate a different number of pixels per page. A better mapping mode for
working with graphics is MM_LOENGLISH, which uses a hundredth of an inch, instead
of a dot or pixel, as a unit of measure. To change the Print1 application so that
it uses the MM_LOENGLISH mapping mode, replace the line you added to the OnDraw()
function with the following two lines:</P>
<P>
<PRE>pDC->SetMapMode(MM_LOENGLISH);
pDC->Rectangle(20, -20, 220, -220);
</PRE>
<P>The first line sets the mapping mode for the device context. The second line draws
the rectangle by using the new coordinate system. Why the negative values? If you
look at MM_LOENGLISH in Table 6.1, you see that although X coordinates increase to
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -