大家好,下面這段程度 運行到 pd.Print() 報錯,請幫看下
Imports System.IO
Imports System.Drawing.Printing
Imports System.Text
Imports System.Drawing
Public Class Form2
Private sr As StringReader
'Private sr As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sb As New StringBuilder()
sb.Append(" 停車場管理系統 \n")
sb.Append("*************************************\n")
Print(sb.toString())
End Sub
Public Function Print(ByVal str As String) As Boolean
Dim result As Boolean = True
' sr = TextBox1.Text
sr = New StringReader(str.ToString())
Dim pd As PrintDocument = New PrintDocument()
Dim pageSize As New PaperSize("First custom size", 600, 600)
pd.PrintController = New System.Drawing.Printing.StandardPrintController()
pd.DefaultPageSettings.Margins.Top = 2
pd.DefaultPageSettings.Margins.Left = 0
pd.DefaultPageSettings.PaperSize.Width = 320
pd.DefaultPageSettings.PaperSize.Height = 5150
pd.PrinterSettings.PrinterName = pd.DefaultPageSettings.PrinterSettings.PrinterName
pd.OriginAtMargins = True
Dim MARGIN As Margins = New Margins(1, 1, 1, 1)
pd.DefaultPageSettings.Margins = MARGIN
AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
Try
pd.Print()
Catch ex As Exception
result = False
Finally
If sr IsNot Nothing Then sr.Close()
End Try
Return result
End Function
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
Dim printFont As Font = New Font("Arial", 9)
Dim linesPerPage As Single = 0
Dim yPos As Single = 0
Dim count As Integer = 0
Dim leftMargin As Single = ev.MarginBounds.Left
Dim topMargin As Single = ev.MarginBounds.Top
Dim line As String = ""
linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)
'line = sr.ReadLine()) != null)
MsgBox(linesPerPage)
While count < linesPerPage And ((line = sr.ReadLine()) <> "")
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics))
ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
count += 1
End While
If line IsNot Nothing Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub
End Class
--------------------------------------------------------------------------
運行到 pd.Print() 報錯:“System.ComponentModel.Win32Exception”型別的未經處理的例外在 System.Drawing.dll 中發生
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/58418.html
標籤:VB.NET
上一篇:關于WPF依賴屬性
