首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >每当我的for next循环在获取面板图像的迭代结束时,我想在printpreviewdialog中添加一个页面

每当我的for next循环在获取面板图像的迭代结束时,我想在printpreviewdialog中添加一个页面
EN

Stack Overflow用户
提问于 2019-01-25 11:10:51
回答 1查看 82关注 0票数 2

我有一个有布局的面板,我想把它带到printdialog,并在每次for next循环结束时向其中添加一个页面,并更改面板的布局,这样我就可以打印For循环收集的所有页面,并一次性打印所有页面。我的代码是这样的:

代码语言:javascript
复制
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循环的其他记录或行,并且只打印我的文本框的最后一行。

EN

回答 1

Stack Overflow用户

发布于 2019-02-11 12:01:46

这不是对问题的完整回答,但我想发布一个相当长的例子,所以评论是不合适的。下面的代码演示了如何打印多页。如果您创建了一个包含ButtonPrintDocumentPrintPreviewDialog的窗体,并设置了对话框的Document属性,则可以运行此代码并查看其实际效果。设置断点并逐步执行PrintPage事件处理程序,以查看如何跨页跟踪要打印的当前项。

代码语言:javascript
复制
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 Class
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54358458

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档