用循环打印vb.net表单

时间:2014-11-27 10:03:19

标签: vb.net winforms loops printing

您好我正在使用vb.net

打印表单上的多个盒子标签副本

副本将取决于作业中的方框数量,范围为1到500份。 我想打印这些,但如果需要也打印一个选择的盒号。 我的所有尝试都失败了,任何人都可以提供帮助。

问题是它捕获屏幕包括当前正在打印或完成的内容的msgbox,并将它们全部调用到文档1.是否有一种简单的方法可以在没有屏幕截图的情况下进行打印。

这是我目前的代码

Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Printing


Public Class print

    Inherits Form
    Private WithEvents printButton As New Button
    Private WithEvents printDocument1 As New PrintDocument


    Dim memoryImage As Bitmap

    Private Sub CaptureScreen()
        Dim myGraphics As Graphics = Me.CreateGraphics()
        Dim s As Size = Me.Size
        memoryImage = New Bitmap(s.Width, s.Height, myGraphics)
        Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
        memoryGraphics.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, s)
    End Sub

    Private Sub printDocument1_PrintPage(ByVal sender As System.Object, _
       ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles _
       printDocument1.PrintPage
        e.Graphics.DrawImage(memoryImage, 0, 0)
    End Sub

 Private Sub printButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles printButton.Click
Dim numberofboxes As Integer = 3
Dim startbox As Integer = 1
Dim counter As Integer = 1
For index As Integer = startbox To numberofboxes
    CaptureScreen()
    printDocument1.Print()
    lblFirstBoxPrint.Text = counter
    counter = counter + 1
    printDocument1.PrinterSettings.PrintToFile = True
    printDocument1.PrinterSettings.PrintFileName = "C:\Users\Joseph\Desktop\test '" & counter & "'.xps"
    printDocument1.Print()
Next
End Sub

    Public Shared Sub Main()
        Application.Run(New print())
    End Sub

    Private Sub print_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        printButton.Text = "Print Form"
        Me.Controls.Add(printButton)
    End Sub


End Class

1 个答案:

答案 0 :(得分:0)

如果您希望实际打印表单(如屏幕上显示),请考虑内置PrintForm Component。添加组件和设置属性后,打印是一个单行过程。

 PrintForm1.Print()

有关详细信息,请参阅此MSDN tutorial