Public Class frmMain
Inherits System.Windows.Forms.Form
Private WithEvents pdoc As New PrintDocument()
Private Sub btnPrintDialog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintDialog.Click
Dim dialog As New PrintDialog()
dialog.Document = pdoc
If dialog.ShowDialog = DialogResult.OK Then
pdoc.Print()
End If
End Sub
Private Sub btnPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click
Dim ppd As New PrintPreviewDialog()
Try
ppd.Document = pdoc
ppd.ShowDialog()
Catch exp As Exception
MessageBox.Show("An error occurred while trying to load the " & _
"document for Print Preview. Make sure you currently have " & _
"access to a printer. A printer must be connected and " & _
"accessible for Print Preview to work.", Me.Text, _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub btnPageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPageSetup.Click
Dim psd As New PageSetupDialog()
With psd
.Document = pdoc
.PageSettings = pdoc.DefaultPageSettings
End With
If psd.ShowDialog = DialogResult.OK Then
pdoc.DefaultPageSettings = psd.PageSettings
End If
End Sub
Private Sub pdoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdoc.PrintPage
Static intCurrentChar As Int32
Dim intPrintAreaHeight, intPrintAreaWidth, marginLeft, marginTop As Int32
With pdoc.DefaultPageSettings
intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
marginLeft = .Margins.Left ' X coordinate
marginTop = .Margins.Top ' Y coordinate
End With
If pdoc.DefaultPageSettings.Landscape Then
Dim intTemp As Int32
intTemp = intPrintAreaHeight
intPrintAreaHeight = intPrintAreaWidth
intPrintAreaWidth = intTemp
End If
Dim intLineCount As Int32 = CInt(intPrintAreaHeight / font.Height)
Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, intPrintAreaWidth, intPrintAreaHeight)
Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
打印出东西
' static.
Dim intLinesFilled, intCharsFitted As Int32
e.Graphics.MeasureString(Mid(txtDocument.Text, intCurrentChar + 1), font, _
New SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt, _
intCharsFitted, intLinesFilled)
' Print the text to the page.
e.Graphics.DrawString(Mid(txtDocument.Text, intCurrentChar + 1), font, _
Brushes.Black, rectPrintingArea, fmt)
intCurrentChar += intCharsFitted
If intCurrentChar < txtDocument.Text.Length Then
e.HasMorePages = True
Else
e.HasMorePages = False
' You must explicitly reset intCurrentChar as it is static.
intCurrentChar = 0
End If
End Sub
' Specifies what happens when the PrintPage event is raised.
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
Dim obj As Bitmap
Dim iData As IDataObject = Clipboard.GetDataObject()
' Determines whether the data is in a format you can use.
If iData.GetDataPresent(DataFormats.Bitmap) Then
obj = iData.GetData(DataFormats.Bitmap)
ev.Graphics.DrawImage(obj, _
obj.GetBounds(System.Drawing.GraphicsUnit.Pixel)) 'ev.Graphics.VisibleClipBounds)
End If
ev.HasMorePages = False
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
'Printscreen
SendKeys.SendWait("%{PRTSC}")
' Assumes the default printer.
Dim pd As New PrintDocument()
AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
pd.DefaultPageSettings.Landscape = True
pd.Print()
Catch ex As Exception
MessageBox.Show("An error occurred while printing" & vbCrLf & _
ex.ToString(), "Error")
End Try
End Sub