A More Complex Print Preview Example

Listing 9.18 contains a more complex example of how the print preview functions work. This is essentially the same function as in Listing 9.12 earlier in the chapter, but it is much shorter due to the processing done in the Write routines.

Listing 9.18: PrintGrid Routine in Charter

Private Sub PrintGrid(o As Object)
Dim i As Single
Dim j As Single
Dim t As String
WriteNewPage o, 1
WriteCenter MSChart1.TitleText, o, 0, 720, 1440 * 8.5, 720, True, , , 24
For i = 0 To MSFlexGrid1.Rows
   WriteLine 1440, 1440 + i * 720, 1440 + 1440 * MSFlexGrid1.Cols, _
      1440 + i * 720, o
Next i
For i = 0 To MSFlexGrid1.Cols
   WriteLine 1440 + i * 1440, 1440, 1440 + i * 1440, _
      1440 + MSFlexGrid1.Rows * 720, o
Next i
For i = 1 To MSFlexGrid1.Cols - 1
   WriteCenter Chr(Asc(“@”) + i), o, 1440 + 1440 * i, 1440, _
      2880 + 1440 * i, 1440 + 720, True
Next i
   
For i = 1 To MSFlexGrid1.Rows - 1
   WriteRight FormatNumber(i, 0) & “  “, o, 2880, 720 * (i + 1), 2880, _
      2160 + 720 * (i + 1), True
Next i
For i = 1 To MSFlexGrid1.Cols - 1
   For j = 1 To MSFlexGrid1.Rows - 1
       WriteRight MSFlexGrid1.TextMatrix(j, i) & “  “, o, 2880 + 1440 * i, _
          1440 + 720 * j, 2880 + 1440 * i, 2160 + 720 * j
   Next j
Next i
WriteEndDoc o
End Sub

I begin this routine by calling the WriteNewPage to make sure that I’m on the first page. I continue by displaying the chart title at the top of the page. Since I can override the default values for this statement, I choose to output the title in 24-point bold. Then I draw the lines to form the grid.

Next, I display the column headers and row headers. Again, because it’s easy to override the default font characteristics, I can easily specify that these values are written in bold without doing a lot of work to get this result. Also, I know that the defaults are restored, so I don’t need to worry about them during the next call. Note that I output a couple of spaces after each right-justified field to make sure that the field doesn’t touch the right end of the cell.

Then I output the actual contents of the cells in much the same fashion as I output the row headers. Finally, I call the WriteEndDoc routine to close the spool file and send it to the printer.

© 1998 SYBEX Inc. All rights reserved.