我有一个有布局的面板,我想把它带到printdialog,并在每次for next循环结束时向其中添加一个页面,并更改面板的布局,这样我就可以打印For循环收集的所有页面,并一次性打印所有页面。我的代码是这样的:
Private bmp As Bitmap
Private Sub PrintDTRToolStripMenuItem_Click(sender As System.Object, e As
System.EventArgs) Handles PrintDTRToolStripMenuItem.Click
PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
For Each line In Me.TextBox1.Lines
'(Inside this are codes in passing data to my panel, I cant paste it here because it is too long)
'And this right here is my print code
bmp = New Bitmap(Panel1.DisplayRectangle.Width, Panel1.DisplayRectangle.Height)
Dim G As Graphics = Graphics.FromImage(bmp)
Dim Hdc As IntPtr = G.GetHdc()
SendMessage(Panel1.Handle, WM_PRINT, Hdc, DrawingOptions.PRF_OWNED Or DrawingOptions.PRF_CHILDREN Or DrawingOptions.PRF_CLIENT Or DrawingOptions.PRF_NONCLIENT)
G.ReleaseHdc(Hdc)
G.Dispose()
PrintPreviewDialog1.Document = PrintDocument1
e.HasMorePages = True
e.Graphics.DrawImage(bmp, 0, 0)
e.HasMorePages = True
Next
Dim startIndex = currentItemIndex
Dim endIndex = Math.Min(currentItemIndex + itemsPerPage - 1, TextBox1.Lines.Count - 1)
'There are more pages to print if there are more items to print.
e.HasMorePages = currentItemIndex < TextBox1.Lines.Count
End sub
Private Sub PrintDocument1_BeginPrint(sender As System.Object, e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
currentItemIndex = 0
End Sub它所做的是跳过我的for next循环的其他记录或行,并且只打印我的文本框的最后一行。
发布于 2019-02-11 12:01:46
这不是对问题的完整回答,但我想发布一个相当长的例子,所以评论是不合适的。下面的代码演示了如何打印多页。如果您创建了一个包含Button、PrintDocument和PrintPreviewDialog的窗体,并设置了对话框的Document属性,则可以运行此代码并查看其实际效果。设置断点并逐步执行PrintPage事件处理程序,以查看如何跨页跟踪要打印的当前项。
Imports System.Drawing.Printing
Public Class Form1
Private items As List(Of String) 'Store data to be printed here.
Private itemsPerPage As Integer
Private currentItemIndex As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
items = New List(Of String) From {
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten"}
itemsPerPage = 3
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Start printing.
'PrintDocument1.Print()
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub PrintDocument1_BeginPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.BeginPrint
'Start printing from the first item.
currentItemIndex = 0
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
'Start printing the current page from the current item.
Dim startIndex = currentItemIndex
'Stop printing the current page after one page's worth of items or at the last item.
Dim endIndex = Math.Min(currentItemIndex + itemsPerPage - 1, items.Count - 1)
'Start printing the current page 10 pixels from the top.
Dim yOffset = 10
For currentItemIndex = startIndex To endIndex
'Print the current item.
e.Graphics.DrawString(items(currentItemIndex), Me.Font, Brushes.Black, 10, yOffset)
'Print the next item 25 pixels lower.
yOffset += 25
Next
'There are more pages to print if there are more items to print.
e.HasMorePages = currentItemIndex < items.Count
End Sub
End Classhttps://stackoverflow.com/questions/54358458
复制相似问题