home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / prtgrid / prtdemo.bas < prev    next >
Encoding:
BASIC Source File  |  1995-09-06  |  6.5 KB  |  264 lines

  1. Option Explicit
  2.  
  3. ' VB constants
  4.  
  5. Const DRAW_COPY_PEN = 13
  6. Const DRAW_SOLID = 0
  7.  
  8. Const COLOR_BLACK = &H0&
  9.  
  10. Const FILL_SOLID = 0
  11. Const FILL_TRANSPARENT = 1
  12.  
  13. ' Windows API constants
  14.  
  15. Const WM_SETREDRAW = &HB
  16.  
  17. Const DT_WORDBREAK = &H10
  18. Const DT_EXTERNALLEADING = &H200
  19. Const DT_NOPREFIX = &H800
  20.  
  21. ' Windows API structures
  22.  
  23. Type utRECT
  24.     Left As Integer
  25.     Top As Integer
  26.     Right As Integer
  27.     Bottom As Integer
  28. End Type
  29.  
  30. ' Windows API function declarations
  31.  
  32. Declare Function GetTextExtent Lib "GDI" (ByVal hDC As Integer, ByVal lpString As String, ByVal nCount As Integer) As Long
  33. Declare Function SendMessage Lib "User" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, lParam As Any) As Long
  34. Declare Function DrawText Lib "User" (ByVal hDC As Integer, ByVal lpStr As String, ByVal nCount As Integer, lpRect As utRECT, ByVal wFormat As Integer) As Integer
  35. Declare Function GetDC Lib "User" (ByVal hWnd As Integer) As Integer
  36.  
  37. ' module constants
  38.  
  39. Const RECT_OFFSET = 30
  40.  
  41. ' module variables
  42.  
  43. Dim RC As Integer
  44.  
  45. Dim GridRowCount As Integer
  46. Dim GridColCount As Integer
  47. Dim GridFixRows As Integer
  48. Dim GridFixCols As Integer
  49.  
  50. Dim PageStartX As Single
  51. Dim PageStartY As Single
  52. Dim PageEndX As Single
  53. Dim PageEndY As Single
  54.  
  55. Dim PrintTwipsX As Single
  56. Dim PrintTwipsY As Single
  57. Dim PrintScaleX As Single
  58. Dim PrintScaleY As Single
  59. Dim PrintFontHeight As Single
  60. Dim PrintX As Single
  61. Dim PrintY As Single
  62. Dim PrintWidth As Single
  63. Dim PrintHeight As Single
  64. Dim PrintText As String
  65.  
  66. Dim TextRect As utRECT
  67.  
  68. Dim ctlGrid As Control
  69.  
  70. ' user-defined types
  71.  
  72. Type utPageInfo
  73.     Margin As Single
  74.     FontName As String
  75.     FontSize As Integer
  76.     FontBold As Integer
  77.     FontItalic As Integer
  78.     FixedShade As Integer
  79. End Type
  80.  
  81. ' global variables
  82.  
  83. Global PageInfo As utPageInfo
  84.  
  85. Private Sub CalcHeight (RowIndex As Integer)
  86.  
  87.     ' claculate PrintHeight for a given row using the RowHeight
  88.  
  89.     PrintHeight = (((ctlGrid.RowHeight(RowIndex) * PrintScaleY) \ PrintTwipsY) + 1) * PrintTwipsY
  90.  
  91. End Sub
  92.  
  93. Private Sub InitPage ()
  94.  
  95.     ' set Printer properties for a new page
  96.  
  97.     Printer.DrawMode = DRAW_COPY_PEN
  98.     Printer.DrawStyle = DRAW_SOLID
  99.     Printer.DrawWidth = 1
  100.     Printer.ForeColor = COLOR_BLACK
  101.     Printer.FillStyle = FILL_TRANSPARENT
  102.  
  103.     ' print something so DrawText uses our Printer properties
  104.  
  105.     Printer.Print " "
  106.  
  107.     PrintY = PageStartY
  108.  
  109. End Sub
  110.  
  111. Private Sub InitPrint ()
  112.  
  113.     Dim SrceExt As Long
  114.     Dim DestExt As Long
  115.     Dim Gutter As Single
  116.  
  117.     Printer.Scale
  118.  
  119.     PrintTwipsX = Printer.TwipsPerPixelX
  120.     PrintTwipsY = Printer.TwipsPerPixelY
  121.  
  122.     ' set page variables based on print area and margins
  123.  
  124.     Gutter = (Printer.Width - Printer.ScaleWidth) / 2
  125.     PageStartX = PageInfo.Margin - Gutter
  126.     PageEndX = Printer.ScaleWidth - (PageInfo.Margin - Gutter)
  127.  
  128.     Gutter = (Printer.Height - Printer.ScaleHeight) / 2
  129.     PageStartY = PageInfo.Margin - Gutter
  130.     PageEndY = Printer.ScaleHeight - (PageInfo.Margin - Gutter)
  131.  
  132.     ' set Printer font and calcuate scale ratios based on FontSize + 2
  133.  
  134.     Printer.FontName = PageInfo.FontName
  135.     Printer.FontSize = PageInfo.FontSize + 2
  136.     Printer.FontBold = PageInfo.FontBold
  137.     Printer.FontItalic = PageInfo.FontItalic
  138.  
  139.     SrceExt = GetTextExtent(GetDC(ctlGrid.hWnd), "GgIiWw ", 7)
  140.     DestExt = GetTextExtent(Printer.hDC, "GgIiWw ", 7)
  141.     PrintScaleX = ((DestExt And &HFFFF) * PrintTwipsX) / ((SrceExt And &HFFFF) * Screen.TwipsPerPixelX)
  142.     PrintScaleY = ((DestExt \ &H10000) * PrintTwipsY) / ((SrceExt \ &H10000) * Screen.TwipsPerPixelY)
  143.  
  144.     Printer.FontSize = PageInfo.FontSize
  145.     PrintFontHeight = Printer.TextHeight("Gg")
  146.  
  147.     ' set shading for fixed rows/cols
  148.  
  149.     Printer.FillColor = RGB(PageInfo.FixedShade, PageInfo.FixedShade, PageInfo.FixedShade)
  150.  
  151. End Sub
  152.  
  153. Private Function OutputRow (RowIndex As Integer) As Integer
  154.  
  155.     Dim ColIndex As Integer
  156.  
  157.     ctlGrid.Row = RowIndex
  158.     PrintX = PageStartX
  159.  
  160.     CalcHeight RowIndex
  161.  
  162.     ' output the text in each cell for a row
  163.  
  164.     For ColIndex = 0 To GridColCount
  165.  
  166.         ctlGrid.Col = ColIndex
  167.         PrintWidth = (((ctlGrid.ColWidth(ColIndex) * PrintScaleX) \ PrintTwipsX) + 1) * PrintTwipsX
  168.         PrintText = ctlGrid.Text
  169.  
  170.         OutputText ((RowIndex < GridFixRows) Or (ColIndex < GridFixCols))
  171.  
  172.         PrintX = PrintX + PrintWidth
  173.         If PrintX >= PageEndX Then Exit For
  174.  
  175.     Next
  176.  
  177.     PrintY = PrintY + PrintHeight
  178.  
  179.     ' return True if the next row falls on a new page
  180.  
  181.     If RowIndex < GridRowCount Then
  182.         CalcHeight RowIndex + 1
  183.         OutputRow = ((PrintY + PrintHeight) >= PageEndY)
  184.     Else
  185.         OutputRow = False
  186.     End If
  187.  
  188. End Function
  189.  
  190. Private Sub OutputText (FillState As Integer)
  191.  
  192.     ' set output rectangle for DrawText
  193.  
  194.     TextRect.Left = (PrintX + RECT_OFFSET) / PrintTwipsX
  195.     TextRect.Top = (PrintY + RECT_OFFSET) / PrintTwipsY
  196.     TextRect.Right = (PrintX + PrintWidth - RECT_OFFSET) / PrintTwipsX
  197.     TextRect.Bottom = (PrintY + ((PrintHeight \ PrintFontHeight) * PrintFontHeight) - RECT_OFFSET) / PrintTwipsY
  198.  
  199.     ' send text to Printer using API function
  200.  
  201.     RC = DrawText(Printer.hDC, PrintText, Len(PrintText), TextRect, DT_WORDBREAK Or DT_NOPREFIX Or DT_EXTERNALLEADING)
  202.  
  203.     ' border the cell area
  204.  
  205.     Printer.FillStyle = IIf(FillState, FILL_SOLID, FILL_TRANSPARENT)
  206.     Printer.Line (PrintX, PrintY)-Step(PrintWidth, PrintHeight), , B
  207.  
  208. End Sub
  209.  
  210. Sub PrintGrid (grdCtl As Control)
  211.  
  212.     Dim NewState As Integer
  213.     Dim RowIndex As Integer
  214.     Dim FixIndex As Integer
  215.  
  216.     ' initialize module variables and disable grid redraw
  217.  
  218.     Set ctlGrid = grdCtl
  219.  
  220.     InitPrint
  221.     SetRedraw False
  222.  
  223.     GridRowCount = ctlGrid.Rows - 1
  224.     GridColCount = ctlGrid.Cols - 1
  225.     GridFixRows = ctlGrid.FixedRows
  226.     GridFixCols = ctlGrid.FixedCols
  227.  
  228.     NewState = True
  229.  
  230.     ' output all rows starting with the first non-fixed row
  231.  
  232.     For RowIndex = GridFixRows To GridRowCount
  233.  
  234.         If NewState Then
  235.             
  236.             ' output fixed rows for each new page
  237.             
  238.             InitPage
  239.             For FixIndex = 0 To (GridFixRows - 1)
  240.                 NewState = OutputRow(FixIndex)
  241.             Next
  242.  
  243.         End If
  244.  
  245.         NewState = OutputRow(RowIndex)
  246.         If NewState Then Printer.NewPage
  247.  
  248.     Next
  249.  
  250.     ' send document and enable grid redraw
  251.  
  252.     Printer.EndDoc
  253.     SetRedraw True
  254.  
  255. End Sub
  256.  
  257. Private Sub SetRedraw (State As Integer)
  258.  
  259.     If SendMessage(ctlGrid.hWnd, WM_SETREDRAW, State, 0) Then
  260.     End If
  261.  
  262. End Sub
  263.  
  264.