home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Advanced Visual Basic Techniques
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471188816
Publication Date: 06/01/97

Search this book:
 
Previous Table of Contents Next


ExpenseReporter uses several printer support subroutines. The PrintString subroutine displays text on a picture box or printer object. It takes parameters that specify such things as the text string to be printed, its placement, and the width and height of an optional box to draw around the text. When it is finished, PrintString advances the variables holding the string’s position by specified amounts so that the program is ready to output the next string.

Sub PrintString(obj As Object, txt As String, cur_x As Single, _
    cur_y As Single, x_off As Single, y_off As Single, _
    ByVal wid As Single, hgt As Single, alignment As Integer, _
    x_add As Single, y_add As Single)

    If wid <= 0 Then wid = obj.TextWidth(txt)

    ‘ Draw the box if desired.
    If hgt > 0 Then _
        obj.Line (cur_x, cur_y)-Step(wid, hgt), , B

    Select Case alignment
        Case vbCenter
            obj.CurrentX = cur_x + (wid - _
                obj.TextWidth(txt)) / 2
        Case vbLeftJustify
            obj.CurrentX = cur_x + x_off
        Case vbRightJustify
            obj.CurrentX = cur_x + wid - _
                obj.TextWidth(txt) - x_off
    End Select

    ‘ Draw the text.
    obj.CurrentY = cur_y + y_off
    obj.Print txt;

    cur_x = cur_x + x_add
    cur_y = cur_y + y_add
End Sub

The PrintMultiline subroutine displays a text string that might span multiple lines. It uses the Get-Token function to break out pieces of the text that are separated by pairs of carriage returns and line feeds.

Sub PrintMultiline(obj As Object, txt As String, cur_x As Single, _
    cur_y As Single, y_off As Single, x_off As Single, line_hgt)
Dim got_token As Boolean
Dim max_x As Single
Dim txt_line As String

    cur_x = cur_x + x_off
    max_x = cur_x
    cur_y = cur_y + y_off

    got_token = GetToken(txt, vbCrLf, txt_line)
    Do While got_token
        obj.CurrentX = cur_x
        obj.CurrentY = cur_y
        obj.Print txt_line;
        If max_x < obj.CurrentX Then _
            max_x = obj.CurrentX
        cur_y = cur_y + line_hgt
        got_token = GetToken(“”, vbCrLf, txt_line)
    Loop
    cur_x = max_x
End Sub

Public Function GetToken(txt As String, delim As String, _
    token As String) As Boolean
Static the_text As String
Dim pos As Integer

    If txt <> “” Then the_text = txt
    pos = InStr(the_text, delim)
    If pos <> 0 Then
        GetToken = True
        token = Left$(the_text, pos - 1)
        the_text = Mid$(the_text, pos + Len(delim))
    Else
        GetToken = (the_text <> “”)
        token = the_text
        the_text = “”
    End If
End Function

Using these routines, ExpenseReporter displays an expense report on a picture box or on the Printer object. The PrintDocument routine that accomplishes this is quite long and not tremendously interesting. Most of the code deals with creating a nice arrangement of text and lines. You can examine the code on the compact disk if you want to see the details. A few parts of this subroutine, however, are worth special attention.

All output created by PrintDocument is specified in the Printer object’s units. If the application uses PrintDocument to send output to a picture box, the picture box must be sized and scaled so its coordinates match those of the printer. This is explained in more detail in the following section, “Print Previewing.”

The following code, for example, uses the Printer object’s ScaleLeft, ScaleTop, ScaleWidth, and ScaleHeight properties to determine where the output’s margins should be. Printing routines would make sure their output remained within the calculated margins.

‘ Set margins to 1/2 inch on the sides and
‘ 1 inch at the top and bottom.
left_margin = Printer.ScaleLeft + 1440 * 0.5
right_margin = Printer.ScaleLeft + _
    Printer.ScaleWidth - 1440 * 0.5
top_margin = Printer.ScaleTop + 1440 * 1
bottom_margin = Printer.ScaleTop + _
    Printer.ScaleHeight - 1440 * 1

Before ExpenseReporter’s PrintDocument subroutine creates any output, it sets the font of the printer or picture box to 8 point Arial. It sets several other font characteristics to be certain the Printer object uses the correct font.

obj.Font.Bold = False
obj.Font.Italic = False
obj.Font.Name = “Arial”
obj.Font.Strikethrough = False
obj.Font.Underline = False
obj.Font.Size = 8

By default, picture boxes use a system-defined font such as MS Sans Serif. This font is shipped with Windows and is very likely to be present on any user’s computer. Unfortunately, this font is unlikely to be found on a user’s physical printer. By specifying a font like Arial or Times Roman, fonts that are common on printers, PrintDocument increases the chances that output to the printer will look like output to a picture box. This is important because it means the printed output will look as much like the previewed output as possible.

ExpenseReporter uses the PrintDocument function, as shown in the following code. Notice that this subroutine performs form validations before it invokes PrintDocument.

Private Sub mnuFilePrint_Click()
Dim form_valid As Boolean

    ‘ Make sure the fields are valid.
    ‘ Ignore modifications made by ValidForm.
    IgnoreModify = True
    form_valid = ValidForm()
    IgnoreModify = False
    If Not form_valid Then Exit Sub

    ‘ Start waiting.
    WaitStart

    ‘ Print to the printer.
    PrintDocument Printer

    ‘ Close the document.
    Printer.EndDoc

    ‘ End waiting.
    WaitEnd
End Sub

Figure 2.3 shows printed output produced by the PrintDocument subroutine.

Print Previewing

A print preview feature allows the user to see how a printed document will look before it is printed. Figure 2.4 shows ExpenseReporter’s print preview screen.

At first one might think implementing a Print Preview command would be quite difficult. It seems reasonable that building a Print Preview command should be at least as hard as actually printing the document. In reality, it takes only a little more work to add print preview capabilities to an application that provides high-resolution printed output. Because this feature makes an application appear more professional, the extra work is well worth the effort.

The document printing routines described in the previous section, PrintString, PrintMultiline, and PrintDocument, all take as their first parameter an object. This parameter is the object to which the routine should send its output. ExpenseReporter uses the following line of code to pass the Printer object into the PrintDocument subroutine so that it can create a printout.

PrintDocument Printer

To create a print preview, the program passes PrintDocument a picture box instead of the Printer object. PrintDocument sends an image of the printout to the picture box instead of the physical printer. The program uses that picture box to give the user a preview of the printout. The basic idea is simple, though a few details require a bit of work.

Many SDI applications present a print preview on the same form where they display the program’s data. The application hides the controls on the main form and makes new controls visible to display the preview. In this case, the application also needs to rearrange menu items to provide scaling and other commands that are appropriate for print previewing. All this makes coding the main form a bit confusing. ExpenseReporter takes a different approach.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.