winforms VB窗口应用程序中的打印窗体

lymnna71  于 2022-12-27  发布在  其他
关注(0)|答案(1)|浏览(157)

我使用VS2013创建了一个Windows应用程序,表单包含很少的标签和文本框。Form1有一点点大,所以我无法以实际大小打印,它在人像模式下打印。
在我的项目中,我添加了PrintFormPageSetup对话框,但这个页面设置不会很好地工作,如果我在PageSetup中单击横向,然后打印,它会以纵向模式打印窗体。
页面设置编码

' 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

pzfprimi

pzfprimi1#

你需要考虑一下你是否真的想打印整个表单,因为可能有用户不需要看到的按钮等,学习如何设计一个只包含相关信息的页面是值得的。
要打印表单,您可以:

**选项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

相关问题