Private Sub pdoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdoc.PrintPage
Declare a variable to hold the posITion of the last printed char. Declare
as static so that subsequent PrintPage events can reference IT. Static intCurrentChar As Int32
InITialize the font to be used for printing.
Dim font As New font("Microsoft Sans Serif", 24)
Dim intPrintAreaHeight, intPrintAreaWidth, marginLeft, marginTop As Int32
WITh pdoc.DefaultPageSettings
InITialize local variables that contain the bounds of the printing
area rectangle.
intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
InITialize local variables to hold margin values that will serve
as the X and Y coordinates for the upper left corner of the printing
area rectangle.
marginLeft = .Margins.Left ' X coordinate
marginTop = .Margins.Top ' Y coordinate
End WITh
If the user selected Landscape mode, swap the printing area height
and width.
If pdoc.DefaultPageSettings.Landscape Then
Dim intTemp As Int32
intTemp = intPrintAreaHeight
intPrintAreaHeight = intPrintAreaWidth
intPrintAreaWidth = intTemp
End If
Calculate the total number of lines in the document based on the height of
the printing area and the height of the font.
Dim intLineCount As Int32 = CInt(intPrintAreaHeight / font.Height)
InITialize the rectangle structure that defines the printing area. Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, intPrintAreaWidth, intPrintAreaHeight) Instantiate the StringFormat class, which encapsulates text layout
information (such as alignment and line spacing), display manipulations
(such as ellipsis insertion and national digit substITution) and OpenType
features. Use of StringFormat causes MeasureString and DrawString to use
only an integer number of lines when printing each page, ignoring partial
lines that would otherwise likely be printed if the number of lines per page do not divide up cleanly for each page (which is usually the case).
See further discussion in the SDK documentation about StringFormatFlags.
Dim fmt As New StringFormat(StringFormatFlags.LineLimIT)
Call MeasureString to determine the number of characters that will fIT in
the printing area rectangle. The CharFITted Int32 is passed ByRef and used
later when calculating intCurrentChar and thus HasMorePages. LinesFilled
is not needed for this sample but must be passed when passing CharsFITted.
Mid is used to pass the segment of remaining text left off from the
previous page of printing (recall that intCurrentChar was declared as
static.
Dim intLinesFilled, intCharsFITted As Int32
e.Graphics.MeasureString(Mid(rtbox.Text, intCurrentChar + 1), font, _
New SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt, _
intCharsFITted, intLinesFilled)
Print the text to the page.
e.Graphics.DrawString(Mid(rtbox.Text, intCurrentChar + 1), font, _
Brushes.Black, rectPrintingArea, fmt)
Advance the current char to the last char printed on this page. As
intCurrentChar is a static variable, ITs value can be used for the next
page to be printed. IT is advanced by 1 and passed to Mid() to print the
next page (see above in MeasureString()).
intCurrentChar += intCharsFITted
HasMorePages tells the printing module whether another PrintPage event
should be fired.
If intCurrentChar < rtbox.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
Private Sub printpreview()
Dim ppd As New PrintPreviewDialog()
Try
ppd.Document = pdoc
ppd.ShowDialog()
Catch exp As Exception
MessageBox.Show("有错误发生!!不能预览 !" & _
"确信现在你是否能够 " & _
"连接到一个打印机?" & _
"然后预览才可以.", Me.Text, _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub mPrintpreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mPrintpreview.Click
printpreview()
End Sub
|