17,741
社区成员
发帖
与我相关
我的任务
分享
Imports System.Drawing
Imports System.Drawing.Font
Imports System.IO '执行IO操作的命名空间
Imports System.IO.IsolatedStorage '执行独立存储操作的命名空间
Imports System.Drawing.Printing '打印所需的命名空间
Public Class FormPrint
'处理打印事件
Private WithEvents PrintText As New PrintDocument
'精确打印事件
Private Sub PrintText_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintText.PrintPage
Static IntCurrentChar As Int32
Dim Font As Font = Me.RichTextBox.Font
Dim IntPrintAreaHeight, IntPrintAreaWidth, MarginLeft, MarginTop As Int32
With PrintText.DefaultPageSettings
IntPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
IntPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
MarginLeft = .Margins.Left
MarginTop = .Margins.Top
End With
If PrintText.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)
Dim IntLinesFilled, IntCharsFitted As Int32
e.Graphics.MeasureString(Mid(RichTextBox.Text, IntCurrentChar + 1), Font, New SizeF(IntPrintAreaWidth, IntPrintAreaHeight), Fmt, intCharsFitted, intLinesFilled)
e.Graphics.DrawString(Mid(RichTextBox.Text, IntCurrentChar + 1), Font, Brushes.Black, RectPrintingArea, Fmt)
IntCurrentChar += intCharsFitted
If IntCurrentChar < RichTextBox.Text.Length Then
e.HasMorePages = True
Else
e.HasMorePages = False
IntCurrentChar = 0
End If
End Sub
Private Sub ButtonPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPrint.Click
Dim PD As New PrintDialog
PD.Document = PrintText
If PD.ShowDialog = DialogResult.OK Then
PrintText.Print()
End If
End Sub
Private Sub ButtonPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPrintPreview.Click
Dim PPD As New PrintPreviewDialog
Try
PPD.Document = PrintText
PPD.ShowDialog()
Catch ex As Exception
MessageBox.Show("有错误发生!不能预览!")
End Try
End Sub
Private Sub ButtonPageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPageSetup.Click
Dim PSD As New PageSetupDialog
With PSD
.Document = PrintText
.PageSettings = PrintText.DefaultPageSettings
End With
If PSD.ShowDialog = DialogResult.OK Then
PrintText.DefaultPageSettings = PSD.PageSettings
End If
End Sub
End Class
Imports System.Drawing.Printing
Private WithEvents Printer As New PrintDocument