Results 1 to 4 of 4

Thread: printing a form

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2010
    Posts
    76

    printing a form

    hello, I don't know hot print my form on the printer

    I want to print the list in my listbox and the captions in my labels, how can I do that?

    form3.PrintForm is not working.

  2. #2
    Junior Member
    Join Date
    Dec 2011
    Location
    KZN South Africa
    Posts
    23

    Re: printing a form

    Hi Mark,

    This is an old problem of mine, started back in VB3. I got a module that sorts it out which I've modified for VB5 & VB6.

    to use it add this to a print screen command button or other event
    vb Code:
    1. Set picPrintScreen.Picture = CaptureForm(Me)
    2.         Call PrintScreen.PrintPictureToFitPage(Printer, picPrintScreen.Picture)
    3.         Printer.EndDoc
    4.         Set picPrintScreen.Picture = Nothing

    here is the code from my module PrintScr.bas. Hope it works for you. Sam

    vb Code:
    1. Attribute VB_Name = "PrintScreen"
    2. Option Explicit
    3.  
    4. Option Base 0
    5.  
    6. Private Type PALETTEENTRY
    7.         peRed As Byte
    8.         peGreen As Byte
    9.         peBlue As Byte
    10.         peFlags As Byte
    11. End Type
    12.  
    13. Private Type LOGPALETTE
    14.    palVersion As Integer
    15.    palNumEntries As Integer
    16.    palPalEntry(255) As PALETTEENTRY  ' Enough for 256 colors
    17. End Type
    18.  
    19. Private Type GUID
    20.         Data1 As Long
    21.         Data2 As Integer
    22.         Data3 As Integer
    23.         Data4(7) As Byte
    24. End Type
    25.  
    26.  
    27. Private Const RASTERCAPS As Long = 38
    28. Private Const RC_PALETTE As Long = &H100
    29. Private Const SIZEPALETTE As Long = 104
    30.  
    31. Private Type RECT
    32.         Left As Long
    33.         Top As Long
    34.         Right As Long
    35.         Bottom As Long
    36. End Type
    37.  
    38. Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Long) As Long
    39. Private Declare Function CreateCompatibleBitmap Lib "GDI32" ( _
    40.     ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    41. Private Declare Function GetDeviceCaps Lib "GDI32" (ByVal hDC As Long, _
    42.         ByVal iCapabilitiy As Long) As Long
    43. Private Declare Function GetSystemPaletteEntries Lib "GDI32" ( _
    44.         ByVal hDC As Long, ByVal wStartIndex As Long, _
    45.         ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) As Long
    46. Private Declare Function CreatePalette Lib "GDI32" (lpLogPalette As LOGPALETTE) As Long
    47. Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Long, ByVal hObject As Long) As Long
    48. Private Declare Function BitBlt Lib "GDI32" (ByVal hDCDest As Long, _
    49.     ByVal XDest As Long, ByVal YDest As Long, ByVal nWidth As Long, _
    50.     ByVal nHeight As Long, ByVal hDCSrc As Long, ByVal XSrc As Long, _
    51.     ByVal YSrc As Long, ByVal dwRop As Long) As Long
    52. Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Long) As Long
    53. Private Declare Function GetForegroundWindow Lib "USER32" () As Long
    54. Private Declare Function SelectPalette Lib "GDI32" (ByVal hDC As Long, _
    55.     ByVal hPalette As Long, ByVal bForceBackground As Long) As Long
    56. Private Declare Function RealizePalette Lib "GDI32" (ByVal hDC As Long) As Long
    57. Private Declare Function GetWindowDC Lib "USER32" (ByVal hWnd As Long) As Long
    58. Private Declare Function GetDC Lib "USER32" (ByVal hWnd As Long) As Long
    59. Private Declare Function GetWindowRect Lib "USER32" (ByVal hWnd As Long, lpRect As RECT) As Long
    60. Private Declare Function ReleaseDC Lib "USER32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
    61. Private Declare Function GetDesktopWindow Lib "USER32" () As Long
    62.  
    63. Private Type PicBmp
    64.         Size As Long
    65.         Type As Long
    66.         hBmp As Long
    67.         hPal As Long
    68.         Reserved As Long
    69. End Type
    70.  
    71. Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" ( _
    72.   PicDesc As PicBmp, RefIID As GUID, _
    73.   ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
    74.  
    75.  
    76. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    77. '
    78. ' CreateBitmapPicture
    79. '    - Creates a bitmap type Picture object from a bitmap and palette
    80. '
    81. ' hBmp
    82. '    - Handle to a bitmap
    83. '
    84. ' hPal
    85. '    - Handle to a Palette
    86. '    - Can be null if the bitmap doesn't use a palette
    87. '
    88. ' Returns
    89. '    - Returns a Picture object containing the bitmap
    90. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    91. '
    92. Public Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As Picture
    93.  
    94.     Dim r As Long
    95.     Dim Pic As PicBmp
    96.     ' IPicture requires a reference to "Standard OLE Types"
    97.     Dim IPic As IPicture
    98.     Dim IID_IDispatch As GUID
    99.  
    100.     ' Fill in with IDispatch Interface ID
    101.     With IID_IDispatch
    102.         .Data1 = &H20400
    103.         .Data4(0) = &HC0
    104.         .Data4(7) = &H46
    105.     End With
    106.    
    107.     ' Fill Pic with necessary parts
    108.     With Pic
    109.         .Size = Len(Pic)          ' Length of structure
    110.         .Type = vbPicTypeBitmap   ' Type of Picture (bitmap)
    111.         .hBmp = hBmp              ' Handle to bitmap
    112.         .hPal = hPal              ' Handle to palette (may be null)
    113.     End With
    114.    
    115.     ' Create Picture object
    116.     r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)
    117.    
    118.     ' Return the new Picture object
    119. Set CreateBitmapPicture = IPic
    120. End Function
    121.  
    122. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    123. '
    124. ' CaptureWindow
    125. '    - Captures any portion of a window
    126. '
    127. ' hWndSrc
    128. '    - Handle to the window to be captured
    129. '
    130. ' Client
    131. '    - If True CaptureWindow captures from the client area of the window
    132. '    - If False CaptureWindow captures from the entire window
    133. '
    134. ' LeftSrc, TopSrc, WidthSrc, HeightSrc
    135. '    - Specify the portion of the window to capture
    136. '    - Dimensions need to be specified in pixels
    137. '
    138. ' Returns
    139. '    - Returns a Picture object containing a bitmap of the specified
    140. '      portion of the window that was captured
    141. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    142. '
    143. Public Function CaptureWindow(ByVal hWndSrc As Long, _
    144.       ByVal Client As Boolean, ByVal LeftSrc As Long, _
    145.       ByVal TopSrc As Long, ByVal WidthSrc As Long, _
    146.       ByVal HeightSrc As Long) As Picture
    147.    
    148.     Dim hDCMemory As Long
    149.     Dim hBmp As Long
    150.     Dim hBmpPrev As Long
    151.     Dim r As Long
    152.     Dim hDCSrc As Long
    153.     Dim hPal As Long
    154.     Dim hPalPrev As Long
    155.     Dim RasterCapsScrn As Long
    156.     Dim HasPaletteScrn As Long
    157.     Dim PaletteSizeScrn As Long
    158.     Dim LogPal As LOGPALETTE
    159.  
    160.     ' Depending on the value of Client get the proper device context
    161.     If Client Then
    162.       hDCSrc = GetDC(hWndSrc) ' Get device context for client area
    163.     Else
    164.       hDCSrc = GetWindowDC(hWndSrc) ' Get device context for entire window
    165.     End If
    166.    
    167.     ' Create a memory device context for the copy process
    168.     hDCMemory = CreateCompatibleDC(hDCSrc)
    169.     ' Create a bitmap and place it in the memory DC
    170.     hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
    171.     hBmpPrev = SelectObject(hDCMemory, hBmp)
    172.    
    173.     ' Get screen properties
    174.     RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS)   ' Raster capabilities
    175.     HasPaletteScrn = RasterCapsScrn And RC_PALETTE       ' Palette support
    176.     PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE) ' Size of palette
    177.    
    178.     ' If the screen has a palette make a copy and realize it
    179.     If HasPaletteScrn And (PaletteSizeScrn = 256) Then
    180.         ' Create a copy of the system palette
    181.         LogPal.palVersion = &H300
    182.         LogPal.palNumEntries = 256
    183.         r = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry(0))
    184.         hPal = CreatePalette(LogPal)
    185.         ' Select the new palette into the memory DC and realize it
    186.         hPalPrev = SelectPalette(hDCMemory, hPal, 0)
    187.         r = RealizePalette(hDCMemory)
    188.     End If
    189.    
    190.     ' Copy the on-screen image into the memory DC
    191.     r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, _
    192.         LeftSrc, TopSrc, vbSrcCopy)
    193.    
    194.     ' Remove the new copy of the the on-screen image
    195.     hBmp = SelectObject(hDCMemory, hBmpPrev)
    196.    
    197.     ' If the screen has a palette get back the palette that was selected
    198.     ' in previously
    199.     If HasPaletteScrn And (PaletteSizeScrn = 256) Then
    200.       hPal = SelectPalette(hDCMemory, hPalPrev, 0)
    201.     End If
    202.    
    203.     ' Release the device context resources back to the system
    204.     r = DeleteDC(hDCMemory)
    205.     r = ReleaseDC(hWndSrc, hDCSrc)
    206.    
    207.     ' Call CreateBitmapPicture to create a picture object from the bitmap
    208.     ' and palette handles.  Then return the resulting picture object.
    209.     Set CaptureWindow = CreateBitmapPicture(hBmp, hPal)
    210. End Function
    211.  
    212.  
    213. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    214. '
    215. ' CaptureForm
    216. '    - Captures an entire form including title bar and border
    217. '
    218. ' frmSrc
    219. '    - The Form object to capture
    220. '
    221. ' Returns
    222. '    - Returns a Picture object containing a bitmap of the entire form
    223. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    224. '
    225. Public Function CaptureForm(frmSrc As Form) As Picture
    226.     ' Call CaptureWindow to capture the entire form given it's window
    227.     ' handle and then return the resulting Picture object
    228.     Set CaptureForm = CaptureWindow(frmSrc.hWnd, False, 0, 0, _
    229.         frmSrc.ScaleX(frmSrc.Width, vbTwips, vbPixels), _
    230.         frmSrc.ScaleY(frmSrc.Height, vbTwips, vbPixels))
    231. End Function
    232.  
    233.  
    234. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    235. '
    236. ' PrintPictureToFitPage
    237. '    - Prints a Picture object as big as possible
    238. '
    239. ' Prn
    240. '    - Destination Printer object
    241. '
    242. ' Pic
    243. '    - Source Picture object
    244. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    245. '
    246. Public Sub PrintPictureToFitPage(Prn As Printer, Pic As Picture)
    247.     Const vbHiMetric As Integer = 8
    248.     Dim Pic_height As Double
    249.     Dim Pic_width As Double
    250.     Dim PicRatio As Double
    251.     Dim PrnWidth As Double
    252.     Dim PrnHeight As Double
    253.     Dim PrnRatio As Double
    254.     Dim PrnPicWidth As Double
    255.     Dim PrnPicHeight As Double
    256.     Dim PicStartX As Double
    257.     Dim PicStartY As Double
    258.    
    259.  
    260.     ' Determine if picture should be printed in landscape or portrait and
    261.     ' set the orientation
    262.     If Pic.Height >= Pic.Width Then
    263.         Prn.Orientation = vbPRORPortrait   ' Taller than wide
    264.     Else
    265.         Prn.Orientation = vbPRORLandscape  ' Wider than tall
    266.     End If
    267.    
    268.     ' Calculate device independent Width to Height ratio for picture
    269.     Pic_width = Pic.Width
    270.     Pic_height = Pic.Height
    271.     PicRatio = Pic_width / Pic_height
    272.    
    273.     ' Calculate the dimentions of the printable area in HiMetric
    274.     PrnWidth = Prn.Width
    275.     PrnHeight = Prn.Height
    276.     ' Calculate device independent Width to Height ratio for printer
    277.     PrnRatio = PrnWidth / PrnHeight
    278.    
    279.     ' Scale the output to the printable area
    280.     If PicRatio >= PrnRatio Then
    281.         ' Scale picture to fit full width of printable area
    282.         PrnPicWidth = 0.8 * Prn.Width
    283.         PrnPicHeight = 0.8 * Prn.Height * PrnRatio / PicRatio
    284.         PicStartX = (PrnWidth - PrnPicWidth) / 2
    285.         PicStartY = (PrnHeight - PrnPicHeight) / 2
    286.    
    287.     Else
    288.         ' Scale picture to fit full height of printable area
    289.         PrnPicWidth = 0.8 * Prn.Width * PicRatio / PrnRatio
    290.         PrnPicHeight = 0.8 * Prn.Height
    291.         PicStartX = (PrnWidth - PrnPicWidth) / 2
    292.         PicStartY = (PrnHeight - PrnPicHeight) / 2
    293.     End If
    294.    
    295.     Prn.PaintPicture Pic, PicStartX, PicStartY, PrnPicWidth, PrnPicHeight
    296.  
    297. End Sub
    298.  
    299.  
    300. Public Sub PrintPicturePortrait(Prn As Printer, Pic As Picture, sngFraction As Single)
    301.     Const vbHiMetric As Integer = 8
    302.     Dim Pic_height As Double
    303.     Dim Pic_width As Double
    304.     Dim PicRatio As Double
    305.     Dim PrnWidth As Double
    306.     Dim PrnHeight As Double
    307.     Dim PrnRatio As Double
    308.     Dim PrnPicWidth As Double
    309.     Dim PrnPicHeight As Double
    310.     Dim PicStartX As Double
    311.     Dim PicStartY As Double
    312.    
    313.     Prn.Orientation = vbPRORPortrait   ' Taller than wide
    314.    
    315.     ' Calculate device independent Width to Height ratio for picture
    316.     Pic_width = Pic.Width
    317.     Pic_height = Pic.Height
    318.     PicRatio = Pic_width / Pic_height
    319.    
    320.     ' Calculate the dimentions of the printable area
    321.     PrnWidth = Prn.Width
    322.     PrnHeight = Prn.Height
    323.     ' Calculate device independent Width to Height ratio for printer
    324.     PrnRatio = PrnWidth / PrnHeight
    325.    
    326.     ' Scale the output to the printable area
    327.     If PicRatio >= PrnRatio Then
    328.         ' Scale picture to fit full width of printable area
    329.         PrnPicWidth = sngFraction * Prn.Width
    330.         PrnPicHeight = sngFraction * Prn.Height * PrnRatio / PicRatio
    331.         PicStartX = (PrnWidth - PrnPicWidth) / 2
    332.         PicStartY = (PrnHeight - PrnPicHeight) / 2
    333.    
    334.     Else
    335.         ' Scale picture to fit full height of printable area
    336.         PrnPicWidth = sngFraction * Prn.Width * PicRatio / PrnRatio
    337.         PrnPicHeight = sngFraction * Prn.Height
    338.         PicStartX = (PrnWidth - PrnPicWidth) / 2
    339.         PicStartY = (PrnHeight - PrnPicHeight) / 2
    340.     End If
    341.    
    342.     Prn.PaintPicture Pic, PicStartX, PicStartY, PrnPicWidth, PrnPicHeight
    343.  
    344. End Sub

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Location
    KZN South Africa
    Posts
    23

    Re: printing a form

    Hi from Sam again,

    I forgot to mention that you need a picturebox on your form (picPrintScreen). It can be any size and hidden.

  4. #4
    New Member
    Join Date
    Dec 2011
    Posts
    7

    Re: printing a form

    Anothe idea, just to get a print out of your form.
    Press PrtSc (top rightish on my keyborad) This copies your screen to the clipboard.
    Load Paint, and use edit to paste in the image.
    Select the part you want from the image of the whole screen,use Edit and Copy (the selected image back to the clipboard)
    Paint, New, and Paste again. Now do a Save As or print. If save as, I then load the save image into Word as an image and print from there.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width