vel21.htm
来自「简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的」· HTM 代码 · 共 2,347 行 · 第 1/4 页
HTM
2,347 行
<HTML><HEAD><TITLE>Visual Basic in 12 Easy Lessons vel21.htm </TITLE><LINK REL="ToC" HREF="index.htm"><LINK REL="Index" HREF="htindex.htm"><LINK REL="Next" HREF="vel22.htm"><LINK REL="Previous" HREF="velp10.htm"></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><A NAME="I0"></A><H2>Visual Basic in 12 Easy Lessons vel21.htm</H2><P ALIGN=LEFT><A HREF="velp10.htm" TARGET="_self"><IMG SRC="purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A><A HREF="index.htm" TARGET="_self"><IMG SRC="purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A><A HREF="vel22.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A><HR ALIGN=CENTER><P><UL><UL><UL><LI><A HREF="#E68E156" >What You'll Learn</A><LI><A HREF="#E68E157" >A Word of Warning</A><LI><A HREF="#E68E158" >Printing With Windows</A><LI><A HREF="#E68E159" >Tell the User You Will Print</A><LI><A HREF="#E68E160" >Introducing the Printer Object</A><LI><A HREF="#E68E161" >The Print Method</A><UL><LI><A HREF="#E69E144" >Printing Constants</A><LI><A HREF="#E69E145" >Printing Variables and Controls</A><LI><A HREF="#E69E146" >Printing Expressions</A><LI><A HREF="#E69E147" >Printing Multiple Values</A><LI><A HREF="#E69E148" >Utilize the Fonts</A><LI><A HREF="#E69E149" >Better Spacing with Spc() and Tab()</A></UL><LI><A HREF="#E68E162" >Initiating the Print</A><LI><A HREF="#E68E163" >Page Breaks</A><LI><A HREF="#E68E164" >Homework</A><UL><LI><A HREF="#E69E150" >General Knowledge</A><LI><A HREF="#E69E151" >Find the Bug</A><LI><A HREF="#E69E152" >What's the Output?</A><LI><A HREF="#E69E153" >Write Code That...</A><LI><A HREF="#E69E154" >Extra Credit</A></UL></UL></UL></UL><HR ALIGN=CENTER><A NAME="E66E31"></A><H1 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Lesson 11, Unit 21</B></FONT></CENTER></H1><BR><A NAME="E67E34"></A><H2 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Using the Printer</B></FONT></CENTER></H2><BR><BR><A NAME="E68E156"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>What You'll Learn</B></FONT></CENTER></H3><BR><UL><LI>A word of warning<BR><BR><LI>Printing with Windows<BR><BR><LI>Telling the user you will print<BR><BR><LI>Introducing the Printer object<BR><BR><LI>The Print method<BR><BR><LI>Initiating the print<BR><BR><LI>Page breaks<BR><BR></UL><P>This lesson describes how you can integrate the printer into Visual Basic applications that you write. Visual Basic communicates with the Windows printer driver so that you can send text and even graphics to the printer.<BR><P>Unlike most of Visual Basic's features, however, sending output to the printer can be a tedious process. Surprisingly, one of Visual Basic's weaknesses is also its strength: Printing requires that you send a fairly long list of instructions to your printer that describes exactly the way the output is to look. As easily as Visual Basic allows you to add and manage controls, one would have thought that the printing could be made easier. Nevertheless, despite the tedium sometimes associated with the printer, you'll see that you can control every aspect of printing, including the font of individual characters that your application sends to the printer. Therefore, the tedious control needed for the printer provides pinpoint accuracy that allows you to control all printing details.<BR><BR><A NAME="E68E157"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>A Word of Warning</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The Visual Basic Primer system that comes with this book doesn't support the use of the Print command on the File menu. You'll still be able to print reports from your applications, however.<BR><P>If you attempt to select Print from the File menu, the Visual Basic Primer system displays the error message box shown in Figure 21.1. This book's version of Visual Basic doesn't support the print commands that you would normally find on Visual Basic's File menu.<BR><P><B> <A HREF="21vel01.gif">Figure 21.1. The File Print command is unavailable.</A></B><BR><P>In case you upgrade to a different version of Visual Basic, you should know that the File menu's Print command usually allows you to produce printed copies of the code within an application. In addition, you would normally be able to print an application's forms graphically as well as produce printed descriptions of forms. Such output could provide documentation that you could file away and use as a starting point if you modify the application at a later date.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>If you attempt to select the Print command from the File menu, the Visual Basic Primer system produces a message box explaining that the feature is unavailable. Although you can't print form and code documentation, you will be able to produce text and graphic reports from the Visual Basic applications that you write by applying the properties and methods discussed in the rest of this unit.<BR><BR><A NAME="E68E158"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Printing With Windows</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>When your application sends output to the printer, Windows intercepts those printer commands. Rather than send output directly to the printer attached to your computer, Visual Basic actually sends printed output to the Windows Print Manager.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>The <I>Windows Print Manager</I> controls all printed output in Windows.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The Windows Print Manager determines how all printed output from all Windows programs eventually appears. Therefore, when your Visual Basic application attempts to send printed output directly to the printer, the Windows Print Manager intercepts those commands, and might change the output before the printer ever sees the output.<BR><P>The Windows Print Manager knows how to communicate with any printer supported by Windows. There are hundreds of different kinds of printers now recognized by Windows, and most of these printers require specialized commands. If every program that you bought had to provide support for every kind of printer that you or your users might own, programs would require even more disk space than they already do. In addition, programs would cost more because each software developer would have to spend the time writing the program to produce output on every kind of printer available.<BR><P>Rather than require that every software developer support all printers, the Windows Print Manager requires that every software developer support only <I>one</I> kind of printed output: the kind required by the Windows Print Manager. If the applications that you write need to produce printed output, Visual Basic produces that output in a form required by the Windows Print Manager. Figure 21.2 shows that Visual Basic applications send output directly to the Windows Print Manager. The Windows Print Manager <I>then</I> converts that output into the individual commands needed by whatever printer is attached to the system.<BR><P><B> <A HREF="21vel02.gif">Figure 21.2. The Windows Print Manager collects all program output and manages </B><B>individual printers.</A></B><BR><P>Suppose that you had both a laser printer and a dot matrix printer attached to your computer. Without the Windows Print Manager, you would need to provide two sets of printer commands for every Visual Basic application you write. With the Windows Print Manager, you'll need to provide only one generic set of printed output commands. Before running the application, you can use commands available in the Windows Print Manager to select one of your two printers. When you run the program, Windows will convert the Visual Basic output into commands needed by whatever printer is selected.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The Windows Print Manager simplifies communication with all the various printers. Your Visual Basic application needs only to send output to the Windows Print Manager no matter what kind of printer that output will eventually be directed to. The Windows Print Manager knows how to communicate with all Windows-supported printers, and converts your Visual Basic application's output to the chosen printer's required format.<BR><BR><A NAME="E68E159"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Tell the User You Will Print</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Users could be caught unaware if your application begins printing without first warning the user that the printer must be ready.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: Online</I> means the printer is ready for printing.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Always remind the user to turn on the printer, make sure that the printer has paper, and ensure that the printer is online. If the user's printer is not first turned on and ready with an ample paper supply, the user will receive a Windows Print Manager error message similar to the one shown in Figure 21.3.<BR><P><B> <A HREF="21vel03.gif">Figure 21.3. The Windows Print Manager senses that something isn't right.</A></B><BR><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>The function procedure in Listing 21.1 provides you with a useful MsgBox() call that you might want to incorporate into your own programs before printing.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>Warn the user that printing is about to take place to make sure that the user puts paper in the printer and turns the printer online.<BR><P><FONT COLOR="#000080"><B>Listing 21.1. Warns the user before printing.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Function PrReady()2: ' Make sure the user is ready to print3: Dim IsReady As Integer4: IsReady = MsgBox("Make sure the printer is ready", 1, "Printer Check")5: If (IsReady = 2) Then6: PrReady = 0 ' A Cancel press returns a False value7: End If8: PrReady = 1 ' User pressed OK so return True9: End Function</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Output: </I></B></FONT>Figure 21.4 shows the message box presented by Listing 21.1.<BR><P><B> <A HREF="21vel04.gif">Figure 21.4. The user now knows to prepare the printer for printing.</A></B><BR><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>After the user reads the message and responds to the message box in line 4, the procedure's return value determines whether the user wants to see the output (assume that the user has properly prepared the printer for printing) or cancel the printing. The return value of zero (meaning false) or one (meaning true) can be checked as follows from another procedure that prints based on the user's response:<BR><PRE><FONT COLOR="#000080">If PrReady() Then ' If function is true Call PrintRoutineEnd If</FONT></PRE><BR><A NAME="E68E160"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Introducing the </B><FONT COLOR="#FF8000"><B><I>Printer</I></B></FONT><B> Object</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Visual Basic applications send all printed output to a special Visual Basic object called the Printer object. The Printer object supports several property values and methods with which you determine the look of the printed output.<BR><P>The keyword Printer specifies the printer object to which your applications will direct all output. There is no Printer control on the Toolbox window. All access to Printer must take place using Visual Basic code.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>The commands that your application sends to the Printer object are generic Windows printer commands. The Windows Print Manager converts those generic commands to a specific printer's commands. You, therefore, worry about what you want printed and let the Windows Print Manager worry about <I>how</I> the output gets produced.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Throughout this book, when you learned a new object, such as the command button control, you learned about the properties that relate to that object. Before using the Printer object, you should see the properties available for the Printer object so that you'll know what kinds of things you can do with printed output from within Visual Basic. All of the Printer object's properties are listed in Table 21.1.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>A <I>pixel</I> is the smallest addressable point on the screen or printer.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 21.1. The </B><B>Printer</B><B> object's properties.</B></FONT></CENTER><BR><TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 ><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Property</I></FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>CurrentX</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Holds the horizontal print column, from the upper-left corner of the page, measured either in twips or the scale defined by the Scale properties.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>CurrentY</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Holds the vertical print row, from the upper-left corner of the page, measured either in twips or the scale defined by Scale properties.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>DrawMode</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Determines the appearance of graphics that you draw on the printer.</FONT>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?