Using the Printer Object to Print a Grid Control's ContentsLast reviewed: June 21, 1995Article ID: Q96941 |
The information in this article applies to:
- Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 1.0, 2.0, and 3.0
SUMMARYThe example program in this article shows you how to print the contents of a grid control using the Printer object.
MORE INFORMATIONThe example code prints a line border around the grid if the grid control BorderStyle is set to 1 and prints grid lines between the cells if GridLines is set to True.
Steps to Create Example Program1. Start Visual Basic or from the File menu, choose New Project (ALT, F, N)if Visual Basic is already running.
Sub Form_Click () ' Add sample data to the grid Dim i, j For i = 0 To Grid1.Cols - 1 For j = 0 To Grid1.Rows - 1 Grid1.Col = i Grid1.Row = j Grid1.Text = Format$(i + j + i ^ j) Next Next ' Print the data Call Grid_Print(Grid1) Printer.EndDoc End Sub
Sub Grid_Print (grid As Control) Dim tppx As Integer ' alias TwipsPerPixelX Dim tppy As Integer ' alias TwipsPerPixelY tppx = Printer.TwipsPerPixelX tppy = Printer.TwipsPerPixelY Dim Col As Integer ' index to grid columns Dim Row As Integer ' index to grid rows Dim x0 As Single ' upper left corner Dim y0 As Single ' " Dim x1 As Single ' position of text Dim y1 As Single ' " Dim x2 As Single ' position of grid lines Dim y2 As Single ' " ' set upper left corner x0 = Printer.CurrentX y0 = Printer.CurrentY ' draw the border around the grid If grid.BorderStyle <> 0 Then Printer.Line -Step(grid.Width - tppx, grid.Height - tppy), , B x0 = x0 + tppx y0 = y0 + tppy End If ' draw the text in the grid x1 = x0 For Col = 0 To grid.Cols - 1 ' skip non-visible columns If Col >= grid.FixedCols And Col < grid.LeftCol Then Col = grid.LeftCol End If ' stop if outside grid If x1 + grid.ColWidth(Col) >= grid.Width Then Exit For y1 = y0 For Row = 0 To grid.Rows - 1 ' skip non-visible columns If Row >= grid.FixedRows And Row < grid.TopRow Then Row = grid.TopRow End If ' stop if outside grid If y1 + grid.RowHeight(Row) >= grid.Height Then Exit For ' set position to print the cell Printer.CurrentX = x1 + tppx * 2 Printer.CurrentY = y1 + tppy ' print cell text grid.Col = Col grid.Row = Row Printer.Print grid.Text ' advance to next row y1 = y1 + grid.RowHeight(Row) If grid.GridLines Then y1 = y1 + tppy End If Next ' advance to next column x1 = x1 + grid.ColWidth(Col) If grid.GridLines Then x1 = x1 + tppx End If Next ' draw grid lines If grid.GridLines Then x2 = x0 y2 = y0 For Col = 0 To grid.Cols - 1 ' skip non-visible columns If Col >= grid.FixedCols And Col < grid.LeftCol Then Col = grid.LeftCol End If x2 = x2 + grid.ColWidth(Col) ' stop if outside grid If x2 >= grid.Width Then Exit For Printer.Line (x2, y0)-Step(0, y1 - tppy) x2 = x2 + tppx Next For Row = 0 To grid.Rows - 1 ' skip non-visible rows If Row >= grid.FixedRows And Row < grid.TopRow Then Row = grid.TopRow End If y2 = y2 + grid.RowHeight(Row) ' stop if outside grid If y2 >= grid.Height Then Exit For Printer.Line (x0, y2)-Step(x1 - tppx, 0) y2 = y2 + tppy Next End If End Sub
|
Additional reference words: 1.00 2.00 3.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |