Visual Basic 8 (2005)Visual Studio 2005.NET 2.0Windows Forms初学者开发Visual StudioWindows.NETVisual Basic
WordWrap 与 PrintDocument





0/5 (0投票)
确保 PrintDocument 对象中文字自动换行的代码。
引言
这是一种简单的确保在显示 PrintDialog
或 PrintPreviewDialog
时文本自动换行的方法。
使用代码
代码很简单……以下是 PrintDialog
的代码
private printFont As Font = New Font("Tahoma", 12, FontStyle.Regular)
private aBunchOfLongLines As String
Private Sub New()
aBunchOfLongLines = "This software is provided 'as-is', with no warrenties " & _
"of any kind stating operability of a specific " & vbNewLine
aBunchOfLongLines &= "or error free operation. Use of " & _
"the application is entirely the user's responsibility. " & vbNewLine
aBunchOfLongLines &= "The software vendor assumes no responsibility " & _
"for use of this application. This includes damage " & vbNewLine
aBunchOfLongLines &= "which may occur during operation of the " & _
"application to the users files or computer system. " & vbNewLine
aBunchOfLongLines &= "By use of this application, you agree that you " & _
"will take full responsibility for any damage that may " & vbNewLine
aBunchOfLongLines &= "occur. If you do not wish to use this " & _
"application any further, please uninstall it from your computer."
End Sub
Private Sub tsbPrint_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles tsbPrint.Click
Dim pd As New Printing.PrintDocument()
Me.PrintDialog1.AllowPrintToFile = False
Me.PrintDialog1.AllowSelection = False
Me.PrintDialog1.AllowSomePages = False
Me.PrintDialog1.UseEXDialog = True
Me.PrintDialog1.Document = pd
If Me.PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
'convert string to stream so we can read the lines
Dim aBytes() As Byte = System.Text.Encoding.ASCII.GetBytes( _
aBunchOfLongLines)
Dim strmMem As New MemoryStream(aBytes)
streamToPrint = New IO.StreamReader(strmMem)
pd.PrinterSettings = Me.PrintDialog1.PrinterSettings
AddHandler pd.PrintPage, AddressOf pd_PrintPage
'print the document
pd.Print()
Catch ex As Exception
Finally
streamToPrint.Close()
End Try
End If
End Sub
以下是 PrintPreviewDialog
的代码
Private Sub tsbPreview_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles tsbPreview.Click
Dim aBytes() As Byte = System.Text.Encoding.ASCII.GetBytes( _
aBunchOfLongLines)
Dim strmMem As New MemoryStream(aBytes)
streamToPrint = New IO.StreamReader(strmMem)
Dim pd As New Printing.PrintDocument()
AddHandler pd.PrintPage, AddressOf pd_PrintPage
Me.PrintPreviewDialog1.AllowTransparency = False
'used to resize the dialog
Dim dlg As Form = DirectCast(Me.PrintPreviewDialog1, Form)
dlg.Width = 600
dlg.Height = 400
dlg.WindowState = FormWindowState.Maximized
Me.PrintPreviewDialog1.Document = pd
Me.PrintPreviewDialog1.ShowDialog()
streamToPrint.Close()
End Sub
这是实际输出到 PrintDocument
的内容。请注意 StringFormat
对象。 这就是导致换行的原因,并且也可以用来测量行的文本区域。
Private Sub pd_PrintPage(ByVal sender As Object, _
ByVal ev As Printing.PrintPageEventArgs)
Dim yPos As Single = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String = Nothing
Dim actual As SizeF = Nothing
yPos = topMargin
While yPos < ev.MarginBounds.Top + ev.MarginBounds.Size.Height
line = streamToPrint.ReadLine()
'stringformat is what makes wrapping happen
Dim sf As StringFormat = StringFormat.GenericTypographic
sf.Alignment = StringAlignment.Near
sf.LineAlignment = StringAlignment.Near
sf.FormatFlags = StringFormatFlags.LineLimit
sf.Trimming = StringTrimming.Word
If line Is Nothing Then
Exit While
ElseIf line.Equals("") Then
'have to have something to easure, even if it is a blank line
line = " "
End If
actual = ev.Graphics.MeasureString(line, printFont, _
New SizeF(ev.MarginBounds.Size.Width, _
ev.MarginBounds.Size.Height), sf)
ev.Graphics.DrawString(line, printFont, Brushes.Black, _
New RectangleF(leftMargin, yPos, ev.MarginBounds.Size.Width, _
ev.MarginBounds.Size.Height), sf)
yPos = yPos + actual.Height
End While
' If more lines exist, print another page.
If (line IsNot Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
希望这能帮助大家。