您好,欢迎来到乌哈旅游。
搜索
您的当前位置:首页如何实现VB.NET打印控件的使用

如何实现VB.NET打印控件的使用

来源:乌哈旅游
如何实现VB.NET打印控件的使⽤ system.drawing.printing 命名空间printerSettings 打印机设置类PageSettings 页⾯设置类PrintPageEventArgs 要打印页的设置信息类⼀、打印图⽚(加⼊控件printdocument1⽅法)[html] view plaincopy01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try AddHandler PrintDocument1.PrintPage, AddressOf Me.PrintGraphics PrintDocument1.Print() Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub PrintGraphics(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs) ev.Graphics.DrawImage(System.Drawing.Image.FromFile(TextBox1.Text), ev.Graphics.VisibleClipBounds) ev.HasMorePages = False End Sub End Class ⼆、打印⽂字(创建对象printdocument⽅法)[html] view plaincopy01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try Dim printDoc As New System.Drawing.Printing.PrintDocument AddHandler printDoc.PrintPage, AddressOf Me.PrintText printDoc.Print() Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub PrintText(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs) ev.Graphics.DrawString(TextBox1.Text, New Font(\"Arial\ ev.HasMorePages = False End Sub End Class 上⾯两个⽅法都是简单的⽅法,但有重⼤的缺点:1、字符串不会⾃动转⾏,也就是说过长的字符串会“打印”到页⾯的“外⾯”去;2、只能打印⼀页。下⾯来解决上⾯两个问题。三、完美打印过长的字符串(占有⼏页)描述:open按钮打开⼀个对话框,选择⼀个⽂件,并将⽂本反映到richtextbox上。同时激活print,再按,就执⾏打印。

[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

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- wuhaninfo.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务