第一步:在視窗類別中申明並執行個體化PrinterPageSetting,當然執行個體化對象可以放在建構函式中。
C#:
private GoldPrinter.PrinterPageSetting printerPageSetting = new GoldPrinter.PrinterPageSetting();
VB.net:
Private printerPageSetting As New GoldPrinter.PrinterPageSetting
VB.net:
Private Sub printDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim g As System.Drawing.Graphics = e.Graphics
If Me.Draw(g) Then
e.HasMorePages = True '要分頁列印
Else
e.HasMorePages = False '列印結束
End If
End Sub
如果你不知上一句是什麼意思,就用這一句吧:
this.printerPageSetting.PrintPageValue = new GoldPrinter.PrintPageDelegate(PrintDocument_PrintPage_Handler);
意思就是告訴printerPageSetting列印的具體實現過程是PrintDocument_PrintPage_Handler(第二步)
VB.net:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.printerPageSetting.PrintPageValue = New GoldPrinter.PrintPageDelegate(AddressOf printDocument_PrintPage)
End Sub
VB.net:
Private Sub menuFilePageSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuFilePageSetup.Click
Me.printerPageSetting.ShowPageSetupDialog()
End Sub
Private Sub menuFilePrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuFilePrintPreview.Click
Me.printerPageSetting.ShowPrintPreviewDialog()
End Sub
Private Sub menuFilePrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuFilePrint.Click
Me.printerPageSetting.ShowPrintSetupDialog()
End Sub
VB.net:
Private Function Draw(ByVal g As System.Drawing.Graphics) As Boolean
g.DrawString("Hello World!", New Font("宋體", 15), Brushes.Black, New PointF(0, 0))
Return False
End Function
VB.net:
Private Function Draw(ByVal g As System.Drawing.Graphics) As Boolean
Return DrawText(g, Me.printerPageSetting.PrintDocument, Me.TextBox1.Text)
End Function
'這段代碼改編自MSDN
Private Function DrawText(ByVal g As System.Drawing.Graphics, ByVal pdoc As System.Drawing.Printing.PrintDocument, ByVal text As String) As Boolean
g.DrawString("Hello World!", New Font("宋體", 15), Brushes.Black, New PointF(0, 0))
Return False
Static intCurrentChar As Int32
Dim font As New Font("宋體", 10)
Dim intPrintAreaHeight, intPrintAreaWidth As Int32
With pdoc.DefaultPageSettings
intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
End With
' 橫向列印,寬與高交換
If pdoc.DefaultPageSettings.Landscape Then
Me.Swap(intPrintAreaWidth, intPrintAreaHeight)
End If
'定義列印範圍
Dim rectPrintingArea As New RectangleF(pdoc.DefaultPageSettings.Margins.Left, pdoc.DefaultPageSettings.Margins.Top, intPrintAreaWidth, intPrintAreaHeight)
Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
Dim intLinesFilled, intCharsFitted As Int32
g.MeasureString(Mid(text, intCurrentChar + 1), font, _
New SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt, _
intCharsFitted, intLinesFilled)
RectangleF recPrintArea = new RectangleF(this.printerPageSetting.PrintDocument.DefaultPageSettings.Margins.Left,this.printerPageSetting.PrintDocument.DefaultPageSettings.Margins.Top,width,height);