Printing with Visual Basic: The source code
19 October 2008
Example 1. Print an Image 3 Inches wide
In this example we'll print an image 3 inches wide at the top of the page. The image has previously been loaded and is defined by vimage, a global variable that holds the image descriptor.
Requires Victor Image Processing Library for 32-bit Windows v 5.1 or higher. Find it here.
........... Add these declarations to your Global module ...........
Declare Function printimage Lib "VIC32.DLL" (ByVal hWnd As Long, ByVal hdcprn As Long, ByVal mode As Long, image As imgdes, pRect As RECT, ByVal boxsiz As Long, ByVal dspfct As Long) As Long
Declare Sub RtlMoveMemory Lib "kernel32" (ByVal des As Long, ByVal src As Long, ByVal cnt As Long)
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (des As Any, ByVal src As Long, ByVal cnt As Long)
............ Add this function to your .BAS module, not to a .FRM module ...........
Public Function print_an_image(hWnd As Long) As Long
Dim rcode As Long
Dim pRect As RECT
Dim boxsize As Long, mode As Long
Dim bm As BITMAPINFOHEADER
boxsize = 0 ' Size of frame around printed image
mode = 0 ' Use default print mode
' Get Bitmap info
CopyMemory bm, vimage.bmh, 40 ' 40=size of BITMAPINFOHEADER
pRect.top = 0 ' Position of printed area on printed page in 1/1000 inch units
pRect.left = 1000 ' One inch from left
pRect.right = pRect.left + 3000 ' Print image 3 inches wide
' Maintain aspect ratio of image
pRect.bottom = (pRect.right - pRect.left) * bm.biHeight / bm.biWidth
' Print the image
rcode = printimage(hWnd, Printer.hdc, mode, vimage, pRect, boxsize, 0)
print_an_image = rcode
End Function
Example 2. Print Text and an Image, Implement a Callback Function
Requires Victor Image Processing Library for 32-bit Windows v 5.1 or higher.
........... Add these declarations to your Global module ...........
Declare Function printimagenoeject Lib "VIC32.DLL" (ByVal hWnd As Long, ByVal hdcprn As Long, ByVal mode As Long, image As imgdes, pRect As RECT, ByVal boxsiz As Long, ByVal dspfct As Long) As Long
Declare Sub RtlMoveMemory Lib "kernel32" (ByVal des As Long, ByVal src As Long, ByVal cnt As Long)
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (des As Any, ByVal src As Long, ByVal cnt As Long)
Global cancelprt As Long ' Print-cancel flag, set to 1 when cancel button is clicked
............ Add these functions to your .BAS module, not to a .FRM module ...........
Public Function displayprintcancelbox(ByVal progress As Long) As Long
' printimage() callback function
If printprogress.Visible = True Then ' If dialog is visible, display band number
printprogress.Label1 = LCase$("printing band number " & progress)
End If
' Print-cancel flag is set to 1 when cancel button is clicked
' If return value is 1, printimage will cancel printing
displayprintcancelbox = cancelprt
End Function
Public Function dotheprintwithcancelbox(hWnd As Long) As Long
Dim rcode As Long
Dim pRect As RECT
Dim docname As String
Dim boxsize As Long, mode As Long
Dim dspfct As Long
Dim bm As BITMAPINFOHEADER
boxsize = 0 ' Size of frame around printed image
mode = 0 ' Use default print mode
docname = "Victor Library" 'Print Manager job name
' Get Bitmap info
CopyMemory bm, vimage.bmh, 40 ' 40=size of BITMAPINFOHEADER
pRect.top = 0 ' Position of printed area on printed page in 1/1000 inch units
pRect.left = 1000 ' One inch from left
pRect.right = pRect.left + 3000 ' Print image 3 inches wide
' Maintain aspect ratio of image
pRect.bottom = (pRect.right - pRect.left) * bm.biHeight / bm.biWidth
' Print some text below the picture
Printer.ScaleMode = 5 ' Inches
Printer.CurrentX = 1 ' One inch from left
Printer.CurrentY = (pRect.bottom + 1000 / 4) / 1000 ' 1/4 inch below picture
Printer.Print "This is a Victor Image"
Screen.MousePointer = 11 ' Display the hourglass mouse pointer.
cancelprt = 0 ' Clear cancel printing flag
printprogress.Show ' Display print progress dialog
' Print the image
rcode = printimagenoeject(hWnd, Printer.hdc, mode, vimage, pRect, boxsize, AddressOf displayprintcancelbox)
printprogress.Hide ' Hide print progress dialog
Screen.MousePointer = 0 ' Restore mouse pointer
If rcode <> NO_ERROR Then
Printer.KillDoc ' Cancel printing if error
End If
Printer.EndDoc
' Handle any errors
If rcode <> NO_ERROR Then
MainWnd.error_handler rcode, ""
End If
dotheprintwithcancelbox = rcode
End Function
............ Add to the printprogress.frm module ...........
Private Sub Command1_Click()
' When the user clicks "Cancel" button in the print progress dialog box
cancelprt = 1
End Sub
source code:
catenary how to print
In this example we'll print an image 3 inches wide at the top of the page. The image has previously been loaded and is defined by vimage, a global variable that holds the image descriptor.
Requires Victor Image Processing Library for 32-bit Windows v 5.1 or higher. Find it here.
........... Add these declarations to your Global module ...........
Declare Function printimage Lib "VIC32.DLL" (ByVal hWnd As Long, ByVal hdcprn As Long, ByVal mode As Long, image As imgdes, pRect As RECT, ByVal boxsiz As Long, ByVal dspfct As Long) As Long
Declare Sub RtlMoveMemory Lib "kernel32" (ByVal des As Long, ByVal src As Long, ByVal cnt As Long)
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (des As Any, ByVal src As Long, ByVal cnt As Long)
............ Add this function to your .BAS module, not to a .FRM module ...........
Public Function print_an_image(hWnd As Long) As Long
Dim rcode As Long
Dim pRect As RECT
Dim boxsize As Long, mode As Long
Dim bm As BITMAPINFOHEADER
boxsize = 0 ' Size of frame around printed image
mode = 0 ' Use default print mode
' Get Bitmap info
CopyMemory bm, vimage.bmh, 40 ' 40=size of BITMAPINFOHEADER
pRect.top = 0 ' Position of printed area on printed page in 1/1000 inch units
pRect.left = 1000 ' One inch from left
pRect.right = pRect.left + 3000 ' Print image 3 inches wide
' Maintain aspect ratio of image
pRect.bottom = (pRect.right - pRect.left) * bm.biHeight / bm.biWidth
' Print the image
rcode = printimage(hWnd, Printer.hdc, mode, vimage, pRect, boxsize, 0)
print_an_image = rcode
End Function
Example 2. Print Text and an Image, Implement a Callback Function
Requires Victor Image Processing Library for 32-bit Windows v 5.1 or higher.
........... Add these declarations to your Global module ...........
Declare Function printimagenoeject Lib "VIC32.DLL" (ByVal hWnd As Long, ByVal hdcprn As Long, ByVal mode As Long, image As imgdes, pRect As RECT, ByVal boxsiz As Long, ByVal dspfct As Long) As Long
Declare Sub RtlMoveMemory Lib "kernel32" (ByVal des As Long, ByVal src As Long, ByVal cnt As Long)
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (des As Any, ByVal src As Long, ByVal cnt As Long)
Global cancelprt As Long ' Print-cancel flag, set to 1 when cancel button is clicked
............ Add these functions to your .BAS module, not to a .FRM module ...........
Public Function displayprintcancelbox(ByVal progress As Long) As Long
' printimage() callback function
If printprogress.Visible = True Then ' If dialog is visible, display band number
printprogress.Label1 = LCase$("printing band number " & progress)
End If
' Print-cancel flag is set to 1 when cancel button is clicked
' If return value is 1, printimage will cancel printing
displayprintcancelbox = cancelprt
End Function
Public Function dotheprintwithcancelbox(hWnd As Long) As Long
Dim rcode As Long
Dim pRect As RECT
Dim docname As String
Dim boxsize As Long, mode As Long
Dim dspfct As Long
Dim bm As BITMAPINFOHEADER
boxsize = 0 ' Size of frame around printed image
mode = 0 ' Use default print mode
docname = "Victor Library" 'Print Manager job name
' Get Bitmap info
CopyMemory bm, vimage.bmh, 40 ' 40=size of BITMAPINFOHEADER
pRect.top = 0 ' Position of printed area on printed page in 1/1000 inch units
pRect.left = 1000 ' One inch from left
pRect.right = pRect.left + 3000 ' Print image 3 inches wide
' Maintain aspect ratio of image
pRect.bottom = (pRect.right - pRect.left) * bm.biHeight / bm.biWidth
' Print some text below the picture
Printer.ScaleMode = 5 ' Inches
Printer.CurrentX = 1 ' One inch from left
Printer.CurrentY = (pRect.bottom + 1000 / 4) / 1000 ' 1/4 inch below picture
Printer.Print "This is a Victor Image"
Screen.MousePointer = 11 ' Display the hourglass mouse pointer.
cancelprt = 0 ' Clear cancel printing flag
printprogress.Show ' Display print progress dialog
' Print the image
rcode = printimagenoeject(hWnd, Printer.hdc, mode, vimage, pRect, boxsize, AddressOf displayprintcancelbox)
printprogress.Hide ' Hide print progress dialog
Screen.MousePointer = 0 ' Restore mouse pointer
If rcode <> NO_ERROR Then
Printer.KillDoc ' Cancel printing if error
End If
Printer.EndDoc
' Handle any errors
If rcode <> NO_ERROR Then
MainWnd.error_handler rcode, ""
End If
dotheprintwithcancelbox = rcode
End Function
............ Add to the printprogress.frm module ...........
Private Sub Command1_Click()
' When the user clicks "Cancel" button in the print progress dialog box
cancelprt = 1
End Sub
source code:
catenary how to print
Labels: printing

















