[vb] view plain
copy
01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. Imports System.IO
Imports System.Drawing.Printing
Public Class Form1
Private PrintPageSettings As New PageSettings Private StringToPrint As String
Private PrintFont As New Font(\"Arial\", 10)
Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click Dim FilePath As String
OpenFileDialog1.Filter = \"Text files (*.txt)|*.txt\" OpenFileDialog1.ShowDialog()
If OpenFileDialog1.FileName <> \"\" Then FilePath = OpenFileDialog1.FileName Try
Dim MyFileStream As New FileStream(FilePath, FileMode.Open)
RichTextBox1.LoadFile(MyFileStream, RichTextBoxStreamType.PlainText) MyFileStream.Close()
StringToPrint = RichTextBox1.Text '初始化打印字符串 btnPrint.Enabled = True Catch ex As Exception
MessageBox.Show(ex.Message) End Try End If End Sub
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click Try
'指定当前页设置
PrintDocument1.DefaultPageSettings = PrintPageSettings '指定“打印”对话框的⽂档并显⽰ StringToPrint = RichTextBox1.Text
PrintDialog1.Document = PrintDocument1
Dim result As DialogResult = PrintDialog1.ShowDialog() If result = DialogResult.OK Then
PrintDocument1.Print() '打印,并⾮由“打印”对话框控制.正如OpenFileDialog表现的形式⼀样。 End If
Catch ex As Exception
MessageBox.Show(ex.Message) End Try End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventA Dim numChars As Integer Dim numLines As Integer Dim stringForPage As String
Dim strFormat As New StringFormat
'根据页⾯设置,定义可⽤的页⾯区域(打印区域)
Dim rectDraw As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height '定义区域,来确定⼀个页⾯可容纳多少⽂本,并使⽂本⾼度少⼀⾏,以免⽂本被减短
Dim sizeMeasure As New SizeF(e.MarginBounds.Width, e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))
'处理长字符串时,按单词进⾏断开(换⾏) strFormat.Trimming = StringTrimming.Word
'⽤MeasureString计算出可容纳的字符串个数numChars和⾏数numLines
e.Graphics.MeasureString(StringToPrint, PrintFont, sizeMeasure, strFormat, numChars, numLines) '计算出适应页⾯的字符串
stringForPage = StringToPrint.Substring(0, numChars) '(逻辑上)在当前页打印字符串
e.Graphics.DrawString(stringForPage, PrintFont, Brushes.Black, rectDraw, strFormat)
60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. e.Graphics.DrawString(stringForPage, PrintFont, Brushes.Black, rectDraw, strFormat) '若还有需要打印的⽂本,则继续处理剩下的页⾯ If numChars < StringToPrint.Length Then '删除已经打印的字符串 StringToPrint = StringToPrint.Substring(numChars) e.HasMorePages = True Else e.HasMorePages = False StringToPrint = RichTextBox1.Text End If End Sub End Class 开始读了N久没明⽩什么意思?看了⼀下午,再逐条进⾏调试才明⽩原来打印的原理是这样的:1、⾸先Printdialog1打印对话框,只是设置选择哪个打印机,⽤哪种⽅式来打印(通过PrintDialog1.Document = PrintDocument1来进⾏关联),与具体打印的字符串⽆关。2、关键:PrintPage事件发⽣在打印“当前”页⾯,也就是说打印3页,这个事件就会发⽣3次。每次可以通过此事件来设置页⾯内容(逻辑上)。这样就可以控制打印多页。3、通过MeasureString来计算每页可容纳的字符串和⾏数,这样就很好的控制每页的具体字符串,循环这个⽅法计算剩下的字符进⾏每页设置并打印。再增加两个按钮,⼀个页⾯设置,⼀个是页⾯预览,代码如下:[vb] view plaincopy01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. Private Sub btnSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSetup.Click Try '页⾯设置 PageSetupDialog1.PageSettings = PrintPageSettings PageSetupDialog1.ShowDialog() Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click Try '页⾯预览 PrintDocument1.DefaultPageSettings = PrintPageSettings StringToPrint = RichTextBox1.Text PrintPreviewDialog1.Document = PrintDocument1 PrintPreviewDialog1.ShowDialog() Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub
因篇幅问题不能全部显示,请点此查看更多更全内容