Class: APIPrinter in Category Microsoft Visual Basic 6 : Printing from Total Visual SourceBook

Working with the printer using the Windows API in VB6.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the CAPIPrinter class.
CurrentX Property Get the current X position of printer drawing.
CurrentY Property Get the current Y position of printer drawing.
Font Property Get the font used for printing text.
hDC Property Get the device context of the printer.
Height Property Get the height of the printer drawing surface.
PrintJobID Property Get the ID of the current print job. This property is only valid after the StartDoc method has been called.
Width Property Get the width of the printer drawing surface.
Class_Initialize Initialize Set initial values to defaults which may be overridden with property settings.
Class_Terminate Terminate Make sure the print job is finished.
DrawLine Method Draws a line.
DrawPicture Method Draws a picture. If the optional parameters lngWidth and lngHeight are not specified, this procedure scales the bitmap to the printer.
DrawText Method Draws text.
EndDoc Method Ends a print job.
EndPage Method Finish printing a page.
ScaleX Method Converts X units between the screen and printer.
ScaleY Method Converts Y units between the screen and printer.
StartDoc Method Begins a print job.
StartPage Method Prepares the printer for a new page.
GetTextHeight Method Get the height of the text.
GetTextWidth Method Get the width of the text. The font height is measured in logical device units. Most windows API functions for dealing with fonts require the font height in logical units. This function only works correctly in the MM_TEXT mapping mode.
PointsToHeight Private Converts a font point SIZEAPI to a font height. The font height is measured in logical device units. Most windows API functions for dealing with fonts require the font height in logical units. This function only works correctly in the MM_TEXT mapping mode.
' Example of CAPIPrinter
' To try this example, do the following:
'  1. Create a new form
'  2. Add a command button named 'cmdPrint'
'  3. Add a command button named 'cmdFont'
'  4. Add a text box named 'txtTest'
'  5. Add a Picture box named 'picTest' and set the Picture property to a picture of your choosing
'  6. In VB6, make sure the Common Dialog Control is on your toolbox.
'     If it's not already there, go under Project, Components, Controls tab, and check Microsoft Common Dialog Control (6.0)
'  7. Add a common dialog control named 'cdlFont'
'  8. Add a common dialog control named 'cdlPrint'
'  9. Paste all the code from this example to the new form's module
' 10. Run the form

Private mclsAPIPrinter As CAPIPrinter

Private Sub cmdFont_Click()
  ' Comments: Set the font to the current printer font and display the font dialog

  cdlFont.FontBold = mclsAPIPrinter.Font.Bold
  cdlFont.FontItalic = mclsAPIPrinter.Font.Italic
  cdlFont.FontName =
  cdlFont.FontSize = mclsAPIPrinter.Font.Size
  cdlFont.FontStrikethru = mclsAPIPrinter.Font.Strikethrough
  cdlFont.FontUnderline = mclsAPIPrinter.Font.Underline

  cdlFont.Flags = cdlCFScreenFonts

  ' Show the font dialog

End Sub

Private Sub cmdPrint_Click()

  cdlPrint.Flags = cdlPDReturnDC

  ' if user presses the cancel button, just exit the procedure
  On Error GoTo Cancel_Print

  cdlPrint.CancelError = True

  ' Set the hdc
  mclsAPIPrinter.hDC = cdlPrint.hDC

  ' setup font
  mclsAPIPrinter.Font.Bold = cdlFont.FontBold
  mclsAPIPrinter.Font.Italic = cdlFont.FontItalic = cdlFont.FontName
  mclsAPIPrinter.Font.Size = cdlFont.FontSize
  mclsAPIPrinter.Font.Strikethrough = cdlFont.FontStrikethru
  mclsAPIPrinter.Font.Underline = cdlFont.FontUnderline

  ' Begin printing
  mclsAPIPrinter.StartDoc "Test Document"
  ' Start the page

  ' Center at top of form
   mclsAPIPrinter.CurrentX = (mclsAPIPrinter.Width - mclsAPIPrinter.TextWidth(txtTest)) / 2
  mclsAPIPrinter.CurrentY = 0
  mclsAPIPrinter.DrawText txtTest

  ' Draw a line underneath the text
  mclsAPIPrinter.DrawLine 0, mclsAPIPrinter.TextHeight(txtTest), mclsAPIPrinter.Width, mclsAPIPrinter.TextHeight(txtTest)

  ' Center picture in form
  mclsAPIPrinter.CurrentX = (mclsAPIPrinter.Width - mclsAPIPrinter.ScaleX(ScaleX(picTest.Width, Me.ScaleMode, vbPixels))) / 2
  mclsAPIPrinter.CurrentY = (mclsAPIPrinter.Height - mclsAPIPrinter.ScaleY(ScaleX(picTest.Height, Me.ScaleMode, vbPixels))) / 2
  mmAPIPrinter.DrawPicture picTest.Picture

  ' End the page

  ' Finish printing the document


End Sub

Private Sub Form_Load()

  Set mclsAPIPrinter = New CAPIPrinter

  cmdPrint.Caption = "Print"
  cmdFont.Caption = "Font"
  txtTest = "Example Text"

End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!

View all FMS products for Microsoft Access All Our Microsoft Access Products


Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info




Free Product Catalog from FMS