Module FormUtilities
Private PicForm As PictureBox
Private blnFirstTimeThrough As Boolean = True
Friend WithEvents Pd As System.Drawing.Printing.PrintDocument
'=======================================================================
' InitializeFormForPrinting
'=======================================================================
' David Grebner
'
' This procedure instantiates the PrintDocument and the PictureBox
' that are requied by the PrintForm and pd_PrintPage procedures.
'=======================================================================
Private Sub InitializeFormForPrinting()
Pd = New System.Drawing.Printing.PrintDocument
PicForm = New PictureBox
End Sub
'=======================================================================
'PrintForm - Prints the passed form (2 parameter version)
'=======================================================================
' This code was originally written by Joseph G. Letts.
'
' It was modified by David Grebner to run from a standard module and to
' provide the option of printing the form with or without buttons.
'
' This procedure accepts the form as its first parameter which is then
' printed on the default printer. Its second parameter determines
' whether or not the command buttons are removed prior to its printing.
'
' This procedure requires the use of the procedure: ButtonMagic and
' InitializeFormForPrinting.
'=======================================================================
Public Overloads Sub PrintForm(ByVal TheForm As Form, ByVal ButtonVisibility As Boolean)
If blnFirstTimeThrough Then
InitializeFormForPrinting()
blnFirstTimeThrough = False
End If
If ButtonVisibility = False Then
Call ButtonMagic(TheForm, False)
End If
'Now, save active form as bmp to the
'clipboard by simulating <Alt> <Prt Sc> keystrokes
SendKeys.SendWait("%{PRTSC}")
'get clipboard image and put it in a picturebox
PicForm.Image = DirectCast(System.Windows.Forms.Clipboard.GetDataObject.GetData _
(System.Windows.Forms.DataFormats.Bitmap), Bitmap)
Try
Pd.Print()
Catch err As Exception
MessageBox.Show(err.ToString(), "Print Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
Call ButtonMagic(TheForm, True)
End Try
End Sub
'=======================================================================
'PrintForm - Prints the passed form (1 parameter version)
'=======================================================================
' This code was originally written by Joseph G. Letts.
'
' It was modified by David Grebner to run from a standard module.
' This procedure prints the form that is passed to it on the
' default printer. This procedure is overloaded so that it may
' be called using either signature.
'
' This procedure requires the use of the procedures: ButtonMagic and
' InitializeFormForPrinting.
'=======================================================================
Public Overloads Sub PrintForm(ByVal TheForm As Form)
If blnFirstTimeThrough Then
InitializeFormForPrinting()
blnFirstTimeThrough = False
End If
Call ButtonMagic(TheForm, True)
'Now, save active form as bmp to the
'clipboard by simulating <Alt> <Prt Sc> keystrokes
SendKeys.SendWait("%{PRTSC}")
'get clipboard image and put it in a picturebox
PicForm.Image = DirectCast(System.Windows.Forms.Clipboard.GetDataObject.GetData _
(System.Windows.Forms.DataFormats.Bitmap), Bitmap)
Try
Pd.Print()
Catch err As Exception
MessageBox.Show(err.ToString(), "Print Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
Call ButtonMagic(TheForm, True)
End Try
End Sub
'=======================================================================
' ButtonMagic - Makes buttons invisible or visible
'=======================================================================
' Dave Grebner
' This procedure takes two arguments. The first is the form and the
' second is the desired visibility of the command buttons (True to make
' them visible and send False to make them invisible).
'=======================================================================
Private Sub ButtonMagic(ByVal TheContainer As Control, ByVal ButtonVisibility As Boolean)
Dim c As Control
' Look at each of the controls in the form or in the current container
For Each c In TheContainer.Controls
' If the control is itself a container then call the ButtonMagic
' and give it the new container to iterate
If Not c.Controls Is Nothing Then
ButtonMagic(c, ButtonVisibility)
End If
'If the control is a command button, check the desired visibility
'and change all buttons to that setting
If TypeOf c Is Button Then
If ButtonVisibility = True Then
c.Visible = True
Else
c.Visible = False
End If
End If
Next
End Sub
'=======================================================================
' Joseph G. Letts - Prints a Picturebox on the default printer.
'
'=======================================================================
Private Sub pd_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Pd.PrintPage
'---------------------------------------------------------------
' this procedure handles events raised by the printer object pd
'---------------------------------------------------------------
e.Graphics.DrawImageUnscaled(PicForm.Image, 0, 0) 'send image to printer - upper left position
e.HasMorePages = False 'this is the last page to print
End Sub
'=======================================================================
End Module