在VB窗口应用程序中打印表单

时间:2016-12-17 07:45:51

标签: vb.net

我使用vs2013创建了一个Windows应用程序,该表单包含很少的标签和文本框。 Form1有点大,所以我无法以实际尺寸打印,它在纵向模式下打印。 在我的项目中,我添加了PrintForm和PageSetup对话框,但是这个页面设置不会很好,如果我点击PageSetup中的Landscape然后打印,它会以纵向模式打印表单。  页面设置编码

' initialize the page settings

        PageSetupDialog1.PageSettings = New Printing.PageSettings


        ' hide the network button

        PageSetupDialog1.ShowNetwork = False


        If PageSetupDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

            Dim settings() As Object = New Object() _
                                       {PageSetupDialog1.PageSettings.Margins, _
            PageSetupDialog1.PageSettings.PaperSize, _
            PageSetupDialog1.PageSettings.Landscape, _
            PageSetupDialog1.PrinterSettings.PrinterName, _
            PageSetupDialog1.PrinterSettings.PrintRange}



        End If

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要考虑是否真的要打印整个表单,因为可能存在用户不需要看的按钮等,并且值得学习如何设计仅包含相关信息的打印页面。

要打印表格,您可以:

选项1)下载Visual Basic Powerpack,因为它包含表单打印控件并使用:

选项2) 将表单转换为位图并放入document.print例程。 以下是您可以使用的一些代码:

Imports System.Drawing.Printing
Public Class Form1
Dim WithEvents mPrintDocument As New PrintDocument
Dim mPrintBitMap As Bitmap

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim prtdoc As New PrintDocument
    Dim strDefaultPrinter As String = prtdoc.PrinterSettings.PrinterName
    MsgBox(strDefaultPrinter)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ' Copy the form's image into a bitmap.
    mPrintBitMap = New Bitmap(Me.Width, Me.Width)
    Dim lRect As System.Drawing.Rectangle
    lRect.Width = Me.Width
    lRect.Height = Me.Width
    Me.DrawToBitmap(mPrintBitMap, lRect)
    ' Make a PrintDocument and print.
    mPrintDocument = New PrintDocument
    mPrintDocument.DefaultPageSettings.Landscape = True
    'mPrintDocument.Print() 'send the document to the printer
    Me.PrintPreviewDialog1.Document = mPrintDocument
    PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub m_PrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles mPrintDocument.PrintPage
    ' Draw the image centered.
    Dim lWidth As Integer = e.MarginBounds.X + (e.MarginBounds.Width - mPrintBitMap.Width) \ 2
    Dim lHeight As Integer = e.MarginBounds.Y + (e.MarginBounds.Height - mPrintBitMap.Height) \ 2
    e.Graphics.DrawImage(mPrintBitMap, lWidth, lHeight)
    ' There's only one page.
    e.HasMorePages = False
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Application.Exit()
End Sub
End Class